qt 使用 带有复选框的qlistwidget ,在外部通过按钮实现全选,全不选,并获取qlistwidget已选择的项,通过C++实现
时间: 2023-07-16 18:16:41 浏览: 197
要实现带有复选框的 QListWidget,并且可以通过按钮实现全选和全不选,可以按照以下步骤进行操作:
1. 创建 QListWidget 和 QPushButton 控件,并将它们添加到 QWidget 中。
```c++
QListWidget *listWidget = new QListWidget(this);
QPushButton *selectAllBtn = new QPushButton("Select All", this);
QPushButton *deselectAllBtn = new QPushButton("Deselect All", this);
```
2. 为 QListWidget 添加项,每个项都包含一个复选框和一些文本。
```c++
QStringList items = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
for (const auto &item : items) {
QListWidgetItem *listItem = new QListWidgetItem(listWidget);
QCheckBox *checkBox = new QCheckBox();
checkBox->setText(item);
listItem->setSizeHint(checkBox->sizeHint());
listWidget->setItemWidget(listItem, checkBox);
}
```
3. 为 QPushButton 添加信号槽,分别实现全选和全不选功能。在槽函数中遍历 QListWidget 中的所有项,并将它们的复选框设置为选中或未选中状态。
```c++
connect(selectAllBtn, &QPushButton::clicked, [=]() {
for (int i = 0; i < listWidget->count(); ++i) {
QCheckBox *checkBox = qobject_cast<QCheckBox *>(listWidget->itemWidget(listWidget->item(i)));
checkBox->setChecked(true);
}
});
connect(deselectAllBtn, &QPushButton::clicked, [=]() {
for (int i = 0; i < listWidget->count(); ++i) {
QCheckBox *checkBox = qobject_cast<QCheckBox *>(listWidget->itemWidget(listWidget->item(i)));
checkBox->setChecked(false);
}
});
```
4. 实现获取 QListWidget 中已选项的功能。遍历 QListWidget 中的所有项,如果一个项的复选框被选中,则将其文本添加到一个 QStringList 中。
```c++
QStringList selectedItems;
for (int i = 0; i < listWidget->count(); ++i) {
QCheckBox *checkBox = qobject_cast<QCheckBox *>(listWidget->itemWidget(listWidget->item(i)));
if (checkBox->isChecked()) {
selectedItems << checkBox->text();
}
}
```
完整的代码示例如下:
```c++
#include <QApplication>
#include <QListWidget>
#include <QCheckBox>
#include <QPushButton>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QWidget *widget = new QWidget;
QListWidget *listWidget = new QListWidget(widget);
QPushButton *selectAllBtn = new QPushButton("Select All", widget);
QPushButton *deselectAllBtn = new QPushButton("Deselect All", widget);
QStringList items = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
for (const auto &item : items) {
QListWidgetItem *listItem = new QListWidgetItem(listWidget);
QCheckBox *checkBox = new QCheckBox();
checkBox->setText(item);
listItem->setSizeHint(checkBox->sizeHint());
listWidget->setItemWidget(listItem, checkBox);
}
connect(selectAllBtn, &QPushButton::clicked, [=]() {
for (int i = 0; i < listWidget->count(); ++i) {
QCheckBox *checkBox = qobject_cast<QCheckBox *>(listWidget->itemWidget(listWidget->item(i)));
checkBox->setChecked(true);
}
});
connect(deselectAllBtn, &QPushButton::clicked, [=]() {
for (int i = 0; i < listWidget->count(); ++i) {
QCheckBox *checkBox = qobject_cast<QCheckBox *>(listWidget->itemWidget(listWidget->item(i)));
checkBox->setChecked(false);
}
});
QPushButton *getSelectedBtn = new QPushButton("Get Selected", widget);
connect(getSelectedBtn, &QPushButton::clicked, [=]() {
QStringList selectedItems;
for (int i = 0; i < listWidget->count(); ++i) {
QCheckBox *checkBox = qobject_cast<QCheckBox *>(listWidget->itemWidget(listWidget->item(i)));
if (checkBox->isChecked()) {
selectedItems << checkBox->text();
}
}
qDebug() << "Selected Items:" << selectedItems;
});
QVBoxLayout *layout = new QVBoxLayout(widget);
layout->addWidget(listWidget);
layout->addWidget(selectAllBtn);
layout->addWidget(deselectAllBtn);
layout->addWidget(getSelectedBtn);
widget->setLayout(layout);
widget->show();
return a.exec();
}
```
阅读全文