QStyledItemDelegate 和 QItemDelegate 的作用

在Qt中,QStyledItemDelegate和QItemDelegate是用于自定义和控制项视图控件(如QListView、QTableView、QTreeView)中项的显示和编辑的委托类。它们提供了对项的外观和编辑行为的定制能力。尽管它们在功能上有相似之处,但它们之间有一些关键的区别。

QItemDelegate

作用

QItemDelegate是Qt中较早期的委托类,用于处理视图中项的显示和编辑。它继承自QAbstractItemDelegate,并提供了默认的绘制和编辑功能。

特点

  • 绘制功能 :QItemDelegate使用QStyle进行绘制,这意味着它的绘制风格是基于系统的默认风格。
  • 编辑功能:它提供了默认的编辑器(如文本框、复选框等)和编辑行为。
  • 自定义性 :可以通过重写paint和createEditor等虚函数来自定义项的显示和编辑行为。

使用示例

cpp 复制代码
#include <QApplication>
#include <QTableView>
#include <QStandardItemModel>
#include <QItemDelegate>

class MyItemDelegate : public QItemDelegate
{
    Q_OBJECT

public:
    MyItemDelegate(QObject *parent = nullptr) : QItemDelegate(parent) {}

    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
        // 自定义绘制逻辑
        QItemDelegate::paint(painter, option, index);
    }

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
        // 自定义编辑器
        return QItemDelegate::createEditor(parent, option, index);
    }
};

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

    QTableView view;
    QStandardItemModel model(4, 2);
    view.setModel(&model);

    MyItemDelegate *delegate = new MyItemDelegate(&view);
    view.setItemDelegate(delegate);

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

QStyledItemDelegate

作用

QStyledItemDelegate是Qt 4.4引入的,旨在替代QItemDelegate,提供更灵活和现代的项委托。它也是继承自QAbstractItemDelegate,并使用QStyle进行绘制,但与QItemDelegate相比,它在处理复杂和定制的用户界面时更加高效和灵活。

特点

  • 增强的绘制功能 :QStyledItemDelegate利用了QStyle的高级功能,可以更好地支持复杂的UI元素和现代风格。
  • 统一的风格:它能更好地与Qt的样式系统集成,确保在不同平台和风格下的外观一致性。
  • 简化的自定义 :提供了一些额外的虚函数(如initStyleOption),使自定义项的显示和编辑更加简单和灵活。

使用示例

cpp 复制代码
#include <QApplication>
#include <QTableView>
#include <QStandardItemModel>
#include <QStyledItemDelegate>

class MyStyledItemDelegate : public QStyledItemDelegate
{
    Q_OBJECT

public:
    MyStyledItemDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}

    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
        // 自定义绘制逻辑
        QStyledItemDelegate::paint(painter, option, index);
    }

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
        // 自定义编辑器
        return QStyledItemDelegate::createEditor(parent, option, index);
    }

    void setEditorData(QWidget *editor, const QModelIndex &index) const override {
        // 设置编辑器的数据
        QStyledItemDelegate::setEditorData(editor, index);
    }

    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override {
        // 保存编辑器的数据
        QStyledItemDelegate::setModelData(editor, model, index);
    }

    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
        // 更新编辑器的几何形状
        QStyledItemDelegate::updateEditorGeometry(editor, option, index);
    }
};

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

    QTableView view;
    QStandardItemModel model(4, 2);
    view.setModel(&model);

    MyStyledItemDelegate *delegate = new MyStyledItemDelegate(&view);
    view.setItemDelegate(delegate);

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

区别

  1. 绘制机制 :QStyledItemDelegate利用了更高级的QStyle功能,能够更好地处理复杂的绘制需求,而QItemDelegate使用的是较早期的绘制方法。
  2. 自定义简便性 :QStyledItemDelegate提供了更多的虚函数和工具函数,使得自定义绘制和编辑行为更加简便和灵活。
  3. 风格一致性 :QStyledItemDelegate能够更好地与Qt的样式系统集成,确保在不同平台和风格下的外观一致性。

结论

总的来说,QStyledItemDelegate是对QItemDelegate的改进,提供了更强大和灵活的功能。在大多数情况下,建议使用QStyledItemDelegate来处理自定义项的显示和编辑。QItemDelegate虽然仍然可以使用,但在新项目中更推荐使用QStyledItemDelegate。

相关推荐
Lhan.zzZ7 小时前
Qt WebEngineView 在 Linux 上无法加载网页:从“看不见 HTML“到“禁用沙箱“
linux·c++·qt
Quz15 小时前
QML TabBar 标签栏交互效果合集
qt·动画·qml
zhanghaha131418 小时前
Qt 工程实例:逐词逐句保姆级讲解
qt
架构师伯子明1 天前
QT+OpenGL(3):FBO离屏渲染
开发语言·qt
mengzhi啊2 天前
把数据存入sql数据库。qt,c++。
数据库·sql·qt
mengzhi啊2 天前
SerialPortItem对串口再封装一层,省去一些常用的步骤,不用每次都去写类
qt
用户8334423966142 天前
从批量操作到任务中心,再到现场工作台:DeviceForge v2.10—v2.12 完整演进
qt
宛 禾2 天前
Qt 控件入门:QLabel 从拖拽到对象树,一篇讲透
c++·qt·学习
打工仔折腾 AI3 天前
Qt网络编程实战:UDP、TCP到HTTP的落地经验与选型
网络·人工智能·后端·python·qt·udp·ai agent 实战
Lhan.zzZ3 天前
Linux + Qt/QML:解决 QtQuick.VirtualKeyboard.Plugins 模块加载失败与环境变量配置
linux·数据库·qt