QT系列教程(19) Qt MVC结构之QItemDelegate介绍

QItemDelegate

当我们想重新实现一个代理时,可以子类化QItemDelegate。实现item编辑时特定的效果,比如在item编辑时我们设置一个QSpinBox返回。

创建一个QApplication项目,然后我们新增一个类,类名叫做spinboxdelegate。

cpp 复制代码
class SpinBoxDelegate : public QItemDelegate
{
     Q_OBJECT
public:
    explicit SpinBoxDelegate(QObject * parent=0);
    QWidget * createEditor(QWidget* parent,   const QStyleOptionViewItem &option,
                           const QModelIndex &index) const override;

    void setEditorData(QWidget *editor, const QModelIndex &index) const override;
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;

    void updateEditorGeometry(QWidget *editor,
                              const QStyleOptionViewItem &option,
                              const QModelIndex &index) const override;
};

SpinBoxDelegate类中声明了几个函数,这些函数在QItemDelegate继承而来,通过重写实现我们自己定义的代理功能。

createEditor函数是在item被双击后进入编辑状态时触发的,返回一个QWidget控件用来管理编辑。

setModelData是在item被修改后触发的,将改动的内容写入model中。

setEditorData是在item被双击进入编辑状态时,将model的内容写入editor中。

updateEditorGeometry是刷新editor的矩形区域,因为随着item变大或者拉伸,它的区域也要随之刷新。

具体实现

1 创建editor, 返回一个spinbox

cpp 复制代码
QWidget * SpinBoxDelegate::createEditor(QWidget* parent,   const QStyleOptionViewItem &option,
                                        const QModelIndex &index) const{
    QSpinBox * editor = new QSpinBox(parent);
    editor->setMinimum(0);
    editor->setMaximum(100);
    return editor;
}

2 在编辑状态时,将model的数据写入editor

cpp 复制代码
void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
    int value = index.model()->data(index, Qt::EditRole).toInt();
    QSpinBox * spinBox = static_cast<QSpinBox*>(editor);
    spinBox->setValue(value);
}

3 编辑完成时将editor的内容写入model

cpp 复制代码
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
    QSpinBox * spinBox = static_cast<QSpinBox*>(editor);
    spinBox->interpretText();
    int value = spinBox->value();
    model->setData(index, value, Qt::EditRole);
}

4 刷新矩形区域

cpp 复制代码
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
                          const QStyleOptionViewItem &option,
                          const QModelIndex &index) const {
    editor->setGeometry(option.rect);
}

接下来我们在MainWindow的构造函数里创建两个model和view,我们对其中的一个view使用我们自定义的delegate

cpp 复制代码
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QStandardItemModel * model = new QStandardItemModel(7,4,this);
    for(int row=0; row < 7; row++){
        for(int column = 0; column < 4; column++){
            QStandardItem * item = new QStandardItem(QString("%1").arg(row*4+column));
            model->setItem(row, column, item);
        }
    }

   QTableView* _table_view = new QTableView;
    _table_view->setModel(model);
    setCentralWidget(_table_view);
    this->resize(800,800);

    QTableView* _table_view2 = new QTableView;
    SpinBoxDelegate * delegate = new SpinBoxDelegate(this);
    QStandardItemModel * model2 = new QStandardItemModel(7,4,this);
    for(int row=0; row < 7; row++){
        for(int column = 0; column < 4; column++){
            QStandardItem * item = new QStandardItem(QString("%1").arg(row*4+column));
            model2->setItem(row, column, item);
        }
    }
     _table_view2->setModel(model2);
     _table_view2->setItemDelegate(delegate);
     _table_view2->show();
     _table_view2->resize(800,800);
}

运行程序后,双击两个view的item,可以看到效果的不同

源码链接

源码链接
https://gitee.com/secondtonone1/qt-learning-notes

相关推荐
程序猿追4 小时前
kotlin-retry 接入鸿蒙后,怎样确认它真的会停止重试
开发语言·kotlin·harmonyos
我是人✓4 小时前
Java文件上传三方式对比:从传统到SpringBoot
java·开发语言
智能直播4 小时前
网络RTMP拉流不卡顿、声音不同步,一文讲透缓冲区、时间戳
开发语言·ffmpeg·php·音视频·视频编解码
墨云安全5 小时前
内网资产无法暴露到公网,怎么做安全检测?2026 年内网渗透测试的落地路径
开发语言·安全·网络安全·渗透测试·php
摇滚侠5 小时前
《计算机是如何工作的:人人都能懂的计算机软硬件工作原理》第 9 章 机器码与汇编语言 阅读笔记 9
java·开发语言·笔记
Cc.Y5 小时前
Java零基础入门:深入Java核心:文件IO与序列化全解
java·开发语言·python
jimy17 小时前
c++的 Type Traits (一)
开发语言·c++
Web极客码7 小时前
Python Ollama 本地 Agent 入门与订单查询实践
开发语言·jvm·python
kimnoic8 小时前
PHP 8.2的DNF类型怎么用才规范
开发语言·php
_Evan_Yao8 小时前
前后端联调怎么排查问题?从 Network 到后端日志,一步步定位错误
开发语言·后端·团队开发·需求分析