-
Notifications
You must be signed in to change notification settings - Fork 0
/
weekcomboboxdelegate.cpp
48 lines (39 loc) · 1.75 KB
/
weekcomboboxdelegate.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
#include "weekcomboboxdelegate.h"
#include "classtable.h"
#include <QComboBox>
WeekComboBoxDelegate::WeekComboBoxDelegate( QObject *parent )
: QItemDelegate( parent ) {}
QWidget *WeekComboBoxDelegate::createEditor( QWidget * parent,
const QStyleOptionViewItem &option,
const QModelIndex &index ) const {
QComboBox *const comboBox = new QComboBox( parent );
comboBox->addItem( "Monday", 1 );
comboBox->addItem( "Tuesday", 2 );
comboBox->addItem( "Wednesday", 3 );
comboBox->addItem( "Thursday", 4 );
comboBox->addItem( "Friday", 5 );
comboBox->addItem( "Saturday", 6 );
comboBox->addItem( "Sunday", 7 );
return comboBox;
}
void WeekComboBoxDelegate::setEditorData( QWidget * editor,
const QModelIndex &index ) const {
QComboBox *const comboBox = qobject_cast< QComboBox * >( editor );
Q_ASSERT( comboBox );
const QString currentText = index.data( Qt::EditRole ).toString();
const int boxIndex = comboBox->findText( currentText );
if ( boxIndex >= 0 )
comboBox->setCurrentIndex( boxIndex );
}
void WeekComboBoxDelegate::setModelData( QWidget * editor,
QAbstractItemModel *model,
const QModelIndex & index ) const {
QComboBox *const comboBox = qobject_cast< QComboBox * >( editor );
Q_ASSERT( comboBox );
model->setData( index, comboBox->currentText(), Qt::EditRole );
}
void WeekComboBoxDelegate::updateEditorGeometry(
QWidget *editor, const QStyleOptionViewItem &option,
const QModelIndex & ) const {
editor->setGeometry( option.rect );
}