1、创建Qt项目
2、设计QT界面
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void onRadioButtonClicked();
void onItemClicked(QListWidgetItem* item);
void onItemDoubleClicked(QListWidgetItem* item);
void onBtnAddClicked();
void onBtnInsertClicked();
void onBtnDeleteClicked();
private:
Ui::Widget *ui;
QButtonGroup* mBtnGroup;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include
QStringList iconStringList = {
":/images/apple.png",
":/images/banana.png",
":/images/orange.png",
":/images/peach.png",
":/images/strawberry.png",
};
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//设置显示模式
mBtnGroup = new QButtonGroup(this);
mBtnGroup->addButton(ui->rbListMode,0);
mBtnGroup->addButton(ui->rbIconMode,1);
connect(ui->rbListMode,&QRadioButton::clicked,this,&Widget::onRadioButtonClicked);
connect(ui->rbIconMode,&QRadioButton::clicked,this,&Widget::onRadioButtonClicked);
ui->rbListMode->setChecked(true);
ui->lwProvince->setViewMode(QListView::ListMode);
//单击和双击信号槽
connect(ui->lwProvince,&QListWidget::itemClicked,this,&Widget::onItemClicked);
connect(ui->lwProvince,&QListWidget::itemDoubleClicked,this,&Widget::onItemDoubleClicked);
//add/insert/delete
connect(ui->btnAdd,&QPushButton::clicked,this,&Widget::onBtnAddClicked);
connect(ui->btnInsert,&QPushButton::clicked,this,&Widget::onBtnInsertClicked);
connect(ui->btnDelete,&QPushButton::clicked,this,&Widget::onBtnDeleteClicked);
}
Widget::~Widget()
{
delete ui;
}
void Widget::onRadioButtonClicked()
{
int checkedId = mBtnGroup->checkedId();
if(checkedId == 0){
//列表模式
ui->lwProvince->setViewMode(QListView::ListMode);
}else if(checkedId == 1){
//图标模式
ui->lwProvince->setViewMode(QListView::IconMode);
}
}
//单击信号槽的时候获取文本
void Widget::onItemClicked(QListWidgetItem *item)
{
ui->leCurrentItem->setText(item->text());
}
//双击信号槽的时候变成可编辑的状态
void Widget::onItemDoubleClicked(QListWidgetItem *item)
{
item->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled);
}
//添加Item
void Widget::onBtnAddClicked()
{
int iconIndex = QRandomGenerator::global()->generate()%5;//获取0-4的随机数
QIcon icon(iconStringList[iconIndex]);
QString text = ui->leInput->text();
QListWidgetItem* item = new QListWidgetItem(icon,text);
ui->lwProvince->addItem(item);
}
//在任意位置插入Item
void Widget::onBtnInsertClicked()
{
int iconIndex = QRandomGenerator::global()->generate()%5;//获取0-4的随机数
QIcon icon(iconStringList[iconIndex]);
QString text = ui->leInput->text();
QListWidgetItem* item = new QListWidgetItem(icon,text);
int currentRow = ui->lwProvince->currentRow();
ui->lwProvince->insertItem(currentRow,item);
}
//删除Item
void Widget::onBtnDeleteClicked()
{
#if 0
QListWidgetItem* item = ui->lwProvince->currentItem();
ui->lwProvince->removeItemWidget(item);
delete item;
#else
int row = ui->lwProvince->currentRow();
QListWidgetItem *item = ui->lwProvince->takeItem(row);
delete item;
#endif
}
演示效果:
喜欢的话,点个赞赞+收藏❤