Delegate(QLabel和QComboBox)

一.最終效果函數

 

二.實現思路this

1.createEditor()中create兩個控件,分別是QLabel和QComboBox,將其添加到一個widget中,而後返回該widget;spa

2.setEditorData()中,經過1中返回的widget找到label,設置參數;code

3.setModelData()中,經過1中返回的widget找到combobox,找到當前選中的index,將其更新到model中;對象

4.updateEditorGeometrey()不變;blog

代碼以下:get

comboboxDelegate.hit

 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.cppio

 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 }

三. 注意如下幾個函數:table

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

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

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

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

相關文章
相關標籤/搜索