-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabledelegate.cpp
144 lines (113 loc) · 3.9 KB
/
tabledelegate.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "tabledelegate.h"
#include <QComboBox>
#include <QCheckBox>
#include <QLineEdit>
#include <QLabel>
#include <QAbstractItemView>
QosDelegate::QosDelegate(QWidget *parent) : QItemDelegate(parent)
{
}
QWidget *QosDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option);
Q_UNUSED(index);
QComboBox *comboBox = new QComboBox(parent);
QLineEdit *lineEdit = new QLineEdit(comboBox);
lineEdit->setReadOnly(true);
lineEdit->setAlignment(Qt::AlignCenter);
comboBox->setLineEdit(lineEdit);
comboBox->addItem("0");
comboBox->addItem("1");
comboBox->addItem("2");
for (int i=0; i<comboBox->count(); i++) {
QStandardItemModel *itemModel = static_cast<QStandardItemModel*>(comboBox->view()->model());
itemModel->item(i)->setTextAlignment(Qt::AlignCenter);
}
return comboBox;
}
void QosDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString text = index.model()->data(index, Qt::EditRole).toString();
QComboBox *comboBox = static_cast<QComboBox*>(editor);
int tindex = comboBox->findText(text);
comboBox->setCurrentIndex(tindex);
}
void QosDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QComboBox *comboBox = static_cast <QComboBox*>(editor);
QString text = comboBox->currentText();
model->setData(index, text, Qt::EditRole);
}
void QosDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(index);
editor->setGeometry(option.rect);
}
CheckedDelegate::CheckedDelegate(QWidget *parent) : QItemDelegate(parent)
{
}
QWidget *CheckedDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option);
Q_UNUSED(index);
QCheckBox *checkBox = new QCheckBox(parent);
checkBox->setChecked(true);
return checkBox;
}
void CheckedDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
Q_UNUSED(editor);
Q_UNUSED(index);
}
void CheckedDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QCheckBox *checkBox = static_cast <QCheckBox*>(editor);
model->setData(index, checkBox->text(), Qt::EditRole);
}
void CheckedDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(index);
editor->setGeometry(option.rect);
}
TopicModel::TopicModel(QObject *parent) : QStandardItemModel(parent)
{
}
TopicModel::TopicModel(int row, int column, QObject *parent) : QStandardItemModel(row, column, parent)
{
}
bool TopicModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid())
return false;
// if (role == Qt::CheckStateRole && index.column() == 0) {
// m_checkd[index.row()] = (value == Qt::Checked ? Qt::Checked : Qt::Unchecked);
// return true;
// }
return QStandardItemModel::setData(index, value, role);
}
QVariant TopicModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
// switch (role) {
// case Qt::TextAlignmentRole:
// return Qt::AlignCenter;
// case Qt::CheckStateRole:
// if (index.column() == 0) {
// if (m_checkd.contains(index.row()))
// return m_checkd[index.row()] == Qt::Checked ? Qt::Checked : Qt::Unchecked;
// return Qt::Unchecked;
// }
// default:
// break;
// }
return QStandardItemModel::data(index, role);
}
Qt::ItemFlags TopicModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return nullptr;
if (index.column() == 0)
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
return QStandardItemModel::flags(index);
}