Qtableview代理类实现添加Combobox 全部代码
2017年07月24日 13:26:23 小岗哥哥 阅读数 3374
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.youkuaiyun.com/ganggegel/article/details/76019417
我是把代理类单独放在一个头文件和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));