首页 > 其他分享 >qt代理QItemDelegate使用

qt代理QItemDelegate使用

时间:2024-08-27 15:36:45浏览次数:3  
标签:index const qt QItemDelegate 代理 editor override model QModelIndex

*-----------------------------------File.h-------------------------------------------------------*

#include <QTableView>
#include <QItemDelegate>
#include <QStandardItemModel>
#include <QComboBox>
#include <QCheckBox>
#include <QPainter>
#include <QEvent>

class ComboBoxDelegate : public QItemDelegate {
Q_OBJECT
public:
ComboBoxDelegate(QObject* parent = nullptr) : QItemDelegate(parent) {}

QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override {
QComboBox* comboBox = new QComboBox(parent);
comboBox->addItems(QStringList() << "Option 1" << "Option 2" << "Option 3");
return comboBox;
}

void setEditorData(QWidget* editor, const QModelIndex& index) const override {
QComboBox* comboBox = qobject_cast<QComboBox*>(editor);
QString value = index.model()->data(index, Qt::EditRole).toString();
comboBox->setCurrentText(value);
}

void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override {
QComboBox* comboBox = qobject_cast<QComboBox*>(editor);
model->setData(index, comboBox->currentText(), Qt::EditRole);
}

void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const override {
editor->setGeometry(option.rect);
}
};

class CheckBoxDelegate : public QItemDelegate {
Q_OBJECT
public:
CheckBoxDelegate(QObject* parent = nullptr) : QItemDelegate(parent) {}

// 重写paint方法,用于绘制CheckBox
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override {
bool checked = index.model()->data(index, Qt::CheckStateRole).toBool();
QStyleOptionButton checkboxOption;
checkboxOption.state |= QStyle::State_Enabled;
checkboxOption.state |= checked ? QStyle::State_On : QStyle::State_Off;
checkboxOption.rect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkboxOption);
checkboxOption.rect.moveCenter(option.rect.center());
QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkboxOption, painter);
}

// 重写editorEvent方法,处理鼠标点击事件
bool editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index) override {
if (event->type() == QEvent::MouseButtonRelease) {
bool checked = index.model()->data(index, Qt::CheckStateRole).toBool();
model->setData(index, !checked, Qt::CheckStateRole);
return true;
}
return QItemDelegate::editorEvent(event, model, option, index);
}

// 创建编辑器 (此方法可选,通常与自定义QCheckBox编辑器一起使用)
QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override {
QCheckBox* checkBox = new QCheckBox(parent);
return checkBox;
}

// 将模型数据设置到编辑器
void setEditorData(QWidget* editor, const QModelIndex& index) const override {
QCheckBox* checkBox = qobject_cast<QCheckBox*>(editor);
bool checked = index.model()->data(index, Qt::CheckStateRole).toBool();
checkBox->setChecked(checked);
}

// 将编辑器的数据存储到模型中
void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override {
QCheckBox* checkBox = qobject_cast<QCheckBox*>(editor);
model->setData(index, checkBox->isChecked(), Qt::CheckStateRole);
}

// 更新编辑器的几何布局
void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const override {
editor->setGeometry(option.rect);
}
};

 

*-----------------------------------Main.h-------------------------------------------------------*

int main(int argc, char* argv[]) {
QApplication app(argc, argv);

QStandardItemModel model(5, 2);
for (int row = 0; row < 5; ++row) {
QStandardItem* item1 = new QStandardItem();
item1->setEditable(false);
item1->setCheckable(true);
item1->setCheckState(Qt::Unchecked);
model.setItem(row, 0, item1);

QStandardItem* item2 = new QStandardItem(QString("Option 1"));
model.setItem(row, 1, item2);
}

QTableView view;
view.setModel(&model);

ComboBoxDelegate* comboBoxDelegate = new ComboBoxDelegate(&view);
view.setItemDelegateForColumn(1, comboBoxDelegate);

CheckBoxDelegate* checkBoxDelegate = new CheckBoxDelegate(&view);
view.setItemDelegateForColumn(0, checkBoxDelegate);

view.show();
return app.exec();
}

 

标签:index,const,qt,QItemDelegate,代理,editor,override,model,QModelIndex
From: https://www.cnblogs.com/zwj-boy/p/18382817

