QTableView 设置QCheckbox代理

编程入门 行业动态 更新时间:2024-10-28 18:22:12

<a href=https://www.elefans.com/category/jswz/34/1732448.html style=QTableView 设置QCheckbox代理"/>

QTableView 设置QCheckbox代理

在QTableView中设置QCheckBox 

头文件:checkboxdelegate.h

#ifndef CHECKBOXDELEGATE_H
#define CHECKBOXDELEGATE_H#include <QStyledItemDelegate>class CheckBoxDelegate:public QStyledItemDelegate
{Q_OBJECTpublic:CheckBoxDelegate(QObject *parent = nullptr);protected:void paint(QPainter* painter,const QStyleOptionViewItem& option,const QModelIndex& index) const;bool editorEvent(QEvent *event,QAbstractItemModel *model,const QStyleOptionViewItem &option,const QModelIndex &index);
};#endif // CHECKBOXDELEGATE_H

源文件: checkboxdelegate.cpp

#include "checkboxdelegate.h"#include <QApplication>
#include <QMouseEvent>
#include <QPainter>
#include <QStyleOption>
#include <QDebug>static QRect CheckBoxRect(const QStyleOptionViewItem &viewItemStyleOptions)/*const*/
{//绘制按钮所需要的参数QStyleOptionButton checkBoxStyleOption;//按照给定的风格参数 返回元素子区域QRect checkBoxRect = QApplication::style()->subElementRect( QStyle::SE_CheckBoxIndicator, &checkBoxStyleOption);//返回QCheckBox坐标QPoint checkBoxPoint(viewItemStyleOptions.rect.x() + viewItemStyleOptions.rect.width() / 2 - checkBoxRect.width() / 2,viewItemStyleOptions.rect.y() + viewItemStyleOptions.rect.height() / 2 - checkBoxRect.height() / 2);//返回QCheckBox几何形状return QRect(checkBoxPoint, checkBoxRect.size());
}CheckBoxDelegate::CheckBoxDelegate(QObject *parent):QStyledItemDelegate(parent)
{}void CheckBoxDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,const QModelIndex& index)const
{bool checked = index.model()->data(index, Qt::DisplayRole).toBool();if(index.column() == 11)//重要:设置在第几列显示checkbox{qDebug() << checked;//此处可以设置信号;QStyleOptionButton checkBoxStyleOption;checkBoxStyleOption.state |= QStyle::State_Enabled;checkBoxStyleOption.state |= checked? QStyle::State_On : QStyle::State_Off;checkBoxStyleOption.rect = CheckBoxRect(option);QApplication::style()->drawControl(QStyle::CE_CheckBox,&checkBoxStyleOption,painter);}else{QStyledItemDelegate::paint(painter, option, index);}
}bool CheckBoxDelegate::editorEvent(QEvent *event,QAbstractItemModel *model,const QStyleOptionViewItem &option,const QModelIndex &index)
{if(index.column() == 11){if((event->type() == QEvent::MouseButtonRelease) ||(event->type() == QEvent::MouseButtonDblClick)){QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);if(mouseEvent->button() != Qt::LeftButton ||!CheckBoxRect(option).contains(mouseEvent->pos())){return true;}if(event->type() == QEvent::MouseButtonDblClick){return true;}}else if(event->type() == QEvent::KeyPress){if(static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space &&static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select){return false;}}else{return false;}bool checked = index.model()->data(index, Qt::DisplayRole).toBool();return model->setData(index, !checked, Qt::EditRole);}else{return QStyledItemDelegate::editorEvent(event, model, option, index);}
}

使用:

ui->tableView_statictable->setItemDelegate(new CheckBoxDelegate(this));

希望也能帮助到你 ~

更多推荐

QTableView 设置QCheckbox代理

本文发布于:2024-02-05 05:28:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1673506.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:QTableView   QCheckbox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!