From 4342e0091c2c0ae0c29e29ac37f5a16146e3cb56 Mon Sep 17 00:00:00 2001 From: touwoyimuli Date: Wed, 28 Aug 2019 19:28:01 +0800 Subject: [PATCH] feat: QComboBox and QPlainTextEdit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QComboBox(下拉列表框)和QPlainTextEdit(可同时编辑多行的富文本编辑器)的介绍和使用 --- QtQcomboBoxEx/ExQcomboBox.cpp | 120 ++++++++++++++++++++++++ QtQcomboBoxEx/ExQcomboBox.h | 32 +++++++ QtQcomboBoxEx/ExQcomboBox.ui | 158 ++++++++++++++++++++++++++++++++ QtQcomboBoxEx/QtQcomboBoxEx.pro | 45 +++++++++ QtQcomboBoxEx/images/github.ico | Bin 0 -> 4286 bytes QtQcomboBoxEx/images/gril.ico | Bin 0 -> 4286 bytes QtQcomboBoxEx/images/qt.ico | Bin 0 -> 4286 bytes QtQcomboBoxEx/main.cpp | 11 +++ QtQcomboBoxEx/resources.qrc | 6 ++ 9 files changed, 372 insertions(+) create mode 100644 QtQcomboBoxEx/ExQcomboBox.cpp create mode 100644 QtQcomboBoxEx/ExQcomboBox.h create mode 100644 QtQcomboBoxEx/ExQcomboBox.ui create mode 100644 QtQcomboBoxEx/QtQcomboBoxEx.pro create mode 100644 QtQcomboBoxEx/images/github.ico create mode 100644 QtQcomboBoxEx/images/gril.ico create mode 100644 QtQcomboBoxEx/images/qt.ico create mode 100644 QtQcomboBoxEx/main.cpp create mode 100644 QtQcomboBoxEx/resources.qrc 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 0000000000000000000000000000000000000000..55865aae5b0155aaab9dfd3c60e65f3a42a1b040 GIT binary patch literal 4286 zcmcInv69m;5LL#73yRAzXIr63N-BmwxT~q?Dc}zXJu~sqP?A63O1kg|aYuRl!Fh%c zIF}SRXZA(jSdmu}W=t~5TC3fCyQ|g8QEEot)k@)4eVZ$lE2W;(AeOqK0si4bYfqn; zVHkh+dvT$!@?75(#iGr!>}5!cWY#3x5Z%t_dKHq5twFyibOl@<@Fk>2*d@z$l>C@e zz%w`60F5wj!2fAk+Q**Ma(jSbwfdlJDEa-} zkw50Y=j+;^7l2?;Xm68QQ#Q8K4%6~*Q`gj?qQQ<+2c@u4kDB9_0>4`1Umjbn5G9I4mY=$ zC!b2jk|^@>zkJBD#TN-v;z4$bc-e+s z4-%flGw(*e_l99FW2{pe|IZ|0+dT69t^fT+_Jj8DRywD)-LcPj7p3>|_oi3qJa5SE9+#T90*xJlueq@<)w=ae^K&+rED2Ji;>4Jb&smXhxL2A4O0H;CWh zT&Bpd?59LwWlPErpMO8NIn`jr`m(r<-)eu^HC0n zV?(@MY&$pAzPZ3YuI-C2W?m~ze9!}SFZ*z~FKFuXiTQ2uwJrNHT(=oJ@#OUUP*5gC zo_XkU4f>e(NdA&`s8`w%a%AhH&)fo+@n`Zw1}`jNV>n}@sXH(e*$Bdp83hUy3mi79nH z=ND_9HjkOK^PSO+Hs}Gykakk{)nE4C>YQ0MHJmM9p8GRYf9yBD=qB5darEV8dnL*}J*>AE%q9rPmI-bUL(!_N- rjO$)6u7_z{pQdp$Op~V9OPX|;%#)@+kL&(IsSLhIsip5Gk%;sLXp+s5 literal 0 HcmV?d00001 diff --git a/QtQcomboBoxEx/images/qt.ico b/QtQcomboBoxEx/images/qt.ico new file mode 100644 index 0000000000000000000000000000000000000000..0ee0568945377c30d5d973d89afcc31770cd8818 GIT binary patch literal 4286 zcmeHKdr(y86+io87ZyYwA_hySrjx{)Njo~7w3D=Go0O&*)2xbtP|_lTj{v^F6l<|4 z!TRE16coi*F_>DPiHV~$YSL&Lgk=|&m4~p)V}S)!47%0a{(A22URjA+|FAR3OzzI_ zp6@&7JHKBO?Lu5#%-ZBiRg$@WRi6TzUI$u#k!Uz4@+6%sBmDt|-5nU_e| z7fHeg@p7SVjknOZZ=`Vjy7MivgQDE}G#O0Oe_VLG8&NBA&`P!TV~z&n1pb#e?G?}cIT z&=`F2_fRJW4~F8#u}5(8_!tb9kHklFz0kAS7lZplFi;kZfgNhJq$t6h5IOG!{VQ~X z2S8Ikg_Bh8jEmPp%hXZkq`TBPS!M-yb_T3X;-}6Hkc0Gxn(s1;a2I2DF zQMg(b%I!v@XIn5j-}YnqC>qnhiyATZjC(_j`NC+@{_Pi}tBIyBD_$a)m&h4GCPW}OX$I{VzbSW->umrt_;?Pqz6}FObux$xM$Ce*qcgVDYBr!b^G2IH$T7-M9pWr$KL5339F zo}z&LQ50A4GT};|0`{%`=qwIL&%P)$WlzGtbj%TVcYHow2J@TWhi&JxXnQ-1;|HOu z1eh#6pNW=5zkM_;sXkHPDN*+Jc>vGiH2i4rS`h|XI zD+obbzCXG)`NFt>P!)G?KZ(=yJ2?s?X>55E&9|)}bZ^!&#|T*eqGf*>Ota;8pe{jy zx`aD*Vw($`gJ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eD}mJH?ThaFqwtIZ;> z_H5PDEm^7`+z-vm$p8Nl|69&)<_KWevy%75gMfQ0?xEpe)zGt&?Wg%a9&lRi!mwam F{Vzx7jH3Vm literal 0 HcmV?d00001 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 + +