Редакция 53 | Редакция 161 | К новейшей редакции | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS
Редакция 53 | Редакция 134 | ||
---|---|---|---|
1 | 1 | ||
2 | /****************************************************************************
|
2 | /****************************************************************************
|
3 | **
|
3 | **
|
4 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
4 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
5 | ** All rights reserved.
|
5 | ** All rights reserved.
|
6 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
6 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
7 | **
|
7 | **
|
8 | ** This file is part of the examples of the Qt Toolkit.
|
8 | ** This file is part of the examples of the Qt Toolkit.
|
9 | **
|
9 | **
|
10 | ** $QT_BEGIN_LICENSE:LGPL$
|
10 | ** $QT_BEGIN_LICENSE:LGPL$
|
11 | ** Commercial Usage
|
11 | ** Commercial Usage
|
12 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
12 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
13 | ** accordance with the Qt Commercial License Agreement provided with the
|
13 | ** accordance with the Qt Commercial License Agreement provided with the
|
14 | ** Software or, alternatively, in accordance with the terms contained in
|
14 | ** Software or, alternatively, in accordance with the terms contained in
|
15 | ** a written agreement between you and Nokia.
|
15 | ** a written agreement between you and Nokia.
|
16 | **
|
16 | **
|
17 | ** GNU Lesser General Public License Usage
|
17 | ** GNU Lesser General Public License Usage
|
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
19 | ** General Public License version 2.1 as published by the Free Software
|
19 | ** General Public License version 2.1 as published by the Free Software
|
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
21 | ** packaging of this file. Please review the following information to
|
21 | ** packaging of this file. Please review the following information to
|
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
24 | **
|
24 | **
|
25 | ** In addition, as a special exception, Nokia gives you certain additional
|
25 | ** In addition, as a special exception, Nokia gives you certain additional
|
26 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
26 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
28 | **
|
28 | **
|
29 | ** GNU General Public License Usage
|
29 | ** GNU General Public License Usage
|
30 | ** Alternatively, this file may be used under the terms of the GNU
|
30 | ** Alternatively, this file may be used under the terms of the GNU
|
31 | ** General Public License version 3.0 as published by the Free Software
|
31 | ** General Public License version 3.0 as published by the Free Software
|
32 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
32 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
33 | ** packaging of this file. Please review the following information to
|
33 | ** packaging of this file. Please review the following information to
|
34 | ** ensure the GNU General Public License version 3.0 requirements will be
|
34 | ** ensure the GNU General Public License version 3.0 requirements will be
|
35 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
35 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
36 | **
|
36 | **
|
37 | ** If you have questions regarding the use of this file, please contact
|
37 | ** If you have questions regarding the use of this file, please contact
|
38 | ** Nokia at qt-info@nokia.com.
|
38 | ** Nokia at qt-info@nokia.com.
|
39 | ** $QT_END_LICENSE$
|
39 | ** $QT_END_LICENSE$
|
40 | **
|
40 | **
|
41 | ****************************************************************************/
|
41 | ****************************************************************************/
|
42 | 42 | ||
43 | /*
|
43 | /*
|
44 | delegate.cpp
|
44 | delegate.cpp
|
45 | 45 | ||
46 | A delegate that allows the user to change integer values from the model
|
46 | A delegate that allows the user to change integer values from the model
|
47 | using a spin box widget.
|
47 | using a spin box widget.
|
48 | */
|
48 | */
|
49 | 49 | ||
50 | #include <QtGui>
|
50 | #include <QtGui>
|
51 | 51 | ||
52 | #include "delegate.h"
|
52 | #include "delegate.h"
|
53 | 53 | ||
54 | 54 | ||
55 | //! [0]
|
55 | //! [0]
|
56 | SpinBoxDelegate::SpinBoxDelegate(QObject *parent) |
56 | SpinBoxDelegate::SpinBoxDelegate(QObject *parent) |
57 | : QItemDelegate(parent) |
57 | : QItemDelegate(parent) |
58 | {
|
58 | {
|
- | 59 | // SpinBoxDelegate::getItems();
|
|
59 | }
|
60 | }
|
60 | //! [0]
|
61 | //! [0]
|
61 | 62 | ||
62 | //! [1]
|
63 | //! [1]
|
63 | QWidget *SpinBoxDelegate::createEditor(QWidget *parent, |
64 | QWidget *SpinBoxDelegate::createEditor(QWidget *parent, |
64 | const QStyleOptionViewItem &/* option */, |
65 | const QStyleOptionViewItem &/* option */, |
65 | const QModelIndex &/* index */) const |
66 | const QModelIndex &/* index */) const |
66 | {
|
67 | {
|
- | 68 | QStringList ID_list;
|
|
67 | //QSpinBox *editor = new QSpinBox(parent);
|
69 | QStringList Name_list;
|
- | 70 | ||
- | 71 | // getItems();
|
|
- | 72 | ||
- | 73 | ||
- | 74 | ||
- | 75 | ||
- | 76 | ||
68 | //editor->setMinimum(0);
|
77 | ID_list = items.keys(); |
69 | //editor->setMaximum(100);
|
78 | Name_list = items.values(); |
- | 79 | ||
70 | QComboBox *editor = new QComboBox(parent); |
80 | QComboBox *editor = new QComboBox(parent); |
- | 81 | ||
- | 82 | /*
|
|
71 | editor->addItem("0"); |
83 | editor->addItem("0");
|
72 | editor->addItem("1"); |
84 | editor->addItem("1");
|
73 | editor->addItem("2"); |
85 | editor->addItem("2");
|
74 | editor->addItem("3"); |
86 | editor->addItem("3");
|
75 | editor->addItem("4"); |
87 | editor->addItem("4");
|
76 | editor->addItem("5"); |
88 | editor->addItem("5");
|
77 | editor->addItem("6"); |
89 | editor->addItem("6");
|
78 | editor->addItem("7"); |
90 | editor->addItem("7");
|
79 | editor->addItem("8"); |
91 | editor->addItem("8");
|
80 | editor->addItem("9"); |
92 | editor->addItem("9");
|
- | 93 | */
|
|
- | 94 | ||
81 | // editor->addItem("0");
|
95 | // editor->addItem("0");
|
82 | 96 | ||
- | 97 | editor->addItems(Name_list); |
|
83 | return editor; |
98 | return editor; |
84 | }
|
99 | }
|
85 | //! [1]
|
100 | //! [1]
|
86 | 101 | ||
87 | //! [2]
|
102 | //! [2]
|
88 | void SpinBoxDelegate::setEditorData(QWidget *editor, |
103 | void SpinBoxDelegate::setEditorData(QWidget *editor, |
89 | const QModelIndex &index) const |
104 | const QModelIndex &index) const |
90 | {
|
105 | {
|
91 | // int value = index.model()->data(index, Qt::EditRole).toInt();
|
106 | QString value = index.model()->data(index, Qt::EditRole).toString(); |
- | 107 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
|
92 | 108 | ||
- | 109 | QStringList ID_list;
|
|
93 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
110 | QStringList Name_list;
|
- | 111 | int curr_index; |
|
- | 112 | ||
- | 113 | ID_list = items.keys(); |
|
94 | // spinBox->setValue(value);
|
114 | Name_list = items.values(); |
- | 115 | ||
95 | QString value = index.model()->data(index, Qt::EditRole).toString(); |
116 | curr_index = ID_list.indexOf(value); |
96 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
117 | comboBox->setCurrentIndex(curr_index); |
- | 118 | /****************************************************
|
|
97 | if (value == "0") comboBox->setCurrentIndex(0); |
119 | if (value == "0") comboBox->setCurrentIndex(0);
|
98 | if (value == "1") comboBox->setCurrentIndex(1); |
120 | if (value == "1") comboBox->setCurrentIndex(1);
|
99 | if (value == "2") comboBox->setCurrentIndex(2); |
121 | if (value == "2") comboBox->setCurrentIndex(2);
|
100 | if (value == "3") comboBox->setCurrentIndex(3); |
122 | if (value == "3") comboBox->setCurrentIndex(3);
|
101 | if (value == "4") comboBox->setCurrentIndex(4); |
123 | if (value == "4") comboBox->setCurrentIndex(4);
|
102 | if (value == "5") comboBox->setCurrentIndex(5); |
124 | if (value == "5") comboBox->setCurrentIndex(5);
|
103 | if (value == "6") comboBox->setCurrentIndex(6); |
125 | if (value == "6") comboBox->setCurrentIndex(6);
|
104 | if (value == "7") comboBox->setCurrentIndex(7); |
126 | if (value == "7") comboBox->setCurrentIndex(7);
|
105 | if (value == "8") comboBox->setCurrentIndex(8); |
127 | if (value == "8") comboBox->setCurrentIndex(8);
|
106 | if (value == "9") comboBox->setCurrentIndex(9); |
128 | if (value == "9") comboBox->setCurrentIndex(9);
|
- | 129 | ******************************************************/
|
|
107 | comboBox->setEditable(true); |
130 | comboBox->setEditable(true); |
108 | 131 | ||
109 | 132 | ||
110 | //comboBox->setItemText(0, value);
|
133 | //comboBox->setItemText(0, value);
|
111 | 134 | ||
112 | }
|
135 | }
|
113 | //! [2]
|
136 | //! [2]
|
114 | 137 | ||
115 | //! [3]
|
138 | //! [3]
|
116 | void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
139 | void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
117 | const QModelIndex &index) const |
140 | const QModelIndex &index) const |
118 | {
|
141 | {
|
119 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
- | |
120 | // spinBox->interpretText();
|
142 | QStringList ID_list;
|
121 | // int value = spinBox->value();
|
143 | QStringList Name_list;
|
122 | - | ||
123 | // model->setData(index, value, Qt::EditRole);
|
- | |
124 | 144 | ||
125 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
145 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
126 | int currIndex; |
146 | int currIndex; |
127 | currIndex = comboBox->currentIndex(); |
147 | currIndex = comboBox->currentIndex(); |
128 | QString value = comboBox->itemText(currIndex); |
148 | // QString value = comboBox->itemText(currIndex);
|
- | 149 | ||
- | 150 | ID_list = items.keys(); |
|
- | 151 | Name_list = items.values(); |
|
- | 152 | ||
- | 153 | QString value = ID_list.at(currIndex); |
|
129 | model->setData(index, value, Qt::EditRole); |
154 | model->setData(index, value, Qt::EditRole); |
- | 155 | ||
- | 156 | ||
130 | }
|
157 | }
|
131 | //! [3]
|
158 | //! [3]
|
132 | 159 | ||
133 | //! [4]
|
160 | //! [4]
|
134 | void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, |
161 | void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, |
135 | const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
162 | const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
136 | {
|
163 | {
|
137 | editor->setGeometry(option.rect); |
164 | editor->setGeometry(option.rect); |
138 | }
|
165 | }
|
139 | //! [4]
|
166 | //! [4]
|
- | 167 | ||
- | 168 | void SpinBoxDelegate::getItems(){ |
|
- | 169 | QMap <QString, QString> result_map; |
|
- | 170 | QSqlQuery q;
|
|
- | 171 | QString query_str;
|
|
- | 172 | bool ok; |
|
- | 173 | ||
- | 174 | query_str = tr("select * from `ListOfClasses`"); |
|
- | 175 | q.prepare(query_str); |
|
- | 176 | ||
- | 177 | ||
- | 178 | ok = q.exec(); |
|
- | 179 | ||
- | 180 | ||
- | 181 | ||
- | 182 | ||
- | 183 | if (!ok) { |
|
- | 184 | /*
|
|
- | 185 | QString error_str;
|
|
- | 186 | error_str = tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ");
|
|
- | 187 | error_str.append(inctance);
|
|
- | 188 | QMessageBox::critical( // Äèàëîã ñ ñîîáùåíèåì îá îøèáêå.
|
|
- | 189 | this, // Ðîäèòåëüñêèé âèäæåò.
|
|
- | 190 | QObject::tr("Database Error"), // Çàãîëîâîê.
|
|
- | 191 | q.lastError().text()); // Òåêñò ñîîáùåíèÿ.
|
|
- | 192 | // tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ")); // Òåêñò ñîîáùåíèÿ.
|
|
- | 193 | ||
- | 194 | */
|
|
- | 195 | items.clear(); |
|
- | 196 | return; |
|
- | 197 | }
|
|
- | 198 | ||
- | 199 | ||
- | 200 | // field_inctance = q.record().indexOf(tr("TableWhithInstance"));
|
|
- | 201 | items.clear(); |
|
- | 202 | while(q.next()){ |
|
- | 203 | ||
- | 204 | QString ID;
|
|
- | 205 | QString ClassName;
|
|
- | 206 | ID = q.value(0).toString(); // îïðåäåëÿåì ID |
|
- | 207 | ClassName = q.value(1).toString(); // îïðåäåëÿåì Name |
|
- | 208 | items.insert(ID, ClassName); |
|
- | 209 | }
|
|
- | 210 | ||
- | 211 | ||
- | 212 | // return result_map;
|
|
- | 213 | }
|
|
- | 214 | ||
- | 215 | ||
- | 216 | ||
- | 217 | ||
- | 218 | ||
- | 219 | ||
- | 220 | ||
- | 221 | ||
- | 222 | ||
- | 223 | ||
- | 224 | ||
- | 225 | ||
- | 226 | ||
- | 227 | ||
- | 228 | ||
- | 229 | ||
- | 230 | ||
140 | MyDEDelegate::MyDEDelegate( |
231 | MyDEDelegate::MyDEDelegate( |
141 | bool calpopup,
|
232 | bool calpopup,
|
142 | QObject *parent) |
233 | QObject *parent) |
143 | : QItemDelegate(parent), |
234 | : QItemDelegate(parent), |
144 | m_calpopup(calpopup) { |
235 | m_calpopup(calpopup) { |
145 | }
|
236 | }
|
146 | 237 | ||
147 | QWidget *MyDEDelegate::createEditor( |
238 | QWidget *MyDEDelegate::createEditor( |
148 | QWidget *parent,
|
239 | QWidget *parent,
|
149 | const QStyleOptionViewItem& /* option */, |
240 | const QStyleOptionViewItem& /* option */, |
150 | const QModelIndex& /* index */) const { |
241 | const QModelIndex& /* index */) const { |
151 | QDateEdit *editor = new QDateEdit(parent); |
242 | QDateEdit *editor = new QDateEdit(parent); |
152 | editor->setCalendarPopup(m_calpopup); |
243 | editor->setCalendarPopup(m_calpopup); |
153 | editor->installEventFilter(const_cast<MyDEDelegate*>(this)); |
244 | editor->installEventFilter(const_cast<MyDEDelegate*>(this)); |
154 | return editor; |
245 | return editor; |
155 | }
|
246 | }
|
156 | 247 | ||
157 | void MyDEDelegate::setEditorData( |
248 | void MyDEDelegate::setEditorData( |
158 | QWidget *editor,
|
249 | QWidget *editor,
|
159 | const QModelIndex &index) const { |
250 | const QModelIndex &index) const { |
160 | QDate value = index.model()->data( |
251 | QDate value = index.model()->data( |
161 | index, Qt::EditRole).toDate(); |
252 | index, Qt::EditRole).toDate(); |
162 | QDateEdit *de = static_cast<QDateEdit*>(editor); |
253 | QDateEdit *de = static_cast<QDateEdit*>(editor); |
163 | de->setDate(value); |
254 | de->setDate(value); |
164 | }
|
255 | }
|
165 | 256 | ||
166 | void MyDEDelegate::setModelData( |
257 | void MyDEDelegate::setModelData( |
167 | QWidget *editor,
|
258 | QWidget *editor,
|
168 | QAbstractItemModel *model,
|
259 | QAbstractItemModel *model,
|
169 | const QModelIndex& index) const { |
260 | const QModelIndex& index) const { |
170 | QDateEdit *de = static_cast<QDateEdit*>(editor); |
261 | QDateEdit *de = static_cast<QDateEdit*>(editor); |
171 | de->interpretText(); |
262 | de->interpretText(); |
172 | QDate value = de->date(); |
263 | QDate value = de->date(); |
173 | model->setData(index, value); |
264 | model->setData(index, value); |
174 | }
|
265 | }
|
175 | 266 | ||
176 | void MyDEDelegate::updateEditorGeometry( |
267 | void MyDEDelegate::updateEditorGeometry( |
177 | QWidget *editor,
|
268 | QWidget *editor,
|
178 | const QStyleOptionViewItem &option, |
269 | const QStyleOptionViewItem &option, |
179 | const QModelIndex& /* index */) const { |
270 | const QModelIndex& /* index */) const { |
180 | editor->setGeometry(option.rect); |
271 | editor->setGeometry(option.rect); |
181 | }
|
272 | }
|
- | 273 | ||
- | 274 | ||
182 | 275 |