QT中级(3)QTableView自定义委托(三)实现QCheckBox委托并且将QCheckBox居中
m晴朗
https://blog.csdn.net/qq_40666149/article/details/128889536
QT中级(3)QTableView自定义委托(三)实现QCheckBox委托并且将QCheckBox居中_实现在qtableview中委托实现qcheckbox-CSDN博客
已于 2023-11-01 14:52:43 修改
阅读量2.3k
收藏 20
点赞数 3
分类专栏: QT中级 文章标签: qt ui QT自定义委托 QTableView
版权
QT自定义委托集合(一个类实现11种常用委托,包含源代码和demo)
讲解文章:https://blog.csdn.net/qq_40666149/article/details/128924599 1. 一个类实现11种常用委托,使用灵活,可以直接将代码加入到自己的项目中,零耦合。 2. 复选框控件:可以居中,可以在点击的时候发送信号,在外部轻松获得数据。 3. 图标控件:可以指定某一种条件(<、>、=灯),指定某一行为判断行,当某个行的值符合条件时,会先一种图标,不符合时显示另一种图标 4.密码框控件:可以显示明文和密码文,密码样式可以自己指定,同时当数值改变时,会发送信号 5. 数值控件/日期时间控件/进度条控件:在前面4篇文章中已经有教程了 6. 下拉框控件:可以一直显示在视图上,也可以点击时才显示 7. 按钮控件:可以根据关键词创建不同个数的按钮,且每个按钮都有独立的信号 已经在win和Ubuntu上测试可以正常运行 8. 本demo里面包含我们创建委托类的全部用法。
立即下载
QT中级
专栏收录该内容
11 篇文章10 订阅
订阅专栏
1 写在前面的话
我们在之前写的《QT(7)-初识委托》文章末尾提到,“使用一个类继承QStyledItemDelegate实现常用的控件委托,在使用时可以直接调用接口,灵活实现各种委托”。我们接下来几篇文章将先详细讲解各个控件的委托,最后整理成一个类,并分享源码。如果大家感兴趣,可以点个关注,后面我们一起学习!
讲解比较详细,大家可以跟着一步一步做,自己就可以实现了。
2 需要用到的部分知识
《QT(3)-QTableView》
《QT(4)-QAbstractItemView》
《QT(6)-QStandardItemModel》
《QT(7)-初识委托》
3 同系列文章
QT中级(1)QTableView自定义委托(一)实现QSpinBox、QDoubleSpinBox委托
QT中级(2)QTableView自定义委托(二)实现QProgressBar委托
4 实现QCheckBox委托
在QTableView中实现QCheckBox委托并居中显示,需要以下步骤:
创建一个自定义的委托类,继承自QStyledItemDelegate。
重写该委托类的paint函数,实现QCheckBox的居中显示。
重写该委托类的editorEvent函数,实现QCheckBox的状态改变。
为QTableView的相应单元格设置该委托类。
在数据模型中处理QCheckBox状态的更改,以便在重绘时正确显示。
4.1 第一步
和前几篇文章一样,我们需要建立一个项目,并且需要我们拖拽QTableView,以及对其初始化。我们在这里就不在赘述,大家可以参考前面几篇文章是如何创建的。
4.2 第二步
创建Delegate类,方法和前面几篇文章一样
需要我们先定义三函数
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
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;
1
2
3
这三个函数的含义前面几篇文章都已经提到了,不再赘述。
4.3 第三步
实现paint函数
void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(index.column() == 2)
{
bool value = index.data(Qt::UserRole).toInt();
QStyleOptionButton checkBoxOption;
QRect checkBoxRect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkBoxOption);
checkBoxOption.rect = option.rect;
checkBoxOption.rect.setLeft(option.rect.left() + (option.rect.width() - checkBoxRect.width()) / 2);
checkBoxOption.rect.setTop(option.rect.top() + (option.rect.height() - checkBoxRect.height()) / 2);
checkBoxOption.state = value ? QStyle::State_On :QStyle::State_Off;
checkBoxOption.state |= QStyle::State_Enabled;
QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkBoxOption, painter);
}//if
else
{
QStyledItemDelegate::paint(painter,option,index);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
实现editorEvent函数
bool Delegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
if(event->type() == QEvent::MouseButtonDblClick)//禁止双击编辑
return true;
//过滤鼠标松开
if (event->type() == QEvent::MouseButtonRelease) {
return false;
}
bool checked = index.data(Qt::UserRole).toBool();
model->setData(index, !checked, Qt::UserRole);
return QStyledItemDelegate::editorEvent(event,model,option,index);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
4.4 最后一步
我们需要在mainwindow.cpp中的init()调用delegate类实现委托。我们将QTableView的第一列设置为委托:
更新mainwindow.cpp中的init()函数
void MainWindow::init()
{
QStringList columnNames;
columnNames<<"QSpinBox"<<"QComboBox"<<"QCheckBox"<<"QProgressBar"<<"···";
model = new QStandardItemModel;
model->setRowCount(10);
model->setHorizontalHeaderLabels(columnNames);
ui->tableView->setModel(model);
Delegate *checkDelegate = new Delegate;
ui->tableView->setItemDelegateForColumn(2,checkDelegate);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
4.5 效果图
5 源码
源码
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_40666149/article/details/128889536