You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将自定义Widget绘制到QListView?Qt模型视图委托问题

问题描述

我在程序主窗口中添加了QListWidget,随后创建了继承自QWidget的自定义类,通过独立对话框创建新对象并添加至QListWidget。但尝试调整元素顺序时,元素内容会被删除。经调研得知,若要创建可编辑(重排)的Widget列表,最佳方案是使用QListView+模型/视图+委托模式。

我参考了Qt官方的stardelegate示例,但理解有限。我已实现可用的模型与委托,却仍不知如何将自定义Widget绘制到QListView中。以下是我的模型与委托代码:

模型代码

model.h

#ifndef OPERATIONWIDGETMODEL_H
#define OPERATIONWIDGETMODEL_H

#include <QAbstractItemModel>

class OperationWidgetModel : public QAbstractItemModel
{
    Q_OBJECT
public:
   explicit OperationWidgetModel(QObject *parent = nullptr);

    QVariant data(const QModelIndex &index, int role) const override;
    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
    QModelIndex parent(const QModelIndex &index) const override;
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;

    void AddItem(OperationWidget* item);

private:
    QList<OperationWidget*> widgetList;

};

#endif // OPERATIONWIDGETMODEL_H

model.cpp

#include "operationwidgetmodel.h"

OperationWidgetModel::OperationWidgetModel(QObject *parent) : QAbstractItemModel(parent)
{
    //
}

QVariant OperationWidgetModel::data(const QModelIndex &index, int role) const
{
    return QVariant();
}


QModelIndex OperationWidgetModel::index(int row, int column, const QModelIndex &parent) const
{
    QModelIndex index;

    index = createIndex(row,column,nullptr);
    return index;
}

QModelIndex OperationWidgetModel::parent(const QModelIndex &index) const
{
    return QModelIndex();
}

int OperationWidgetModel::rowCount(const QModelIndex &parent) const
{
    if(parent.isValid())return 0;

    return 1;
}

int OperationWidgetModel::columnCount(const QModelIndex &parent) const
{
    if(parent.isValid()) return 0;
    return 1;
}
void OperationWidgetModel::AddItem(OperationWidget *item)
{

    beginInsertRows(QModelIndex(),widgetList.count(),widgetList.count());
    widgetList.append(item);
    endInsertRows();
}

委托代码

delegate.h

#ifndef OPERATIONWIDGETDELEGATE_H
#define OPERATIONWIDGETDELEGATE_H

#include <QStyledItemDelegate>
#include "operationwidget.h"

class OperationWidgetDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    OperationWidgetDelegate(QObject *parent = nullptr);

    void paint(QPainter *painter, const QStyleOptionViewItem &option,
               const QModelIndex &index) const override;
    QSize sizeHint(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;
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index) const override;


};

#endif // OPERATIONWIDGETDELEGATE_H

delegate.cpp

#include "operationwidgetdelegate.h"



OperationWidgetDelegate::OperationWidgetDelegate(QObject *parent)
{

}

void OperationWidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{

    QStyledItemDelegate::paint(painter,option,index);
}

QSize OperationWidgetDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{

    OperationWidget widget;

    return QSize(widget.geometry().width(),widget.geometry().height());
}

QWidget *OperationWidgetDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    OperationWidget *widget = new OperationWidget(parent);

    return widget;
}

void OperationWidgetDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    OperationWidget *widget = qobject_cast<OperationWidget*>(editor);
    if(widget)
    {
        widget->SetOperationName("Cięcie");
    }

}

void OperationWidgetDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    OperationWidget *widget = qobject_cast<OperationWidget*>(editor);

    if(widget)
    {
        model->setData(index,widget->GetOperationName(),Qt::EditRole);
    }
}

界面截图

  • 自定义Widget样式:
    自定义Widget截图

  • 当前QListView效果:
    QListView效果截图

提问

请问如何将自定义Widget绘制到QListView中?


内容的提问来源于stack exchange,提问作者Mikołaj Nawrocki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 16:37:08