-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomboboxdelegate.cpp
56 lines (49 loc) · 2 KB
/
comboboxdelegate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "comboboxdelegate.h"
ComboBoxDelegate::ComboBoxDelegate(int choiceColumn, QObject *parent) :
QItemDelegate(parent)
{
this->choiceColumn = choiceColumn;
}
void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
if (index.column() == choiceColumn) {
QString text = index.model()->data(index, Qt::DisplayRole).toString();
QStyleOptionViewItem myOption = option;
myOption.displayAlignment = Qt::AlignHCenter | Qt::AlignVCenter;
drawDisplay(painter, myOption, myOption.rect, text);
drawFocus(painter, myOption, myOption.rect);
} else {
QItemDelegate::paint(painter, option, index);
}
}
QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
if (index.column() == choiceColumn) {
QComboBox *cmbBox = new QComboBox(parent);
cmbBox->addItem(tr("Begins With"));
cmbBox->addItem(tr("Contains"));
return cmbBox;
} else {
return QItemDelegate::createEditor(parent, option, index);
}
}
void ComboBoxDelegate::commitAndCloseEditor( void ) {
QComboBox *editor = qobject_cast<QComboBox *>(sender());
emit commitData(editor);
emit closeEditor(editor);
}
void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
if (index.column() == choiceColumn) {
int item = index.model()->data(index, Qt::DisplayRole).toInt();
QComboBox *cmbBox = qobject_cast<QComboBox *>(editor);
cmbBox->setCurrentIndex(item);
} else {
QItemDelegate::setEditorData(editor, index);
}
}
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
if (index.column() == choiceColumn) {
QComboBox *cmbBox = qobject_cast<QComboBox *>(editor);
model->setData(index, cmbBox->currentText());
} else {
QItemDelegate::setModelData(editor, model, index);
}
}