diff --git a/QtQcomboBoxEx/ExQcomboBox.cpp b/QtQcomboBoxEx/ExQcomboBox.cpp new file mode 100644 index 0000000..18d8c33 --- /dev/null +++ b/QtQcomboBoxEx/ExQcomboBox.cpp @@ -0,0 +1,120 @@ +#include "ExQcomboBox.h" +#include "ui_ExQcomboBox.h" + +#include +#include +#include + +ExQcomboBox::ExQcomboBox(QWidget *parent) : + QWidget(parent), + ui(new Ui::ExQcomboBox) +{ + ui->setupUi(this); + setWindowTitle(QObject::tr("QComboBox和QPlainTextEdit的用法")); + + connect(ui->comBoxRight, &QComboBox::currentTextChanged, this, &ExQcomboBox::onSelectDisplay); +} + +ExQcomboBox::~ExQcomboBox() +{ + delete ui; +} + + +//左上角区域+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +//初始化简单的QComboBox控件 +void ExQcomboBox::on_btnLeftInit_clicked() +{ + QIcon ico; + ico.addFile(":/images/github.ico"); + + ui->comBoxLeft->clear(); + for (int i = 0; i < 13; i++) { + ui->comBoxLeft->addItem(ico, QString("第%1个item项").arg(i)); //带有ico图标的项 + } +} + +//清除简单的QComboBox控件 +void ExQcomboBox::on_btnLeftClear_clicked() +{ + ui->comBoxLeft->clear(); +} + +//勾选QComboBox为可以编辑状态 +void ExQcomboBox::on_checkBoxOnlyWrite_clicked() +{ + if(ui->checkBoxOnlyWrite->isChecked()) + ui->comBoxLeft->setEditable(true); + else + ui->comBoxLeft->setEditable(false); +} + +//右上角区域+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +//初始化复杂的QComboBox控件(给每一项都添加一个对应的自定义数据[不显示]) +void ExQcomboBox::on_btnRightInit_clicked() +{ + QIcon ico; + ico.addFile(":/images/gril.ico"); + + QMap map; + map.insert("张投", "16岁"); + map.insert("张我", "17岁"); + map.insert("张以", "18岁"); + map.insert("张木", "19岁"); + map.insert("张李", "20岁"); + map.insert("张,", "21岁"); + map.insert("张报", "22岁"); + map.insert("张之", "23岁"); + map.insert("张以", "24岁"); + map.insert("张琼", "25岁"); + map.insert("张玖", "26岁"); + map.insert("张。", "27岁"); + + ui->comBoxRight->clear(); + foreach(QString str, map.keys()){ + ui->comBoxRight->addItem(ico, str, map.value(str)); //因为有Map,所以QComboBox显示会按照key排序,而非上面的定义顺序,注意不是map.key(str) + } +} + +//底部区域+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +//文本框内容每次读取一行,添加到ComboBox作为item项 +void ExQcomboBox::on_btnBottomAdd_clicked() +{ + QTextDocument* doc = ui->plainTextEdit->document(); //获取文本对象 + int cnt = doc->blockCount(); //回车符是一个block + QIcon ico; + ico.addFile(":/images/github.ico"); + ui->comBoxLeft->clear(); + ui->comBoxRight->clear(); + + for (int i = 0; i < cnt; i++) { + QTextBlock text = doc->findBlockByNumber(i); //获取文本中一段(以换行为标志) + ui->comBoxLeft->addItem(ico, text.text()); + ui->comBoxRight->addItem(ico, text.text(), QString("附加内容:%1").arg(i)); + } +} + +//清除可编辑的富文本的编辑器的所有内容 +void ExQcomboBox::on_btnBottomClear_clicked() +{ + ui->plainTextEdit->clear(); +} + +//设置富文本的编辑器(plainTextEdit)只可读 +void ExQcomboBox::on_checkBoxOnlyRead_clicked() +{ + if(ui->checkBoxOnlyRead->isChecked()) + ui->plainTextEdit->setEnabled(false); + else + ui->plainTextEdit->setEnabled(true); +} + + +//公共的槽函数区域+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +//显示当前选中的ComboBox的item项的内容 +void ExQcomboBox::onSelectDisplay(QString str) +{ + QString strData = ui->comBoxRight->currentData().toString(); // 获取当前item的关联数据的内容 + ui->labDisplay->setText(str + " " + strData); + ui->plainTextEdit->appendPlainText(str + " " + strData); +} diff --git a/QtQcomboBoxEx/ExQcomboBox.h b/QtQcomboBoxEx/ExQcomboBox.h new file mode 100644 index 0000000..937aadc --- /dev/null +++ b/QtQcomboBoxEx/ExQcomboBox.h @@ -0,0 +1,32 @@ +#ifndef EXQCOMBOBOX_H +#define EXQCOMBOBOX_H + +#include + +namespace Ui { +class ExQcomboBox; +} + +class ExQcomboBox : public QWidget +{ + Q_OBJECT + +public: + explicit ExQcomboBox(QWidget *parent = nullptr); + ~ExQcomboBox(); + +private slots: + void on_btnLeftInit_clicked(); + void on_btnLeftClear_clicked(); + void on_checkBoxOnlyWrite_clicked(); + void on_btnRightInit_clicked(); + void on_btnBottomAdd_clicked(); + void on_btnBottomClear_clicked(); + void on_checkBoxOnlyRead_clicked(); + void onSelectDisplay(QString str); + +private: + Ui::ExQcomboBox *ui; +}; + +#endif // EXQCOMBOBOX_H diff --git a/QtQcomboBoxEx/ExQcomboBox.ui b/QtQcomboBoxEx/ExQcomboBox.ui new file mode 100644 index 0000000..e03ade4 --- /dev/null +++ b/QtQcomboBoxEx/ExQcomboBox.ui @@ -0,0 +1,158 @@ + + + ExQcomboBox + + + + 0 + 0 + 775 + 383 + + + + ExQcomboBox + + + + + + + + 简单地ComboBox(下拉列表框): + + + + + + 初始化列表 + + + + + + + 清除列表 + + + + + + + 下拉框为可编辑 + + + + + + + + 默认的第一个item:投我以木李 + + + + :/images/github.ico:/images/github.ico + + + + + 默认的第二个item:报之以琼玖 + + + + :/images/gril.ico:/images/gril.ico + + + + + github.io:https://touwoyimuli.github.io/ + + + + + + + + + + + 复杂地ComboBox(项可存储自定义的数据内容): + + + + + + 初始化姓名+年龄 + + + + + + + + 因为有Map,所以QComboBox显示会按照key排序,而非程序代码的定义顺序 + + + + :/images/gril.ico:/images/gril.ico + + + + + + + + + + + + + QPlainTextEdit(多行可编辑的富文本的编辑器): + + + + + + + + 文本框内容添加到ComboBox + + + + + + + 清除文本内容 + + + + + + + 富文本的编辑器只可读 + + + + + + + + + + + + 显示当前的Item项: + + + + + + + + + + + + + + diff --git a/QtQcomboBoxEx/QtQcomboBoxEx.pro b/QtQcomboBoxEx/QtQcomboBoxEx.pro new file mode 100644 index 0000000..ef5898b --- /dev/null +++ b/QtQcomboBoxEx/QtQcomboBoxEx.pro @@ -0,0 +1,45 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2019-08-26T23:53:34 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = QtQcomboBoxEx +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +CONFIG += c++11 + +SOURCES += \ + main.cpp \ + ExQcomboBox.cpp + +HEADERS += \ + ExQcomboBox.h + +FORMS += \ + ExQcomboBox.ui + +RC_ICONS += images/qt.ico + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target + +RESOURCES += \ + resources.qrc diff --git a/QtQcomboBoxEx/images/github.ico b/QtQcomboBoxEx/images/github.ico new file mode 100644 index 0000000..55865aa Binary files /dev/null and b/QtQcomboBoxEx/images/github.ico differ diff --git a/QtQcomboBoxEx/images/gril.ico b/QtQcomboBoxEx/images/gril.ico new file mode 100644 index 0000000..61aabe9 Binary files /dev/null and b/QtQcomboBoxEx/images/gril.ico differ diff --git a/QtQcomboBoxEx/images/qt.ico b/QtQcomboBoxEx/images/qt.ico new file mode 100644 index 0000000..0ee0568 Binary files /dev/null and b/QtQcomboBoxEx/images/qt.ico differ diff --git a/QtQcomboBoxEx/main.cpp b/QtQcomboBoxEx/main.cpp new file mode 100644 index 0000000..fdfb5fc --- /dev/null +++ b/QtQcomboBoxEx/main.cpp @@ -0,0 +1,11 @@ +#include "ExQcomboBox.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + ExQcomboBox w; + w.show(); + + return a.exec(); +} diff --git a/QtQcomboBoxEx/resources.qrc b/QtQcomboBoxEx/resources.qrc new file mode 100644 index 0000000..f0f8cde --- /dev/null +++ b/QtQcomboBoxEx/resources.qrc @@ -0,0 +1,6 @@ + + + images/github.ico + images/gril.ico + +