Delegate(QLabel和QComboBox)

一.最终效果

二.实现思路

1.createEditor()中create两个控件,分别是QLabel和QComboBox,将其添加到一个widget中,然后返回该widget;

2.setEditorData()中,通过1中返回的widget找到label,设置参数;

3.setModelData()中,通过1中返回的widget找到combobox,找到当前选中的index,将其更新到model中;

4.updateEditorGeometrey()不变;

代码如下:

comboboxDelegate.h

 1 #ifndef COMBODELEGATE_H
 2 #define COMBODELEGATE_H
 3 
 4 #include <QItemDelegate>
 5 
 6 class ComboDelegate : public QItemDelegate
 7 {
 8     Q_OBJECT
 9 public:
10     ComboDelegate(QObject *parent = 0);
11 
12     QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex    &index) const;
13     void setEditorData(QWidget *editor, const QModelIndex &index) const;
14     void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
15     void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const  QModelIndex &index) const;
16 
17 };
18 
19 #endif // COMBODELEGATE_H

comboboxDelegate.cpp

 1 #include <QComboBox>
 2 #include <QDebug>
 3 
 4 ComboDelegate::ComboDelegate(QObject *parent) :
 5 QItemDelegate(parent)
 6 {
 7 }
 8 
 9 QWidget *ComboDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/*option*/,const QModelIndex &/*index*/) const
10 {
11 
12     QComboBox *comboBox = new QComboBox();
13     comboBox->setObjectName("comboBox");    //为该对象设置名字,否则后面使用findchild()函数会出错
14     //editor->lineEdit()->setAlignment(Qt::AlignCenter);
15     comboBox->setEditable(true);
16     //editor->setStyleSheet("QComboBox{border:1px solid gray;}""QComboBox QAbstractItemView::item{height:25px;}");
17 
18     //editor->setView(new QListView());
19     comboBox->addItem("N");
20     comboBox->addItem("m");
21     comboBox->addItem("m/s");
22     comboBox->installEventFilter(const_cast<ComboDelegate*>(this));
23 
24     QLabel *label = new QLabel;
25     label->setObjectName("label");
26     label->setText(tr("m/s"));
27 
28     QHBoxLayout *hLay = new QHBoxLayout;
29     hLay->addWidget(comboBox);
30     hLay->addWidget(label);
31 
32     QWidget *wighet = new QWidget(parent);
33     wighet->setLayout(hLay);
34     return wighet;
35 }
36 
37 void ComboDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
38 {
39     //QString str =index.model()->data(index).toString();
40     QString str = "m";
41     //QString str = "meos";
42     QWidget *box = static_cast<QWidget*>(editor);
43     //QPushButton *button = parentWidget->findChild<QPushButton *>("button1");
44     //QComboBox *comboBox = static_cast<QComboBox *>(box->findChild<QComboBox *>("editor"));
45     //int i = comboBox->findText(str);
46     //comboBox->setCurrentIndex(i);
47     //QComboBox *combo = new QComboBox(comboBox);
48     QLabel *label = editor->findChild<QLabel *>("label");
49     //label->setText(str);
50     qDebug("Test:%s",qPrintable(label->text()));
51     //label->setText(tr("1"));
52     //box->findChild<QComboBox *>("editor")->setCurrentIndex(i);
53 }
54 
55 void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
56 {
57     QWidget *box = static_cast<QWidget*>(editor);
58     QComboBox *comboBox= box->findChild<QComboBox *>();
59     QString str = comboBox->currentText();
60     model->setData(index,str);
61 }
62 
63 void ComboDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
64 {
65     editor->setGeometry(option.rect);
66 }

三. 注意以下几个函数:

1.comboBox->setObjectName("comboBox"); 

2.QWidget *box = static_cast<QWidget*>(editor);

3.QLabel *label = editor->findChild<QLabel *>("label");

4.QComboBox *comboBox= box->findChild<QComboBox *>();

原文地址:https://www.cnblogs.com/wang-kai/p/6566124.html