1、设置单元格显示复选框
QStandardItem::setCheckable 设置单元格显示复选框,但显示的复选框居左且右边有文字区域。
2、自定义定理
实现复选框居中显示效果,需要重写 QStyledItemDelegate,
A、始终生效(这里介绍第一种)
-
paint() 绘制显示组件。
-
editorEvent() 处理编辑事件。
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);
}