Qt 之 QTableView 添加复选框(QAbstractItemDelegate)

作者: 一去、二三里
个人微信号: iwaleon
微信公众号: 高效程序员

上节分享了使用自定义模型QAbstractTableModel来实现复选框。下面我们来介绍另外一种方式:
自定义委托-QAbstractItemDelegate。

效果

这里写图片描述

QAbstractTableModel

源码

table_model.cpp

#define CHECK_BOX_COLUMN 0
#define File_PATH_COLUMN 1

TableModel::TableModel(QObject *parent)
    : QAbstractTableModel(parent)
{

}

TableModel::~TableModel()
{

}

// 更新表格数据
void TableModel::updateData(QList<FileRecord> recordList)
{
    m_recordList = recordList;
    beginResetModel();
    endResetModel();
}

// 行数
int TableModel::rowCount(const QModelIndex &parent) const
{
    return m_recordList.count();
}

// 列数
int TableModel::columnCount(const QModelIndex &parent) const
{
    return 2;
}

// 设置表格项数据
bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid())
        return false;

    int nColumn = index.column();
    FileRecord record = m_recordList.at(index.row());
    switch (role)
    {
    case Qt::DisplayRole:
    {
        if (nColumn == File_PATH_COLUMN)
        {
            record.strFilePath = value.toString();

            m_recordList.replace(index.row(), record);
            emit dataChanged(index, index);
            return true;
        }
    }
    case Qt::CheckStateRole:
    case Qt::UserRole:
    {
        if (nColumn == CHECK_BOX_COLUMN)
        {
            record.bChecked = value.toBool();

            m_recordList.replace(index.row(), record);
            emit dataChanged(index, index);
            return true;
        }
    }
    default:
        return false;
    }
    return false;
}

// 表格项数据
QVariant TableModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    int nRow = index.row();
    int nColumn = index.column();
    FileRecord record = m_recordList.at(nRow);

    switch (role)
    {
    case Qt::TextColorRole:
        return QColor(Qt::white);
    case Qt::TextAlignmentRole:
        return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
    case Qt::DisplayRole:
    {
        if (nColumn == File_PATH_COLUMN)
            return record.strFilePath;
        return "";
    }
    case Qt::UserRole:
    {
        if (nColumn == CHECK_BOX_COLUMN)
            return record.bChecked;
    }
    default:
        return QVariant();
    }

    return QVariant();
}

// 表头数据
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    switch (role)
    {
    case Qt::TextAlignmentRole:
        return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
    case Qt::DisplayRole:
    {
        if (orientation == Qt::Horizontal)
        {
            if (section == CHECK_BOX_COLUMN)
                return QStringLiteral("状态");

            if (section == File_PATH_COLUMN)
                return QStringLiteral("文件路径");
        }
    }
    default:
        return QVariant();
    }

    return QVariant();
}

// 表格可选中、可复选
Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return QAbstractItemModel::flags(index);

    Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;

    return flags;
}

接口说明

  • updateData
    主要用于更新数据,刷新界面。

  • data
    用来显示数据,根据角色(颜色、文本、对齐方式、选中状态等)判断需要显示的内容。

  • setData
    用来设置数据,根据角色(颜色、文本、对齐方式、选中状态等)判断需要设置的内容。

  • headerData
    用来显示水平/垂直表头的数据。

  • flags
    用来设置单元格的标志(可用、可选中、可复选等)。

QStyledItemDelegate

源码

CheckBoxDelegate::CheckBoxDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{

}

CheckBoxDelegate::~CheckBoxDelegate()
{

}

// 绘制复选框
void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem viewOption(option);
    initStyleOption(&viewOption, index);
    if (option.state.testFlag(QStyle::State_HasFocus))
        viewOption.state = viewOption.state ^ QStyle::State_HasFocus;

    QStyledItemDelegate::paint(painter, viewOption, index);

    if (index.column() == CHECK_BOX_COLUMN)
    {
        bool data = index.model()->data(index, Qt::UserRole).toBool();

        QStyleOptionButton checkBoxStyle;
        checkBoxStyle.state = data ? QStyle::State_On : QStyle::State_Off;
        checkBoxStyle.state |= QStyle::State_Enabled;
        checkBoxStyle.iconSize = QSize(20, 20);
        checkBoxStyle.rect = option.rect;

        QCheckBox checkBox;
        QApplication::style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &checkBoxStyle, painter, &checkBox);
    }
}

// 响应鼠标事件,更新数据
bool CheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    QRect decorationRect = option.rect;

    QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
    if (event->type() == QEvent::MouseButtonPress && decorationRect.contains(mouseEvent->pos()))
    {
        if (index.column() == CHECK_BOX_COLUMN)
        {
            bool data = model->data(index, Qt::UserRole).toBool();
            model->setData(index, !data, Qt::UserRole);
        }
    }

    return QStyledItemDelegate::editorEvent(event, model, option, index);
}

接口说明

  • paint
    用来设置表格样式,绘制复选框-状态、大小、显示区域等。

  • editorEvent
    用来设置数据,响应鼠标事件,判断当前选中状态,互相切换。

