QTableView 中设置复选框列居中

1、设置单元格显示复选框

QStandardItem::setCheckable 设置单元格显示复选框,但显示的复选框居左且右边有文字区域。

2、自定义定理

实现复选框居中显示效果,需要重写 QStyledItemDelegate,

    A、始终生效(这里介绍第一种)

   B、只在编辑时起作用

  • createEditor()  创建用于编辑模型数据的widget组件 ,如QSpinBox、QComboBox组件。
  • setEditorData() 从数据模型获取数据,供widget组件进行编辑。
  • setModelData()  将widget上的数据更新到数据模型。
  • updateEditorGeometry() 用于设置widget组件大小。

3、显示效果

单元格复选框居中请参考:

QTableView 中设置复选框列居中_qtableview居中-CSDN博客

4、使用

 m_checkBoxDelegate = new QCheckBoxDelegate(this);
 ui->tableView->setItemDelegateForColumn(0,m_checkBoxDelegate);
 //单元格复选框选中响应
 connect(m_checkBoxDelegate, SIGNAL(checkStateChanged(const QModelIndex& , bool)), 
            this, SLOT(slotCheckStateChanged(const QModelIndex&, bool)));

 5、代码

#ifndef QCHECKBOXDELEGATE_H
#define QCHECKBOXDELEGATE_H

#include <QStyledItemDelegate>


class QCheckBoxDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    explicit QCheckBoxDelegate(QObject *parent = nullptr);
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
    bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override;
signals:
    void checkStateChanged(const QModelIndex &index, bool bState);
};

#endif // QCHECKBOXDELEGATE_H
#include "QCheckBoxDelegate.h"
#include <QMouseEvent>
#include <QApplication>
#include <QPainter>

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

}

void QCheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                                    const QModelIndex &index) const
{
    QStyleOptionButton checkOption;
    checkOption.state |= QStyle::State_Enabled;
    bool checkState = index.model()->data(index,Qt::CheckStateRole).toBool();
    if(checkState)
    {
        checkOption.state |= QStyle::State_On;
    }
    else
    {
        checkOption.state |= QStyle::State_Off;
    }
    bool isSelected = option.state & QStyle::State_Selected;
    bool hasFocus = option.state & QStyle::State_HasFocus;
    if (isSelected || hasFocus)
    {
        painter->fillRect(option.rect, option.palette.highlight()); // 选中或有焦点时的颜色
    }

    QRect checkBoxRect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkOption, option.widget);
    checkBoxRect.moveCenter(option.rect.center());
    painter->save();
    QPoint pt;
    pt.setX(checkBoxRect.left());
    pt.setY(checkBoxRect.center().y());
    painter->translate(pt);
    QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkOption, painter,option.widget);
    painter->restore();
}

bool QCheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
                                          const QStyleOptionViewItem &option, const QModelIndex &index)
{
    if(event->type() == QEvent::MouseButtonDblClick)
    {
        return true;
    }
    if(event->type() == QEvent::MouseButtonRelease)
    {
        QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
        QStyleOptionButton checkOption;
        QRect checkBoxRect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkOption, option.widget);
        checkBoxRect.moveCenter(option.rect.center());
        if(checkBoxRect.contains(mouseEvent->pos()))
        {
            bool checkState = model->data(index, Qt::CheckStateRole).toBool();
            model->setData(index, !checkState, Qt::CheckStateRole);
            emit checkStateChanged(index, !checkState);
            return true;
        }
    }
    return QStyledItemDelegate::editorEvent(event, model, option,index);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值