Qt listView添加复选框,可全选
时间: 2023-11-18 09:04:57 浏览: 246
要在Qt ListView中添加复选框并实现全选功能,需要执行以下步骤:
1. 创建一个自定义的QAbstractListModel或QStandardItemModel来作为数据模型。
2. 在数据模型中添加一个bool类型的属性来表示每个项目是否选中。
3. 在ListView的delegate中添加一个QCheckBox来显示每个项目的选中状态。
4. 创建一个全选/取消全选按钮并连接到相应的槽函数。
5. 在槽函数中遍历所有项目并设置它们的选中状态。
以下是一个示例代码:
```
// 自定义数据模型
class MyModel : public QAbstractListModel
{
public:
struct Item {
QString name;
bool selected;
};
MyModel(QObject *parent = nullptr) : QAbstractListModel(parent) {}
int rowCount(const QModelIndex &parent = QModelIndex()) const override {
return m_items.count();
}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override {
if (!index.isValid() || index.row() >= m_items.count())
return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole)
return m_items.at(index.row()).name;
if (role == Qt::CheckStateRole)
return m_items.at(index.row()).selected ? Qt::Checked : Qt::Unchecked;
return QVariant();
}
Qt::ItemFlags flags(const QModelIndex &index) const override {
if (!index.isValid() || index.row() >= m_items.count())
return Qt::NoItemFlags;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsUserCheckable;
}
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override {
if (!index.isValid() || index.row() >= m_items.count())
return false;
if (role == Qt::CheckStateRole) {
m_items[index.row()].selected = value.toBool();
emit dataChanged(index, index, {role});
return true;
}
if (role == Qt::EditRole) {
m_items[index.row()].name = value.toString();
emit dataChanged(index, index, {role});
return true;
}
return false;
}
QList<Item> items() const { return m_items; }
void setItems(const QList<Item> &items) {
beginResetModel();
m_items = items;
endResetModel();
}
void selectAll(bool selected) {
for (int i = 0; i < m_items.count(); ++i) {
if (m_items[i].selected != selected) {
m_items[i].selected = selected;
emit dataChanged(index(i), index(i), {Qt::CheckStateRole});
}
}
}
private:
QList<Item> m_items;
};
// ListView的delegate
class MyDelegate : public QStyledItemDelegate
{
public:
MyDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
QCheckBox *checkBox = new QCheckBox(parent);
checkBox->setCheckState(index.data(Qt::CheckStateRole).toBool() ? Qt::Checked : Qt::Unchecked);
return checkBox;
}
void setEditorData(QWidget *editor, const QModelIndex &index) const override {
QCheckBox *checkBox = qobject_cast<QCheckBox *>(editor);
checkBox->setCheckState(index.data(Qt::CheckStateRole).toBool() ? Qt::Checked : Qt::Unchecked);
}
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override {
QCheckBox *checkBox = qobject_cast<QCheckBox *>(editor);
model->setData(index, checkBox->isChecked(), Qt::CheckStateRole);
}
};
// MainWindow中的代码
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 创建数据模型并设置ListView的model和delegate
m_model = new MyModel(this);
ui->listView->setModel(m_model);
ui->listView->setItemDelegate(new MyDelegate(this));
// 添加测试数据
QList<MyModel::Item> items;
items.append({"Item 1", false});
items.append({"Item 2", false});
items.append({"Item 3", false});
items.append({"Item 4", false});
m_model->setItems(items);
// 创建全选/取消全选按钮并连接到槽函数
QPushButton *selectAllButton = new QPushButton("Select All", this);
ui->mainToolBar->addWidget(selectAllButton);
connect(selectAllButton, &QPushButton::clicked, this, &MainWindow::onSelectAllClicked);
}
void MainWindow::onSelectAllClicked()
{
m_model->selectAll(true);
}
```
阅读全文