Qtableview代理类实现添加Combobox 全部代码

我是把代理类单独放在一个头文件和APP中,我看网上有的代码是掺杂在其他类实现文件中,觉得这样不好

目前仅仅是实现了添加combobox,若实现其他的,可以在以下几个文件中再添加即可。

网上很多代码都是支离破碎,于是我就想整理下完整的实现步骤,方便自己以后查阅,如果能帮到别人,那更好了。

1.代理类实现头文件:

#ifndef ALLDELEGATE_H
#define ALLDELEGATE_H
#include <QItemDelegate>
#include <QComboBox>


class ComboDelegateYC : public QItemDelegate
{
    Q_OBJECT
public:
    ComboDelegateYC(QObject *parent = 0);

//任何代理类重写,都需要重写以下至少前三个函数
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;  //创建一个EDIT
    void setEditorData(QWidget *editor, const QModelIndex &index) const; //设置EDIT内的文本显示
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; //设置edit内的data值(有时需要,有时不需要看情况)
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; //设置形状之类,可以省略
};
#endif

2.代理类实现cpp

#include "alldelegate.h"
#include <QWidget>
#include "syscfg.h"
#include <QStyledItemDelegate>
//代理类的实现


ComboDelegateYC::ComboDelegateYC(QObject *parent) //空的构造函数也是必须的,之前忘了写,编译报错
        : QItemDelegate(parent)
{
}


QWidget* ComboDelegateYC::createEditor(
        QWidget *parent, const QStyleOptionViewItem &option,
        const QModelIndex &index) const
{
    if(index.column() == colRycchannel)   //colRycchannel是列的枚举类型,也可以用立即数,那样比较low
    {
        QComboBox *channelBox = new QComboBox(parent); //此处如果不加parent,edit不会内嵌在表格中 会跑出来
        channelBox->setFixedHeight(option.rect.height());
        channelBox->addItem(QString::fromLocal8Bit("CHANNEL_TYPE_U"));
        channelBox->addItem(QString::fromLocal8Bit("CHANNEL_TYPE_I"));
        channelBox->addItem(QString::fromLocal8Bit("CHANNEL_TYPE_P"));
        channelBox->addItem(QString::fromLocal8Bit("CHANNEL_TYPE_F"));


        return channelBox;
    }
    else
    {
        return QItemDelegate::createEditor(parent, option, index);
    }
    
}

//以下几个函数都可以直接拷贝用
void ComboDelegateYC::setEditorData(QWidget * editor, const QModelIndex & index) const
{
    if(index.column() == colRycchannel)
    {
        QComboBox *comboBox = static_cast<QComboBox*>(editor);
        if(comboBox)
        {
            QString str = index.model()->data(index, Qt::EditRole).toString();
            comboBox->setCurrentIndex(comboBox->findText(str));
        }
    }
    else
    {
        QItemDelegate::setEditorData(editor, index);
    }
}


void ComboDelegateYC::setModelData(QWidget *editor, QAbstractItemModel *model,
                                   const QModelIndex &index) const
{
    int nColumn = index.column();
    if((nColumn==colRycchannel))
    {
        QComboBox *comboBox = static_cast<QComboBox*>(editor);
        if(comboBox!=0)
            model->setData(index, comboBox->currentText(), Qt::EditRole);
    }
    else
    {
        QItemDelegate::setModelData(editor, model, index);
    }
}


void ComboDelegateYC::updateEditorGeometry(QWidget *editor,
                                           const QStyleOptionViewItem &option, const QModelIndex &) const
{
    editor->setGeometry(option.rect);
}



3.在某处如何调用

代用处文件需要包含以上两个

#include <QItemDelegate>
#include "alldelegate.h"

//在界面其他初始化显示工作完成以后添加如下代码

 ui->tableViewTransmitMeasures->setItemDelegate(new ComboDelegateYC(this));

  • 12
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值