QListWidget的初始化、清空、Append添加、Insert添加、删除item
QListWidget的事件的使用
QToolBox的使用,每个Page可以添加其他控件
QToolBar使用代码添加QMenu,QMenu添加3个Action
QToolButton绑定Action
布局
其中 QSplitter比较特殊, 允许在水平或垂直方向上分割窗口中的多个子部件或控件。
添加方法,选中ToolBox和groupbox两个控件后, 右键布局→在水平方向上分割
.h
#ifndef LISTWIDGETEXAMPLE_H
#define LISTWIDGETEXAMPLE_H
#include <QMainWindow>
#include <QListWidgetItem>
namespace Ui {
class ListWidgetExample;
}
class ListWidgetExample : public QMainWindow
{
Q_OBJECT
private:
void setActionsForButton();//为工具button关联到action
void createSelectionMenu();//创建下拉菜单
void setItem(QListWidgetItem *item, bool editEnable);//设置一个item
QIcon getIcon();
public:
explicit ListWidgetExample(QWidget *parent = nullptr);
~ListWidgetExample();
private slots:
void on_actListClear_triggered();
void on_actListIni_triggered();
void on_chkBoxListEditable_clicked(bool checked);
void on_actListAppend_triggered();
void on_actListDelete_triggered();
void on_actListInsert_triggered();
void on_actSelectAll_triggered();
void on_actSelectNone_triggered();
void on_actSelectInvert_triggered();
void on_btnAsc_clicked();
void on_btnDec_clicked();
void on_chkAllowSort_clicked(bool checked);
void on_listWidget_currentRowChanged(int currentRow);
void on_listWidget_currentTextChanged(const QString ¤tText);
void on_listWidget_itemChanged(QListWidgetItem *item);
void on_listWidget_itemEntered(QListWidgetItem *item);
void on_listWidget_itemPressed(QListWidgetItem *item);
void on_listWidget_itemDoubleClicked(QListWidgetItem *item);
void on_btnClearText_clicked();
void on_btnAddEmptyRow_clicked();
void on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous);
void on_listWidget_customContextMenuRequested(const QPoint &pos);
private:
Ui::ListWidgetExample *ui;
};
#endif // LISTWIDGETEXAMPLE_H
.cpp
#include "listwidgetexample.h"
#include "ui_listwidgetexample.h"
//将 toolButton 关联到 相应的action
void ListWidgetExample::setActionsForButton()
{
ui->tBtnListIni->setDefaultAction(ui->actListIni);
ui->tBtnListClear->setDefaultAction(ui->actListClear);
ui->tBtnListAppend->setDefaultAction(ui->actListAppend);
ui->tBtnListInsert->setDefaultAction(ui->actListInsert);
ui->tBtnListDelete->setDefaultAction(ui->actListDelete);
ui->tBtnSelAll->setDefaultAction(ui->actSelectAll);
ui->tBtnSelNone->setDefaultAction(ui->actSelectNone);
ui->tBtnSelnvert->setDefaultAction(ui->actSelectInvert);
}
//在toolBar中创建一个下拉菜单
void ListWidgetExample::createSelectionMenu()
{
//创建下拉菜单 加入全选、空选、反选
QMenu *menuSelection = new QMenu(this);
menuSelection->addAction(ui->actSelectAll);
menuSelection->addAction(ui->actSelectNone);
menuSelection->addAction(ui->actSelectInvert);
//在listWidget的tBtnSelection 关联menu 关联action
ui->tBtnSelectItem->setPopupMode(QToolButton::MenuButtonPopup);
// ui->tBtnSelectItem->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
ui->tBtnSelectItem->setDefaultAction(ui->actSelPopMenu);
ui->tBtnSelectItem->setMenu(menuSelection);
//工具栏 toolBar新建一个ToolBatton 然后关联menu 关联action
QToolButton *btn = new QToolButton(this);
btn->setPopupMode(QToolButton::InstantPopup);
btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
btn->setMenu(menuSelection);
btn->setText("选择");
QIcon icon;
icon.addFile("://images/icon.bmp");
btn->setIcon(icon);
ui->toolBar->addWidget(btn);
//最后在工具栏toolBar增加一个分隔符,然后加入退出
ui->toolBar->addSeparator();
ui->toolBar->addAction(ui->actQuit);
}
ListWidgetExample::ListWidgetExample(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::ListWidgetExample)
{
ui->setupUi(this);
setCentralWidget(ui->splitter); //设置中心组件
ui->listWidget->setContextMenuPolicy(Qt::CustomContextMenu);//不设置这句,无法弹出自定义菜单
this->setActionsForButton();
this->createSelectionMenu();
}
ListWidgetExample::~ListWidgetExample()
{
delete ui;
}
void ListWidgetExample::on_actListClear_triggered()
{
ui->listWidget->clear();
}
//设置item的flag属性
void ListWidgetExample::setItem(QListWidgetItem *item, bool editEnable)
{
if(editEnable)
item->setFlags(Qt::ItemIsSelectable
|Qt::ItemIsEnabled
|Qt::ItemIsUserCheckable
|Qt::ItemIsEditable);
else
item->setFlags(Qt::ItemIsSelectable
|Qt::ItemIsEnabled
|Qt::ItemIsUserCheckable);
}
//初始化列表数据
void ListWidgetExample::on_actListIni_triggered()
{
ui->listWidget->clear();
bool chk= ui->chkBoxListEditable->isChecked();
QIcon icon;
icon.addFile(":/toolbar/toolbarimages/724.bmp");
QListWidgetItem *item ;
for(int i=0;i<10;i++)
{
QString str=QString("Item %1").arg(i);
item= new QListWidgetItem();
item->setText(str);
item->setIcon(icon);
item->setCheckState(Qt::Checked);
this->setItem(item,chk);
ui->listWidget->addItem(item);
}
}
//点击勾选框,遍历listWidget里面的item,设置 是否可编辑。
void ListWidgetExample::on_chkBoxListEditable_clicked(bool checked)
{
int count = ui->listWidget->count();
QListWidgetItem *item;
for(int i=0;i<count;i++)
{
item = ui->listWidget->item(i);
this->setItem(item,checked);
}
}
QIcon ListWidgetExample::getIcon()
{
QIcon icon;
icon.addFile(":/toolbar/toolbarimages/724.bmp");
return icon;
}
void ListWidgetExample::on_actListAppend_triggered()
{
QListWidgetItem *item = new QListWidgetItem("Added Item");
item->setIcon(this->getIcon());
item->setCheckState(Qt::Checked);
this->setItem(item,ui->chkBoxListEditable->isChecked());
ui->listWidget->addItem(item);
}
void ListWidgetExample::on_actListInsert_triggered()
{
QListWidgetItem *item = new QListWidgetItem("Inserted Item");
item->setIcon(this->getIcon());
item->setCheckState(Qt::Checked);
this->setItem(item,ui->chkBoxListEditable->isChecked());
ui->listWidget->insertItem(ui->listWidget->currentRow(),item);
}
void ListWidgetExample::on_actListDelete_triggered()
{
int row = ui->listWidget->currentRow();
QListWidgetItem *item = ui->listWidget->takeItem(row);
delete item;//需要手工删除对象
}
void ListWidgetExample::on_actSelectAll_triggered()
{
for(int i=0;i<ui->listWidget->count();i++)
{
QListWidgetItem *item = ui->listWidget->item(i);
item->setCheckState(Qt::Checked);//设置为选中
}
}
void ListWidgetExample::on_actSelectNone_triggered()
{
for(int i=0;i<ui->listWidget->count();i++)
{
QListWidgetItem *item = ui->listWidget->item(i);
item->setCheckState(Qt::Unchecked);//设置为不选中
}
}
//反选
void ListWidgetExample::on_actSelectInvert_triggered()
{
for(int i=0;i<ui->listWidget->count();i++)
{
QListWidgetItem *item = ui->listWidget->item(i);
if(item->checkState()!= Qt::Checked)
item->setCheckState(Qt::Checked);
else
item->setCheckState(Qt::Unchecked);//设置为不选中
}
}
void ListWidgetExample::on_btnAsc_clicked()
{
ui->listWidget->sortItems(Qt::AscendingOrder);
}
void ListWidgetExample::on_btnDec_clicked()
{
ui->listWidget->sortItems(Qt::DescendingOrder);
}
void ListWidgetExample::on_chkAllowSort_clicked(bool checked)
{
ui->listWidget->setSortingEnabled(checked);
}
void ListWidgetExample::on_listWidget_currentRowChanged(int currentRow)
{
ui->plainTextEdit->appendPlainText(
QString("currentRowChanged,currentRow=%1").arg(currentRow)
);
}
void ListWidgetExample::on_listWidget_currentTextChanged(const QString ¤tText)
{
ui->plainTextEdit->appendPlainText(
QString("currentTextChanged,currentText=%1").arg(currentText)
);
}
void ListWidgetExample::on_listWidget_itemChanged(QListWidgetItem *item)
{
ui->plainTextEdit->appendPlainText(
QString("itemChanged,itemText=%1").arg(item->text())
);
}
void ListWidgetExample::on_listWidget_itemEntered(QListWidgetItem *item)
{
ui->plainTextEdit->appendPlainText(
QString("itemEntered,itemText=%1").arg(item->text())
);
}
void ListWidgetExample::on_listWidget_itemPressed(QListWidgetItem *item)
{
ui->plainTextEdit->appendPlainText(
QString("itemPressed,itemText=%1").arg(item->text())
);
}
void ListWidgetExample::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
ui->plainTextEdit->appendPlainText(
QString("itemDoubleClicked,itemText=%1").arg(item->text())
);
}
void ListWidgetExample::on_btnClearText_clicked()
{
ui->plainTextEdit->clear();
}
void ListWidgetExample::on_btnAddEmptyRow_clicked()
{
ui->plainTextEdit->appendPlainText("");
}
void ListWidgetExample::on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
QString str;
if(current!=NULL)
{
if(previous!=NULL)
{
str=QString("前项:%1 当前:%2").arg(previous->text()).arg(current->text());
}
else
str=QString("当前:%2").arg(current->text());
ui->editCutItemText->setText(str);
}
ui->plainTextEdit->appendPlainText(QString("currentItemChanged触发"));
}
//自定义右键
void ListWidgetExample::on_listWidget_customContextMenuRequested(const QPoint &pos)
{
ui->plainTextEdit->appendPlainText("customContextMenuRequested---开始");
Q_UNUSED(pos);
QMenu *menu= new QMenu(this);
menu->addAction(ui->actListIni);
menu->addAction(ui->actListClear);
menu->addAction(ui->actListAppend);
menu->addAction(ui->actListDelete);
menu->addAction(ui->actListInsert);
menu->addSeparator();
menu->addAction(ui->actSelectAll);
menu->addAction(ui->actSelectNone);
menu->addAction(ui->actSelectInvert);
menu->exec(QCursor::pos());//在光标处显示快捷菜单
delete menu; //菜单显示完,需要删除对象
ui->plainTextEdit->appendPlainText("customContextMenuRequested---结束");
}
效果
疑问
setSortingEnabled之后,仍然可以ui->listWidget->sortItems(Qt::AscendingOrder);
后面再看什么问题