QComboBox
QComboBox是下拉列表框组件类,它提供一个下拉列表供用户选择,也可以直接当做一个QLineEdit用做输入,每个项还可以关联一个QVariant类型的变量,用于存储一些不可见的数据。
添加简单项
ui->mode_select->addItem("ReadOnly");
添加具有用户数据的项
QVariant val = QByteArray::fromHex("00");
ui->mode_select->addItem(tr("ReadOnly"), val);
QComboBox列表向的访问
QComboBox存储的项是一个列表,但是QComboBox不提供整个列表用于访问,可以通过索引访问某个项。
在QComboBox组件上选择项发生变化时会发射这两个信号
void currentIndexChanged(int index):参数为当前项的索引
void currentIndexChanged(const QString &text):参数为当前项的文字
QPlainTextEdit
QPlainTextEdit是一个多行文本编辑器,用于显示和编辑多行简单文本。
常用方法
图片示例
代码示例
#ifndef QCOMBOBOXANDQPLAINTEXTEDITDIALOG_H
#define QCOMBOBOXANDQPLAINTEXTEDITDIALOG_H
#include
namespace Ui
{
class QComboBoxAndQPlainTextEditDialog;
}
class QComboBoxAndQPlainTextEditDialog : public QDialog
{
Q_OBJECT
public:
explicit QComboBoxAndQPlainTextEditDialog(QWidget* parent = nullptr);
~QComboBoxAndQPlainTextEditDialog();
void init();
private slots:
void on_comboBox_currentIndexChanged(int index);
void on_pushButton_cut_clicked();
void on_pushButton_copy_clicked();
void on_pushButton_paste_clicked();
void on_pushButton_undo_clicked();
void on_pushButton_clear_clicked();
void on_pushButton_redo_clicked();
void on_pushButton_selectAll_clicked();
void on_pushButton_onLine_clicked();
void on_plainTextEdit_customContextMenuRequested(const QPoint& pos);
private:
Ui::QComboBoxAndQPlainTextEditDialog* ui;
};
#endif // QCOMBOBOXANDQPLAINTEXTEDITDIALOG_H
#include "QComboBoxAndQPlainTextEditDialog.h"
#include "ui_QComboBoxAndQPlainTextEditDialog.h"
#include
#include
#include
QComboBoxAndQPlainTextEditDialog::QComboBoxAndQPlainTextEditDialog(QWidget* parent)
: QDialog(parent)
, ui(new Ui::QComboBoxAndQPlainTextEditDialog)
{
ui->setupUi(this);
init();
}
QComboBoxAndQPlainTextEditDialog::~QComboBoxAndQPlainTextEditDialog()
{
delete ui;
}
void QComboBoxAndQPlainTextEditDialog::init()
{
ui->comboBox->addItem("北京", "1");
ui->comboBox->addItem("上海", "2");
ui->comboBox->addItem("广州", "3");
ui->comboBox->addItem("深圳", "4");
ui->comboBox->addItem("天津", "5");
ui->plainTextEdit->setPlainText("测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据");
}
void QComboBoxAndQPlainTextEditDialog::on_comboBox_currentIndexChanged(int index)
{
QString str = ui->comboBox->itemText(index);
ui->showText->setText(str);
int count = ui->comboBox->count();
ui->showCount->setText(QString::number(count));
ui->showIndex->setText(QString::number(index));
}
void QComboBoxAndQPlainTextEditDialog::on_pushButton_cut_clicked()
{
ui->plainTextEdit->cut();
}
void QComboBoxAndQPlainTextEditDialog::on_pushButton_copy_clicked()
{
ui->plainTextEdit->copy();
}
void QComboBoxAndQPlainTextEditDialog::on_pushButton_paste_clicked()
{
ui->plainTextEdit->paste();
}
void QComboBoxAndQPlainTextEditDialog::on_pushButton_undo_clicked()
{
ui->plainTextEdit->undo();
}
void QComboBoxAndQPlainTextEditDialog::on_pushButton_clear_clicked()
{
ui->plainTextEdit->clear();
}
void QComboBoxAndQPlainTextEditDialog::on_pushButton_redo_clicked()
{
ui->plainTextEdit->redo();
}
void QComboBoxAndQPlainTextEditDialog::on_pushButton_selectAll_clicked()
{
ui->plainTextEdit->selectAll();
}
void QComboBoxAndQPlainTextEditDialog::on_pushButton_onLine_clicked()
{
ui->comboBox_2->clear();
//文本对象
QTextDocument* doc = ui->plainTextEdit->document();
//回车符是一个block 获得文本块个数
int cnt = doc->blockCount();
for(int i=0; i<cnt; i++) {
//获取文本中的一行
QTextBlock textLine = doc->findBlockByLineNumber(i);
QString str = textLine.text();
ui->comboBox_2->addItem(str);
qDebug()<<"str"<<str;
}
}
//自带的快捷菜单
void QComboBoxAndQPlainTextEditDialog::on_plainTextEdit_customContextMenuRequested(const QPoint& pos)
{
QMenu* menu = ui->plainTextEdit->createStandardContextMenu();
menu->exec(pos);
}