样式

复选框样式:

QCheckBox{
        spacing: 5px;
        color: white;
}
QCheckBox::indicator {
        width: 17px;
        height: 17px;
}
QCheckBox::indicator:enabled:unchecked {
        image: url(:/Images/checkBox);
}
QCheckBox::indicator:enabled:unchecked:hover {
        image: url(:/Images/checkBoxHover);
}
QCheckBox::indicator:enabled:unchecked:pressed {
        image: url(:/Images/checkBoxPressed);
}
QCheckBox::indicator:enabled:checked {
        image: url(:/Images/checkBoxChecked);
}
QCheckBox::indicator:enabled:checked:hover {
        image: url(:/Images/checkBoxCheckedHover);
}
QCheckBox::indicator:enabled:checked:pressed {
        image: url(:/Images/checkBoxCheckedPressed);
}
QCheckBox::indicator:enabled:indeterminate {
        image: url(:/Images/checkBoxIndeterminate);
}
QCheckBox::indicator:enabled:indeterminate:hover {
        image: url(:/Images/checkBoxIndeterminateHover);
}
QCheckBox::indicator:enabled:indeterminate:pressed {
        image: url(:/Images/checkBoxIndeterminatePressed);
}

使用

QTableView *pTableView = new QTableView(this);
TableModel *pModel = new TableModel(this);
CheckBoxDelegate *pDelegate = new CheckBoxDelegate(this);

// 设置单行选中、最后一列拉伸、表头不高亮、无边框等
pTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
pTableView->horizontalHeader()->setStretchLastSection(true);
pTableView->horizontalHeader()->setHighlightSections(false);
pTableView->verticalHeader()->setVisible(false);
pTableView->setShowGrid(false);
pTableView->setFrameShape(QFrame::NoFrame);
pTableView->setSelectionMode(QAbstractItemView::SingleSelection);
pTableView->setModel(pModel);
pTableView->setItemDelegate(pDelegate);

// 加载数据、更新界面
QList<FileRecord> recordList;
for (int i = 0; i < 5; ++i)
{
	FileRecord record;
	record.bChecked = false;
	record.strFilePath = QString("E:/Qt/image_%1.png").arg(i + 1);
	recordList.append(record);
}
pModel->updateData(recordList);
  • 19
    点赞
  • 106
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 45
    评论
QTableViewQt框架中的一个类,用于展示表格数据。要使用QTableView,需要先包含头文件#include <QTableView>和#include <QStandardItemModel>。然后创建一个QTableView对象和QStandardItemModel,并使用QTableView的setModel()函数将视图和模型对象进行绑定。 以下是一个继承QTableView的示例代码: ```cpp // .h #include <QTableView> #include <QStandardItemModel> class DBTableView : public QTableView { public: explicit DBTableView(QWidget* _parent_widget = nullptr); ~DBTableView() override; private: QStandardItemModel* db_table_model_; }; // .cpp DBTableView::DBTableView(QWidget* _parent_widget) : QTableView(_parent_widget) { db_table_model_ = new QStandardItemModel(); setModel(db_table_model_); } DBTableView::~DBTableView() {} ``` 要填充表格数据,可以使用QStandardItemModel作为表格的数据模型。每一行每一列的数据可以通过操作模型来设置。 以下是一个使用QStandardItemModel的示例代码: ```cpp // .h #include <QStandardItemModel> #include <QWidget> #include <QTableView> #include <QStandardItem> #include <QPushButton> #include <QHBoxLayout> #include <QVBoxLayout> #include <QEvent> #include <QMenu> #include <QDebug> class TableViewWidget : public QWidget { Q_OBJECT public: TableViewWidget(QWidget* parent = nullptr); ~TableViewWidget(); private: void initTableView(); private: QTableView* m_pMyTableView; QStandardItemModel* m_model; QPushButton* m_pBtnRemove; QPushButton* m_pBtnAdd; }; // .cpp TableViewWidget::TableViewWidget(QWidget* parent) : QWidget(parent) { initTableView(); } TableViewWidget::~TableViewWidget() { delete m_model; } void TableViewWidget::initTableView() { m_model = new QStandardItemModel(this); m_pMyTableView = new QTableView(this); m_pMyTableView->setModel(m_model); // 设置其他属性和布局 // 添加到布局中 QVBoxLayout* layout = new QVBoxLayout(this); layout->addWidget(m_pMyTableView); layout->addWidget(m_pBtnRemove); layout->addWidget(m_pBtnAdd); setLayout(layout); } bool TableViewWidget::eventFilter(QObject* object, QEvent* event) { // 事件过滤器的处理逻辑 // ... return false; } ``` 在创建完QTableView对象后,可以设置使用自定义菜单策略,并进行信号槽关联。下面是一个示例: ```cpp void TestTable::tableViewMenu(const QPoint& _pos) { // 响应数据处理 } void TestTable::initUI() { // ... connect(table_view, &QTableView::customContextMenuRequested, this, &TestTabel::tableViewMenu); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 45
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一去丶二三里

有收获,再打赏!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值