一.最终效果
二.实现思路
1.createEditor()中create两个控件,分别是QLabel和QComboBox,将其添加到一个widget中,然后返回该widget;
2.setEditorData()中,通过1中返回的widget找到label,设置参数;
3.setModelData()中,通过1中返回的widget找到combobox,找到当前选中的index,将其更新到model中;
4.updateEditorGeometrey()不变;
代码如下:
comboboxDelegate.h
#ifndef COMBODELEGATE_H
#define COMBODELEGATE_H #include <QItemDelegate> class ComboDelegate : public QItemDelegate
{
Q_OBJECT
public:
ComboDelegate(QObject *parent = ); QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; }; #endif // COMBODELEGATE_H
comboboxDelegate.cpp
#include <QComboBox>
#include <QDebug> ComboDelegate::ComboDelegate(QObject *parent) :
QItemDelegate(parent)
{
} QWidget *ComboDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/*option*/,const QModelIndex &/*index*/) const
{ QComboBox *comboBox = new QComboBox();
comboBox->setObjectName("comboBox"); //为该对象设置名字,否则后面使用findchild()函数会出错
//editor->lineEdit()->setAlignment(Qt::AlignCenter);
comboBox->setEditable(true);
//editor->setStyleSheet("QComboBox{border:1px solid gray;}""QComboBox QAbstractItemView::item{height:25px;}"); //editor->setView(new QListView());
comboBox->addItem("N");
comboBox->addItem("m");
comboBox->addItem("m/s");
comboBox->installEventFilter(const_cast<ComboDelegate*>(this)); QLabel *label = new QLabel;
label->setObjectName("label");
label->setText(tr("m/s")); QHBoxLayout *hLay = new QHBoxLayout;
hLay->addWidget(comboBox);
hLay->addWidget(label); QWidget *wighet = new QWidget(parent);
wighet->setLayout(hLay);
return wighet;
} void ComboDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
{
//QString str =index.model()->data(index).toString();
QString str = "m";
//QString str = "meos";
QWidget *box = static_cast<QWidget*>(editor);
//QPushButton *button = parentWidget->findChild<QPushButton *>("button1");
//QComboBox *comboBox = static_cast<QComboBox *>(box->findChild<QComboBox *>("editor"));
//int i = comboBox->findText(str);
//comboBox->setCurrentIndex(i);
//QComboBox *combo = new QComboBox(comboBox);
QLabel *label = editor->findChild<QLabel *>("label");
//label->setText(str);
qDebug("Test:%s",qPrintable(label->text()));
//label->setText(tr("1"));
//box->findChild<QComboBox *>("editor")->setCurrentIndex(i);
} void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QWidget *box = static_cast<QWidget*>(editor);
QComboBox *comboBox= box->findChild<QComboBox *>();
QString str = comboBox->currentText();
model->setData(index,str);
} void ComboDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
{
editor->setGeometry(option.rect);
}
三. 注意以下几个函数:
1.comboBox->setObjectName("comboBox");
2.QWidget *box = static_cast<QWidget*>(editor);
3.QLabel *label = editor->findChild<QLabel *>("label");
4.QComboBox *comboBox= box->findChild<QComboBox *>();