feat: QComboBox and QPlainTextEdit
QComboBox(下拉列表框)和QPlainTextEdit(可同时编辑多行的富文本编辑器)的介绍和使用
This commit is contained in:
parent
054148433a
commit
4342e0091c
120
QtQcomboBoxEx/ExQcomboBox.cpp
Normal file
120
QtQcomboBoxEx/ExQcomboBox.cpp
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
#include "ExQcomboBox.h"
|
||||||
|
#include "ui_ExQcomboBox.h"
|
||||||
|
|
||||||
|
#include <QMap>
|
||||||
|
#include <QTextBlock>
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
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<QString, QString> 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);
|
||||||
|
}
|
32
QtQcomboBoxEx/ExQcomboBox.h
Normal file
32
QtQcomboBoxEx/ExQcomboBox.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#ifndef EXQCOMBOBOX_H
|
||||||
|
#define EXQCOMBOBOX_H
|
||||||
|
|
||||||
|
#include <QWidget>
|
||||||
|
|
||||||
|
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
|
158
QtQcomboBoxEx/ExQcomboBox.ui
Normal file
158
QtQcomboBoxEx/ExQcomboBox.ui
Normal file
@ -0,0 +1,158 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<ui version="4.0">
|
||||||
|
<class>ExQcomboBox</class>
|
||||||
|
<widget class="QWidget" name="ExQcomboBox">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>775</width>
|
||||||
|
<height>383</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="windowTitle">
|
||||||
|
<string>ExQcomboBox</string>
|
||||||
|
</property>
|
||||||
|
<layout class="QVBoxLayout" name="verticalLayout_2">
|
||||||
|
<item>
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||||
|
<item>
|
||||||
|
<widget class="QGroupBox" name="groupBox">
|
||||||
|
<property name="title">
|
||||||
|
<string>简单地ComboBox(下拉列表框):</string>
|
||||||
|
</property>
|
||||||
|
<layout class="QGridLayout" name="gridLayout">
|
||||||
|
<item row="0" column="0">
|
||||||
|
<widget class="QPushButton" name="btnLeftInit">
|
||||||
|
<property name="text">
|
||||||
|
<string>初始化列表</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="0" column="1">
|
||||||
|
<widget class="QPushButton" name="btnLeftClear">
|
||||||
|
<property name="text">
|
||||||
|
<string>清除列表</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="0" column="2">
|
||||||
|
<widget class="QCheckBox" name="checkBoxOnlyWrite">
|
||||||
|
<property name="text">
|
||||||
|
<string>下拉框为可编辑</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="1" column="0" colspan="3">
|
||||||
|
<widget class="QComboBox" name="comBoxLeft">
|
||||||
|
<item>
|
||||||
|
<property name="text">
|
||||||
|
<string>默认的第一个item:投我以木李</string>
|
||||||
|
</property>
|
||||||
|
<property name="icon">
|
||||||
|
<iconset resource="resources.qrc">
|
||||||
|
<normaloff>:/images/github.ico</normaloff>:/images/github.ico</iconset>
|
||||||
|
</property>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<property name="text">
|
||||||
|
<string>默认的第二个item:报之以琼玖</string>
|
||||||
|
</property>
|
||||||
|
<property name="icon">
|
||||||
|
<iconset resource="resources.qrc">
|
||||||
|
<normaloff>:/images/gril.ico</normaloff>:/images/gril.ico</iconset>
|
||||||
|
</property>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<property name="text">
|
||||||
|
<string>github.io:https://touwoyimuli.github.io/</string>
|
||||||
|
</property>
|
||||||
|
</item>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QGroupBox" name="groupBox_2">
|
||||||
|
<property name="title">
|
||||||
|
<string>复杂地ComboBox(项可存储自定义的数据内容):</string>
|
||||||
|
</property>
|
||||||
|
<layout class="QGridLayout" name="gridLayout_2">
|
||||||
|
<item row="0" column="0">
|
||||||
|
<widget class="QPushButton" name="btnRightInit">
|
||||||
|
<property name="text">
|
||||||
|
<string>初始化姓名+年龄</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="1" column="0">
|
||||||
|
<widget class="QComboBox" name="comBoxRight">
|
||||||
|
<item>
|
||||||
|
<property name="text">
|
||||||
|
<string>因为有Map,所以QComboBox显示会按照key排序,而非程序代码的定义顺序</string>
|
||||||
|
</property>
|
||||||
|
<property name="icon">
|
||||||
|
<iconset resource="resources.qrc">
|
||||||
|
<normaloff>:/images/gril.ico</normaloff>:/images/gril.ico</iconset>
|
||||||
|
</property>
|
||||||
|
</item>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QGroupBox" name="groupBox_3">
|
||||||
|
<property name="title">
|
||||||
|
<string>QPlainTextEdit(多行可编辑的富文本的编辑器):</string>
|
||||||
|
</property>
|
||||||
|
<layout class="QVBoxLayout" name="verticalLayout">
|
||||||
|
<item>
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="btnBottomAdd">
|
||||||
|
<property name="text">
|
||||||
|
<string>文本框内容添加到ComboBox</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="btnBottomClear">
|
||||||
|
<property name="text">
|
||||||
|
<string>清除文本内容</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QCheckBox" name="checkBoxOnlyRead">
|
||||||
|
<property name="text">
|
||||||
|
<string>富文本的编辑器只可读</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPlainTextEdit" name="plainTextEdit"/>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QLabel" name="labDisplay">
|
||||||
|
<property name="text">
|
||||||
|
<string>显示当前的Item项:</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
<layoutdefault spacing="6" margin="11"/>
|
||||||
|
<resources>
|
||||||
|
<include location="resources.qrc"/>
|
||||||
|
</resources>
|
||||||
|
<connections/>
|
||||||
|
</ui>
|
45
QtQcomboBoxEx/QtQcomboBoxEx.pro
Normal file
45
QtQcomboBoxEx/QtQcomboBoxEx.pro
Normal file
@ -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
|
BIN
QtQcomboBoxEx/images/github.ico
Normal file
BIN
QtQcomboBoxEx/images/github.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.2 KiB |
BIN
QtQcomboBoxEx/images/gril.ico
Normal file
BIN
QtQcomboBoxEx/images/gril.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.2 KiB |
BIN
QtQcomboBoxEx/images/qt.ico
Normal file
BIN
QtQcomboBoxEx/images/qt.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.2 KiB |
11
QtQcomboBoxEx/main.cpp
Normal file
11
QtQcomboBoxEx/main.cpp
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include "ExQcomboBox.h"
|
||||||
|
#include <QApplication>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
QApplication a(argc, argv);
|
||||||
|
ExQcomboBox w;
|
||||||
|
w.show();
|
||||||
|
|
||||||
|
return a.exec();
|
||||||
|
}
|
6
QtQcomboBoxEx/resources.qrc
Normal file
6
QtQcomboBoxEx/resources.qrc
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<RCC>
|
||||||
|
<qresource prefix="/">
|
||||||
|
<file>images/github.ico</file>
|
||||||
|
<file>images/gril.ico</file>
|
||||||
|
</qresource>
|
||||||
|
</RCC>
|
Loading…
Reference in New Issue
Block a user