相关文章

  • 爬虫使用代理IP池:提高数据抓取效率的绝佳实践
    爬虫使用代理IP池的最佳实践在进行网络爬虫和数据抓取时,使用代理IP池是一个有效的策略,可以提高抓取效率,避免IP封禁。本文将详细介绍如何在爬虫中使用代理IP池,并提供一些最佳实践。一、什么是代理IP池?代理IP池是指一组可用的代理IP地址集合。通过轮换使用这些代理IP,爬虫可以......
  • IP代理可以做爬虫吗?探讨其可行性与相应实践
    IP代理在网络爬虫中的应用在网络爬虫的世界里,IP代理就像是爬虫的“隐形斗篷”,帮助它们在获取数据的过程中隐藏真实身份,避免被目标网站识别和封禁。那么,IP代理到底能否用于爬虫呢?答案是肯定的!接下来,我们将深入探讨IP代理在爬虫中的重要性、工作原理以及使用时需要注意的事项。......
  • 爬虫初学者用哪家代理ip?
    选择合适的代理IP服务商的指南在网络爬虫的世界中,代理IP扮演着至关重要的角色。对于初学者来说,选择一个合适的代理IP服务商不仅可以提高数据抓取的效率,还能有效降低被封禁的风险。本文将探讨好的代理IP服务商应具备的特点,以及在选择和使用时需要注意的事项。一、好的代理IP服......
  • 爬虫使用优质代理:确保高效稳定的数据采集之道
    爬虫使用优质代理的最佳实践在进行网络爬虫时,使用优质代理就像是为你的爬虫装上了强劲的发动机,能够大幅提升数据抓取的效率和成功率。然而,选择和使用优质代理并非易事,今天我们就来探讨如何在爬虫中有效使用优质代理。1.什么是优质代理?优质代理通常指的是那些速度快、稳定性......
  • 爬虫使用代理IP:提升数据抓取效率的实践
    爬虫使用代理IP的技巧和方法在进行网络爬虫时,使用代理IP可以帮助你提高数据抓取效率和保护隐私。本文将介绍爬虫使用代理IP的技巧和方法,帮助你更好地进行数据抓取。为什么爬虫需要使用代理IP在进行大规模数据抓取时,目标网站可能会检测到你的爬虫行为,并对你的IP地址进行封锁......
  • VScode+QT 无法自动补全代码的解决方法
    问题:没有添加包含的头文件路径,即include文件夹所在位置第一步找到库路径并复制(在qt安装路径中)第二步打开vscode环境配置文件,添加库路径最终效果头文件红色波浪线消失了,并且代码可以完美补全!注意事项请根据自己的来修改。记得把路径的\更换成\\或者用/表示记得在incl......
  • 【编程规范具体案例(基于Qt、微软、谷歌和AUTOSAR C++14 参考)】 C++ 编码规范 之并发篇
    目录标题并发目录12.并发编程规范12.1线程创建与管理规则12.1.1\[必须]明确定义线程的生命周期管理策略12.1.2\[必须]为关键线程设置明确的标识符12.1.3\[必须]在多线程环境中安全地处理异常12.2线程同步规则12.2.1\[必须]使用线程安......
  • npm 设置代理
    目录 前言一、查看npm配置二、设置代理三、删除代理四、代理认证往期回顾 前言在使用npm时,如果您处于需要使用代理的网络环境中,您可以通过配置npm来设置代理。一、查看npm配置npmconfiglist或者单独查看npmconfiggetproxynpmconfiggethttps-prox......
  • Qt5.14.2 操作PostgreSQL 记录
    在Qt5.14.2中操作PostgreSQL数据库.#include<QSqlDatabase>#include<QSqlQuery>#include<QSqlError>#include<QDebug>//初始化数据库连接QSqlDatabasedb=QSqlDatabase::addDatabase("QPSQL");//qDebug()<<"aaaa"......
  • [QT]QSharedMemory读写cv::Mat
    1.写入共享内存voidWriteMat(constchar*section,constcv::Mat&img){QSharedMemory*mem=newQSharedMemory();mem->setKey(section);intsize=img.cols*img.rows*img.channels();//cols+rows+channels+data//ifexistreturn......