qt实现代理Delegate

2025-06-15更新于 2025-08-12469 字4 分钟C++Qt
目录

1.继承QStyledItemDelegate

相关实例示例

cpp
#pragma once
#include <QStyledItemDelegate>
class GYServerDelegate :
    public QStyledItemDelegate
{
    Q_OBJECT
public:
    GYServerDelegate(QObject* parent = nullptr);
    // 绘制方法
    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
    // 触发编辑时返回一个控件或者其他
    QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
    // 在编辑状态时对返回的控件进行编辑或者写入数据
    void setEditorData(QWidget* editor, const QModelIndex& index) const override;
    // 当完成编辑时操作createEditor创建的控件,读取里面的数据或者其他东西
    void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override;
    // 控制是否刷新编辑的区域
    void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
    // 返回每一个条目的大小
    QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override;
};

注意事项!需要编辑条目时需要在model实现Qt::ItemFlags flags(const QModelIndex& index) const override;来控制条目是否能被编辑

2.重写方法

在实现代理类时,需要重写以下方法:
1.paint

cpp
void GYServerDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
    // 自定义绘制代码
}

2.createEditor

cpp
QWidget* GYServerDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
    // 返回自定义的编辑控件
}

3.setEditorData

cpp
void GYServerDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {
    // 设置编辑控件的数据
}

4.setModelData

cpp
void GYServerDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const {
    // 将编辑控件的数据保存到模型中
}

5.updateEditorGeometry

cpp
void GYServerDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const {
    // 更新编辑控件的几何形状
}

3.注意事项

在使用自定义代理时,需要注意以下几点:

1.在模型中实现 Qt::ItemFlags flags(const QModelIndex& index) const override 方法,以控制哪些条目可以被编辑。例如:

cpp
Qt::ItemFlags MyModel::flags(const QModelIndex& index) const {
    Qt::ItemFlags flags = QAbstractItemModel::flags(index);
    if (index.isValid()) {
        flags |= Qt::ItemIsEditable;
    }
    return flags;
    /*
    if (!index.isValid())
        return Qt::NoItemFlags;
        //这样允许编辑
    return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
    */
}

2.确保在代理类中正确处理编辑控件的创建、数据设置和保存。

3.根据需要自定义绘制方法,以实现特定的外观效果。

4.测试代理类的各个方法,确保其行为符合预期。

5.创建代理类时需要使用new创建不然无法被应用到listview上面

4.使用示例

在使用自定义代理类时,可以按照以下步骤进行:

  1. 创建模型和视图: QStandardItemModel* model = new QStandardItemModel(this); QTableView* view = new QTableView(this); view->setModel(model);
  2. 创建并设置自定义代理: GYServerDelegate* delegate = new GYServerDelegate(this); view->setItemDelegate(delegate);
  3. 向模型中添加数据:
    cpp model->setItem(0, 0, new QStandardItem("Item 1")); model->setItem(0, 1, new QStandardItem("Item 2"));

5.常见问题及解决方案

问题1:编辑控件没有正确显示

解决方案:确保在 createEditor 方法中返回了正确的编辑控件,并在 updateEditorGeometry 方法中正确设置了控件的几何形状。

问题2:编辑后的数据没有保存到模型中

解决方案:检查 setModelData 方法,确保将编辑控件中的数据正确保存到模型中。

问题3:自定义绘制方法没有生效

解决方案:检查 paint 方法,确保自定义的绘制代码正确实现,并且在需要时调用了基类的 paint 方法。

希望我的博客能够对你有帮助

hhhh6448

hhhh6448

转载请注明出处,欢迎留言交流~

评论

评论区还没开放,部署评论服务后就能留言啦~

主题色