diff --git a/QtQlistWidgetEx/ExQListWidget.cpp b/QtQlistWidgetEx/ExQListWidget.cpp index b7d9fb9..7bfaa8f 100644 --- a/QtQlistWidgetEx/ExQListWidget.cpp +++ b/QtQlistWidgetEx/ExQListWidget.cpp @@ -1,14 +1,216 @@ #include "ExQListWidget.h" #include "ui_ExQListWidget.h" +#include ExQListWidget::ExQListWidget(QWidget *parent) : QMainWindow(parent), ui(new Ui::ExQListWidget) { ui->setupUi(this); + setWindowTitle(QObject::tr("QListWidget和QToolButton用法")); + + setCentralWidget(ui->splitter); //将QSplitter分割,填充满整个分割区 (分别选中左右控件,然后鼠标右键,Lay out -> Horizon ...) + setActionForToolBtn(); //左侧全部按钮 + 右侧三个按钮关联action + creatorPopMenu(); //右侧一个按钮 + 顶部ToolBar两个按钮的创建 + + ui->listWidget->setContextMenuPolicy(Qt::CustomContextMenu); //设置QListWidget支持右键菜单,这句话一定要有 + } ExQListWidget::~ExQListWidget() { delete ui; } + + +//action控件的槽函数+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +//actListInit初始化ListWidget的列表项listWidget +void ExQListWidget::on_actListInit_triggered() +{ + QListWidgetItem* item; //每一行都是一个QListWidgetItem + QIcon icon; + icon.addFile(":/images/github.png"); + bool chk = ui->checkBox->isChecked(); //是否可编辑 + + ui->listWidget->clear(); + for (int i = 0; i < 13; i++) { + item = new QListWidgetItem();//(ui->listWidget); //创建一个item + item->setText(QString("初始化 第%1个项item").arg(i)); //设置文字 + item->setIcon(icon); //设置icon图标 + item->setCheckState(Qt::Unchecked); //设置选中方式 + + if (chk) //可编辑 + item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); + else + item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); + + ui->listWidget->addItem(item); //添加项item到listWidget里面 + } +} + +//actInsert在ListWidget的列表项中插入一个项item +void ExQListWidget::on_actInsert_triggered() +{ + QListWidgetItem* item; //每一行都是一个QListWidgetItem + QIcon icon; + icon.addFile(":/images/gril.png"); + bool chk = ui->checkBox->isChecked(); //是否可编辑 + + item = new QListWidgetItem(); //创建一个item + item->setText(QString("插入一个项item: " + QDateTime::currentDateTime().toString("yyyy-mm-dd hh:MM:ss:zzz"))); //设置文字 + item->setIcon(icon); //设置icon图标 + item->setCheckState(Qt::Unchecked); //设置选中方式 + + if (chk) //可编辑 + item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); + else + item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); + + ui->listWidget->insertItem(ui->listWidget->currentRow(), item); //添加项item到listWidget里面 +} + +//actClear清空ListWidget的所有列表项 +void ExQListWidget::on_actClear_triggered() +{ + ui->listWidget->clear(); +} + +//actDel删除一个指定的ListWidget的列表项 +void ExQListWidget::on_actDel_triggered() +{ + int row = ui->listWidget->currentRow(); + QListWidgetItem* item = ui->listWidget->takeItem(row); //只是移除为row的item(并不删除对象) + delete item; //手动删除对象 +} + +//actAdd添加一个指定的ListWidget的列表项 +void ExQListWidget::on_actAdd_triggered() +{ + QListWidgetItem* item; //每一行都是一个QListWidgetItem + QIcon icon; + icon.addFile(":/images/TREE.png"); + bool chk = ui->checkBox->isChecked(); //是否可编辑 + + item = new QListWidgetItem(); //创建一个item + item->setText(QString("添加一个项item: " + QDateTime::currentDateTime().toString("yyyy-mm-dd hh:MM:ss:zzz"))); //设置文字 + item->setIcon(icon); //设置icon图标 + item->setCheckState(Qt::Unchecked); //设置选中方式 + + if (chk) //可编辑 + item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsUserTristate | Qt::ItemIsEnabled); + else + item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserTristate | Qt::ItemIsEnabled); + + ui->listWidget->addItem(item); //添加项item到listWidget里面 +} + +//设置全选 +void ExQListWidget::on_actSelAll_triggered() +{ + int nCount = ui->listWidget->count(); + for (int i = 0; i < nCount; i++) { + QListWidgetItem* item = ui->listWidget->item(i); + item->setCheckState(Qt::Checked); + } +} + +//设置全不选 +void ExQListWidget::on_actSelNone_triggered() +{ + int nCount = ui->listWidget->count(); + for (int i = 0; i < nCount; i++) { + QListWidgetItem* item = ui->listWidget->item(i); + item->setCheckState(Qt::Unchecked); + } +} + +//设置反选 +void ExQListWidget::on_actSelInvs_triggered() +{ + int nCount = ui->listWidget->count(); + for (int i = 0; i < nCount; i++) { + QListWidgetItem* item = ui->listWidget->item(i); + + if (item->checkState() == Qt::Checked) + item->setCheckState(Qt::Unchecked); + else + item->setCheckState(Qt::Checked); + } +} + +//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +//设置QListWidget的当前item发生变化,触发的信号,会在右侧显示出来 +void ExQListWidget::on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous) +{ + if (current != nullptr) { + if (previous == nullptr) + ui->lineEditRight->setText(QString("当前项:%1; 前一项%2").arg(current->text()).arg("不存在")); + else + ui->lineEditRight->setText(QString("当前项:%1; 前一项%2").arg(current->text()).arg(previous->text())); + } +} + +//左侧五个按钮, 右侧右边三个按钮 进行QToolButton和action 关联 +void ExQListWidget::setActionForToolBtn() +{ + //左侧,第一页的五个按钮 + ui->toolBtnInit->setDefaultAction(ui->actListInit); + ui->toolBtnClear->setDefaultAction(ui->actClear); + ui->toolBtnInsert->setDefaultAction(ui->actInsert); + ui->toolBtnAdd->setDefaultAction(ui->actAdd); + ui->toolBtnDelete->setDefaultAction(ui->actDel); + + //右侧 右边的三个按钮 + ui->toolBtnSelAll->setDefaultAction(ui->actSelAll); + ui->toolBtnSelNone->setDefaultAction(ui->actSelInvs); + ui->toolBtnSelInvs->setDefaultAction(ui->actSelNone); +} + + +//ToolBar 添加项选择(带下拉框) 和退出;右侧一个按钮也添加一个项的选择 +void ExQListWidget::creatorPopMenu() +{ + //创建菜单 + QMenu* menu = new QMenu(this); //创建弹出式菜单 + menu->addAction(ui->actSelAll); + menu->addAction(ui->actSelInvs); + menu->addAction(ui->actSelNone); + + //右侧ListWidget, 其上方的toolBtn按钮 + //设置toolBtnSelectItem的多个属性:PopupMode、ToolButtonStyle等(在Design已经设置) + ui->toolBtnSelectItem->setDefaultAction(ui->actSelPopMenu); //关联action + ui->toolBtnSelectItem->setMenu(menu); //设置下拉菜单menu + + //工具栏QToolBar上面的下拉菜单样式按钮 + QToolButton* toolBtn = new QToolButton(this); + toolBtn->setPopupMode(QToolButton::InstantPopup); //下拉式菜单样式属性 + toolBtn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); //设置汉字出现在icon下面 + toolBtn->setDefaultAction(ui->actSelPopMenu); //关联action + toolBtn->setMenu(menu); //关联菜单 + ui->toolBar->addSeparator(); //添加隔栏 + ui->toolBar->addWidget(toolBtn); //添加到工具栏 + + //添加退出按钮 + ui->toolBar->addSeparator(); + ui->toolBar->addAction(ui->actExit); //添加退出按钮 +} + + +//鼠标右键自定义快捷菜单 +void ExQListWidget::on_listWidget_customContextMenuRequested(const QPoint &pos) +{ + qDebug()<<"11111111"; + Q_UNUSED(pos) + QMenu* menu = new QMenu(this); //创建菜单 + menu->addAction(ui->actListInit); + menu->addAction(ui->actAdd); + menu->addAction(ui->actDel); + menu->addAction(ui->actClear); + menu->addAction(ui->actInsert); + menu->addSeparator(); + menu->addAction(ui->actSelAll); + menu->addAction(ui->actSelNone); + menu->addAction(ui->actSelInvs); + menu->exec(QCursor::pos()); //在鼠标光标位置显示右键快捷菜单 + delete menu; //手工创建的指针必须手工删除 +} diff --git a/QtQlistWidgetEx/ExQListWidget.h b/QtQlistWidgetEx/ExQListWidget.h index 30c5f5b..7e9a7ab 100644 --- a/QtQlistWidgetEx/ExQListWidget.h +++ b/QtQlistWidgetEx/ExQListWidget.h @@ -2,6 +2,8 @@ #define EXQLISTWIDGET_H #include +#include +#include namespace Ui { class ExQListWidget; @@ -15,6 +17,22 @@ public: explicit ExQListWidget(QWidget *parent = nullptr); ~ExQListWidget(); +private slots: + void on_actListInit_triggered(); //初始化ListWidget + void on_actInsert_triggered(); //插入item + void on_actAdd_triggered(); //添加item + void on_actClear_triggered(); //清除所有item + void on_actDel_triggered(); //删除item + void on_actSelAll_triggered(); //选中所有item + void on_actSelNone_triggered(); //全部不选所有的item + void on_actSelInvs_triggered(); //反选已筛选的item + void on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous); //显示当前item的变动 + void on_listWidget_customContextMenuRequested(const QPoint &pos); //ListWidget弹出鼠标右键菜单 + +private: + void setActionForToolBtn(); //创建cation和toolButton的关联 + void creatorPopMenu(); //创建两个单独的弹出式菜单的ToolButton + private: Ui::ExQListWidget *ui; }; diff --git a/QtQlistWidgetEx/ExQListWidget.ui b/QtQlistWidgetEx/ExQListWidget.ui index 25d4157..a66e542 100644 --- a/QtQlistWidgetEx/ExQListWidget.ui +++ b/QtQlistWidgetEx/ExQListWidget.ui @@ -1,24 +1,584 @@ + ExQListWidget - - + + 0 0 - 400 - 300 + 730 + 436 - + ExQListWidget - - - - + + + + + 0 + 0 + 731 + 341 + + + + Qt::Horizontal + + + + + 140 + 0 + + + + + 180 + 16777215 + + + + 0 + + + + + 0 + 0 + 180 + 251 + + + + + :/images/List.png:/images/List.png + + + QListWidget展示 + + + + + + + 120 + 25 + + + + toolBtnInit + + + Qt::ToolButtonTextBesideIcon + + + + + + + + 120 + 25 + + + + toolBtnClear + + + Qt::ToolButtonTextBesideIcon + + + + + + + + 120 + 25 + + + + toolBtnInsert + + + Qt::ToolButtonTextBesideIcon + + + + + + + + 120 + 25 + + + + toolBtnAdd + + + Qt::ToolButtonTextBesideIcon + + + + + + + + 120 + 25 + + + + toolBtnDelete + + + Qt::ToolButtonTextUnderIcon + + + + + + + + + 0 + 0 + 180 + 251 + + + + SizeVerCursor + + + + :/images/TREE.png:/images/TREE.png + + + QTreeWidget展示 + + + + + + GroupBox + + + + + + 此处没有实现任何功能 + + + + + + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + :/images/Table.png:/images/Table.png + + + QTableWidget展示 + + + + + 10 + 60 + 161 + 91 + + + + 此处连布局都没有 + + + + + + + + 320 + 0 + + + + 0 + + + + + :/images/List.png:/images/List.png + + + QListWidget + + + + + + + + + 0 + 25 + + + + tBtnSelectItem + + + QToolButton::MenuButtonPopup + + + Qt::ToolButtonTextBesideIcon + + + + + + + + 0 + 25 + + + + tBtnSelAll + + + + + + + + 0 + 25 + + + + tBtnSelNone + + + + + + + + 0 + 25 + + + + tBtnSelInvs + + + + + + + + + + + 当前项的变化: + + + + + + + + + + 可编辑 + + + + + + + + + + this is first item + + + Unchecked + + + + :/images/github.png:/images/github.png + + + ItemIsSelectable|ItemIsEditable|ItemIsDragEnabled|ItemIsUserCheckable|ItemIsEnabled + + + + + 这是第二个项item + + + Unchecked + + + + :/images/gril.png:/images/gril.png + + + + + + + + + + :/images/TREE.png:/images/TREE.png + + + QTreeWidget + + + + + + 这里什么都没有,只有一个布局,此页面也没有实现任何功能 + + + + + + + + + :/images/Table.png:/images/Table.png + + + QTableWidget + + + + + 110 + 120 + 331 + 101 + + + + 这里什么都没有,连布局都没有 + + + + + + + + + + 0 + 0 + 730 + 23 + + + + + + + toolBar + + + Qt::ToolButtonTextUnderIcon + + + TopToolBarArea + + + false + + + + + + + + + + + :/images/init.png:/images/init.png + + + 初始化 + + + 初始化列表 + + + Ctrl+I + + + + + + :/images/clear.png:/images/clear.png + + + 清除 + + + 清除列表 + + + Alt+C + + + + + + :/images/insert.png:/images/insert.png + + + 插入 + + + 插入项 + + + Alt+I + + + + + + :/images/add.png:/images/add.png + + + 添加 + + + 添加项 + + + Alt+A + + + + + + :/images/delete.png:/images/delete.png + + + 删除 + + + 删除当前项目 + + + Ctrl+D + + + + + 全选 + + + 选择全部 + + + Ctrl+A + + + + + 全不选 + + + 全部不选中 + + + + + 反选 + + + 选择相反的部分 + + + + + + :/images/menu.png:/images/menu.png + + + 选项 + + + 多种选择方式 + + + + + + :/images/exit.png:/images/exit.png + + + 退出 + + + 退出程序 + + + Ctrl+0 + + - - - - + + + + + + + actSelPopMenu + triggered() + actSelInvs + trigger() + + + -1 + -1 + + + 363 + 212 + + + + diff --git a/QtQlistWidgetEx/Examples.cpp b/QtQlistWidgetEx/Examples.cpp deleted file mode 100644 index ca01efa..0000000 --- a/QtQlistWidgetEx/Examples.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "Examples.h" -#include "ui_Examples.h" - -Examples::Examples(QWidget *parent) : - QWidget(parent), - ui(new Ui::Examples) -{ - ui->setupUi(this); -} - -Examples::~Examples() -{ - delete ui; -} diff --git a/QtQlistWidgetEx/Examples.h b/QtQlistWidgetEx/Examples.h deleted file mode 100644 index 8fe4eaa..0000000 --- a/QtQlistWidgetEx/Examples.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef EXAMPLES_H -#define EXAMPLES_H - -#include - -namespace Ui { -class Examples; -} - -class Examples : public QWidget -{ - Q_OBJECT - -public: - explicit Examples(QWidget *parent = nullptr); - ~Examples(); - -private: - Ui::Examples *ui; -}; - -#endif // EXAMPLES_H diff --git a/QtQlistWidgetEx/Examples.ui b/QtQlistWidgetEx/Examples.ui deleted file mode 100644 index b6774ff..0000000 --- a/QtQlistWidgetEx/Examples.ui +++ /dev/null @@ -1,20 +0,0 @@ - - Examples - - - - 0 - 0 - 400 - 300 - - - - Examples - - - - - - - diff --git a/QtQlistWidgetEx/QtQlistWidgetEx.pro b/QtQlistWidgetEx/QtQlistWidgetEx.pro index 40a4374..a72dbd6 100644 --- a/QtQlistWidgetEx/QtQlistWidgetEx.pro +++ b/QtQlistWidgetEx/QtQlistWidgetEx.pro @@ -1,6 +1,6 @@ #------------------------------------------------- # -# Project created by QtCreator 2019-08-30T19:36:43 +# Project created by QtCreator 2019-08-30T19:39:47 # #------------------------------------------------- @@ -8,7 +8,7 @@ QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets -TARGET = QtQlistWidgetEx +TARGET = QtQListWidgetEx TEMPLATE = app # The following define makes your compiler emit warnings if you use @@ -26,15 +26,21 @@ CONFIG += c++11 SOURCES += \ main.cpp \ - Examples.cpp + ExQListWidget.cpp HEADERS += \ - Examples.h + ExQListWidget.h FORMS += \ - Examples.ui + ExQListWidget.ui + +RC_ICONS += 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/QtQlistWidgetEx/images/exit.png b/QtQlistWidgetEx/images/exit.png new file mode 100644 index 0000000..a013a84 Binary files /dev/null and b/QtQlistWidgetEx/images/exit.png differ diff --git a/QtQlistWidgetEx/images/github.png b/QtQlistWidgetEx/images/github.png new file mode 100644 index 0000000..d4e95fa Binary files /dev/null and b/QtQlistWidgetEx/images/github.png differ diff --git a/QtQlistWidgetEx/images/gril.png b/QtQlistWidgetEx/images/gril.png new file mode 100644 index 0000000..45e0c07 Binary files /dev/null and b/QtQlistWidgetEx/images/gril.png differ diff --git a/QtQlistWidgetEx/images/menu.png b/QtQlistWidgetEx/images/menu.png new file mode 100644 index 0000000..1e0cc95 Binary files /dev/null and b/QtQlistWidgetEx/images/menu.png differ diff --git a/QtQlistWidgetEx/main.cpp b/QtQlistWidgetEx/main.cpp index 5c200aa..e2aadf7 100644 --- a/QtQlistWidgetEx/main.cpp +++ b/QtQlistWidgetEx/main.cpp @@ -1,10 +1,10 @@ -#include "Examples.h" +#include "ExQListWidget.h" #include int main(int argc, char *argv[]) { QApplication a(argc, argv); - Examples w; + ExQListWidget w; w.show(); return a.exec(); diff --git a/QtQlistWidgetEx/qt.ico b/QtQlistWidgetEx/qt.ico new file mode 100644 index 0000000..0ee0568 Binary files /dev/null and b/QtQlistWidgetEx/qt.ico differ diff --git a/QtQlistWidgetEx/resources.qrc b/QtQlistWidgetEx/resources.qrc index 90f4a83..2ea74b3 100644 --- a/QtQlistWidgetEx/resources.qrc +++ b/QtQlistWidgetEx/resources.qrc @@ -1,2 +1,16 @@ - - + + + images/add.png + images/clear.png + images/delete.png + images/init.png + images/insert.png + images/List.png + images/Table.png + images/TREE.png + images/github.png + images/gril.png + images/exit.png + images/menu.png + +