Text Edit + ComboBox 属性(2)
文章目录
- Text Edit
-
- 1. 代码示例
- 2. 核心属性
- 3. 核心信号
- ComboBox
-
- 1. 代码示例
- 2. 核心属性
- 3. 核心方法
- 4. 核心信号
Text Edit
QTextEdit
表示多行输入框,也是一个富文本,markdown
编辑器,并且能在内容超出编辑框范围时自动提供滚动条
QPlainTextEdit
只能表示纯文本(Plain Text
),QTextEdit
不仅能表示纯文本,还可以表示html
和markdown
1. 代码示例
编辑widget.ui
文件,创建一个label
和Text Edit
控件,右键Text Edit
控件,转到槽,选择textChanged
信号去定义槽函数
// widget.cppvoid Widget::on_textEdit_textChanged(){ // 获取到多行输入框中的内容,将该内容设置到label上 const QString& text = ui->textEdit->toPlainText(); ui->label->setText(text);}
2. 核心属性
markdown
html
img
和 table
等placeHolderText
readOnly
undoRedoEnable
按下
Ctrl + z
触发 undo按下
Ctrl + y
触发 redoautoFormating
tabstopWidth
overwriteMode
acceptRichText
verticalScrollBarPolicy
Qt::ScrollBarAsNeeded
:根据内容自动决定是否需要滚动条(默认值)Qt::ScrollBarAlwaysOff
:总是关闭滚动条Qt::ScrollBarAlwaysOn
:总是显示滚动条horizontalScrollBarPolicy
Qt::ScrollBarAsNeeded
:根据内容自动决定是否需要滚动条(默认值)Qt::ScrollBarAlwaysOff
:总是关闭滚动条Qt::ScrollBarAlwaysOn
:总是显示滚动条3. 核心信号
textChanged()
selectionChanged()
cursorPositionChanged()
undoAvailable(bool)
redoAvailable(bool)
copyAvailable(bool)
演示
QTextEdit
的这几个信号,textChanged
信号
selectionChanged
信号,这里按住键盘上的shift
键 + 左右方向键,可以逐个选中内容(多行内容,也可以按上下方向键进行选中)
cursorPositionChanged()
剩余三个信号:
undo, redo, copy + Available
,哪个是true
按下组合键就有效,比如说undoAvailable: fasle
,按下ctrl + z
就无法撤销
void Widget::on_textEdit_textChanged(){ qDebug() << \"textChanged: \" << ui->textEdit->toPlainText();}void Widget::on_textEdit_selectionChanged(){ QTextCursor cursor = ui->textEdit->textCursor(); qDebug() << \"selectionChanged: \" << cursor.selectedText();}void Widget::on_textEdit_cursorPositionChanged(){ QTextCursor cursor = ui->textEdit->textCursor(); qDebug() << \"cursorPositionChanged: \" << cursor.position();}void Widget::on_textEdit_redoAvailable(bool b){ qDebug() << \"redoAvailabel: \" << b;}void Widget::on_textEdit_undoAvailable(bool b){ qDebug() << \"undoAvailable: \" << b;}void Widget::on_textEdit_copyAvailable(bool b){ qDebug() << \"copyAvailable: \" << b;}
ComboBox
QComboBox
表示一个下拉框
1. 代码示例
使用下拉框模拟麦当劳点餐。首先在界面上创建三个下拉框,三个label
标签和一个提交按钮
接下来往下拉框中添加文本内容,有两种方式,先来看第一种代码方式,编辑widget.cpp
#include \"widget.h\"#include \"ui_widget.h\"#includeWidget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget){ ui->setupUi(this); ui->comboBox->addItem(\"麦辣鸡腿堡\"); ui->comboBox->addItem(\"巨无霸\"); ui->comboBox->addItem(\"双层牛堡\"); ui->comboBox_2->addItem(\"薯条\"); ui->comboBox_2->addItem(\"麦乐鸡块\"); ui->comboBox_2->addItem(\"麦乐鸡翅\"); ui->comboBox_3->addItem(\"百事可乐\"); ui->comboBox_3->addItem(\"雪碧\");}Widget::~Widget(){ delete ui;}void Widget::on_pushButton_clicked(){ // 当用户选中完成后,点击提交按钮,打印出用户的选择 qDebug() << ui->comboBox->currentText() << \", \" << ui->comboBox_2->currentText() << \", \" << ui->comboBox_3->currentText();}
往下拉框添加文本内容,第二种方式,右键下拉框,编辑项目,直接在界面上添加。与上面代码的效果是相同的
但是在实际应用中,下拉框里面的内容很多时候并不是代码中写死的,而是通过文件或网络加载数据得到的(比如选课操作)
从文件中读取下拉框文本内容的实现方法如下:
还是先来最简单的操作,编辑widget.ui
,在界面上创建一个label
和下拉框控件
在本地新建一个名称为data
(任意取)的文本文件,要能找到该文件的路径,后面写代码读取文件要用,后续从文件中读取文本内容时,路径后面要跟上文件名(包括后缀),将\\
改为/
在编辑widget.cpp
源文件前,需要补充文件读写相关的知识。C语言使用fopen
函数进行操作,而C++则提供了fstream
类来实现这一功能
fstream
类可分为ifstream
(输入流类)和ofstream
(输出流类)。关于文件输入(input
)和输出(output
)的区分:输入指从文件读取数据到程序,输出则是将程序数据写入文件
ui->comboBox->addItem()
,这个函数要求的参数类型是QString
,并不是std::string
,虽然都是字符串,但它们是不同的类型
此处就需要进行手动转换,QString::fromStdString(line)
,就可以将std::string
转换成QString
QString s; s.toStdString();
可以将QString
转换成std::string
#include \"widget.h\"#include \"ui_widget.h\"#include#includeWidget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget){ ui->setupUi(this); // 需要读取文件内容,把文件中的每一行读取出来,作为一个ComboBox选项 std::ifstream file(\"D:/Qt proc_position/ComboBox_2/data.txt\"); // 将\"/\"改成\"\\\" if (!file.is_open()){ qDebug() << \"文件打开失败\"; exit(-1); } // getline函数按行来读取文本内容 std::string line; while (std::getline(file, line)){ // 取到的每一行内容,设置到下拉框中 ui->comboBox->addItem(QString::fromStdString(line)); } file.close();// 记得关闭文件}Widget::~Widget(){ delete ui;}
2. 核心属性
currentText
currentIndex
若无选中项,值为
-1
。editable
设为
true
时,行为接近QLineEdit
,可设置验证器validator
iconSize
maxCount
3. 核心方法
addItem(const QString &)
currentIndex()
若无选中项,返回
-1
。currentText()
4. 核心信号
activated(int)
activated(const QString &text)
currentIndexChanged(int)
currentIndexChanged(const QString &text)
用户或程序操作都会触发。
editTextChanged(const QString &text)
editable=true
时有效)。