QTabelView使用代理自定义,第一列为QLabel第二列为下拉框(2)

文章详细描述了一个CustomParamViewDelegate类,用于Qt应用中,该类扩展了QStyledItemDelegate,实现了创建编辑器、设置编辑器数据、绘制以及与模型数据交互的方法,以满足项目中需要在视图中默认显示数据的需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一章节中的不足是需要进入编辑模式才能显示数据(即双击才能展示),但是真实的项目中需要默认需要展示数据的。需要实现paint函数和setmodel的时候设置自定义数据代码全部如下

CustomParamViewDelegate.cpp

#include "CustomParamViewDelegate.h"

CustomParamViewDelegate::CustomParamViewDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{
   }

CustomParamViewDelegate::~CustomParamViewDelegate()
{
   }

QWidget* CustomParamViewDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
   
    int type = index.data(Qt::UserRole).toInt();

    switch (type) {
   
    case (CustomParamViewDelegate::Type_ComboBox): {
   
        return new QComboBox(parent);
    }
    case (CustomParamViewDelegate::Type_Label): {
   
        return new QLabel(parent);
    }
    case (CustomParamViewDelegate::Type_LinEdit): {
   
        return new QLineEdit(parent);
    }
    default:
        break;
    }
    return nullptr;
}

void CustomParamViewDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
   
    int type = index.data(Qt::UserRole).toInt();
    //QString data = index.data().toString();
    QString data = index.data(Qt::UserRole + 1).toString();

    switch (type) {
   
    case (CustomParamViewDelegate::Type_ComboBox): {
   
        QComboBox* cob = static_cast<QComboBox*>(editor);
        QStringList strList = data.split(",");
        cob->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_小白鱼儿_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值