Редакция 126 | К новейшей редакции | Содержимое файла | Последнее изменение | Открыть журнал | RSS
Редакция | Автор | № строки | Строка |
---|---|---|---|
123 | pingvin | 1 | |
2 | |||
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
||
4 | ** All rights reserved. |
||
5 | ** Contact: Nokia Corporation (qt-info@nokia.com) |
||
6 | ** |
||
7 | ** This file is part of the examples of the Qt Toolkit. |
||
8 | ** |
||
9 | ** $QT_BEGIN_LICENSE:LGPL$ |
||
10 | ** Commercial Usage |
||
11 | ** Licensees holding valid Qt Commercial licenses may use this file in |
||
12 | ** accordance with the Qt Commercial License Agreement provided with the |
||
13 | ** Software or, alternatively, in accordance with the terms contained in |
||
14 | ** a written agreement between you and Nokia. |
||
15 | ** |
||
16 | ** GNU Lesser General Public License Usage |
||
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
||
18 | ** General Public License version 2.1 as published by the Free Software |
||
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
||
20 | ** packaging of this file. Please review the following information to |
||
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
||
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
||
23 | ** |
||
24 | ** In addition, as a special exception, Nokia gives you certain additional |
||
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception |
||
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
||
27 | ** |
||
28 | ** GNU General Public License Usage |
||
29 | ** Alternatively, this file may be used under the terms of the GNU |
||
30 | ** General Public License version 3.0 as published by the Free Software |
||
31 | ** Foundation and appearing in the file LICENSE.GPL included in the |
||
32 | ** packaging of this file. Please review the following information to |
||
33 | ** ensure the GNU General Public License version 3.0 requirements will be |
||
34 | ** met: http://www.gnu.org/copyleft/gpl.html. |
||
35 | ** |
||
36 | ** If you have questions regarding the use of this file, please contact |
||
37 | ** Nokia at qt-info@nokia.com. |
||
38 | ** $QT_END_LICENSE$ |
||
39 | ** |
||
40 | ****************************************************************************/ |
||
41 | |||
42 | /* |
||
43 | |||
44 | |||
45 | A delegate that allows the user to change integer values from the model |
||
46 | |||
47 | */ |
||
48 | |||
49 | #include <QtGui> |
||
50 | |||
51 | #include "delegate.h" |
||
52 | |||
53 | |||
54 | |||
55 | |||
56 | : QItemDelegate(parent) |
||
57 | { |
||
58 | } |
||
59 | //! [0] |
||
60 | |||
61 | //! [1] |
||
62 | |||
63 | const QStyleOptionViewItem &/* option */, |
||
64 | const QModelIndex &/* index */) const |
||
65 | { |
||
66 | //QSpinBox *editor = new QSpinBox(parent); |
||
67 | //editor->setMinimum(0); |
||
68 | //editor->setMaximum(100); |
||
69 | QComboBox *editor = new QComboBox(parent); |
||
70 | editor->addItem("0"); |
||
71 | editor->addItem("1"); |
||
72 | editor->addItem("2"); |
||
73 | editor->addItem("3"); |
||
74 | editor->addItem("4"); |
||
75 | editor->addItem("5"); |
||
76 | editor->addItem("6"); |
||
77 | editor->addItem("7"); |
||
78 | editor->addItem("8"); |
||
79 | editor->addItem("9"); |
||
80 | // editor->addItem("0"); |
||
81 | |||
82 | return editor; |
||
83 | |||
84 | //! [1] |
||
85 | |||
86 | //! [2] |
||
87 | |||
88 | const QModelIndex &index) const |
||
89 | { |
||
90 | // int value = index.model()->data(index, Qt::EditRole).toInt(); |
||
91 | |||
92 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor); |
||
93 | |||
94 | QString value = index.model()->data(index, Qt::EditRole).toString(); |
||
95 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
||
96 | if (value == "0") comboBox->setCurrentIndex(0); |
||
97 | if (value == "1") comboBox->setCurrentIndex(1); |
||
98 | if (value == "2") comboBox->setCurrentIndex(2); |
||
99 | if (value == "3") comboBox->setCurrentIndex(3); |
||
100 | if (value == "4") comboBox->setCurrentIndex(4); |
||
101 | if (value == "5") comboBox->setCurrentIndex(5); |
||
102 | if (value == "6") comboBox->setCurrentIndex(6); |
||
103 | if (value == "7") comboBox->setCurrentIndex(7); |
||
104 | if (value == "8") comboBox->setCurrentIndex(8); |
||
105 | if (value == "9") comboBox->setCurrentIndex(9); |
||
106 | comboBox->setEditable(true); |
||
107 | |||
108 | |||
109 | |||
110 | |||
111 | } |
||
112 | |||
113 | |||
114 | //! [3] |
||
115 | |||
116 | const QModelIndex &index) const |
||
117 | { |
||
118 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor); |
||
119 | // spinBox->interpretText(); |
||
120 | // int value = spinBox->value(); |
||
121 | |||
122 | // model->setData(index, value, Qt::EditRole); |
||
123 | |||
124 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
||
125 | |||
126 | currIndex = comboBox->currentIndex(); |
||
127 | QString value = comboBox->itemText(currIndex); |
||
128 | model->setData(index, value, Qt::EditRole); |
||
129 | } |
||
130 | //! [3] |
||
131 | |||
132 | //! [4] |
||
133 | |||
134 | const QStyleOptionViewItem &option, const QModelIndex &/* index */) const |
||
135 | { |
||
136 | editor->setGeometry(option.rect); |
||
137 | } |
||
138 | //! [4] |
||
139 | MyDEDelegate::MyDEDelegate( |
||
140 | bool calpopup, |
||
141 | QObject *parent) |
||
142 | : QItemDelegate(parent), |
||
143 | m_calpopup(calpopup) { |
||
144 | } |
||
145 | |||
146 | QWidget *MyDEDelegate::createEditor( |
||
147 | |||
148 | const QStyleOptionViewItem& /* option */, |
||
149 | const QModelIndex& /* index */) const { |
||
150 | QDateEdit *editor = new QDateEdit(parent); |
||
151 | editor->setCalendarPopup(m_calpopup); |
||
152 | editor->installEventFilter(const_cast<MyDEDelegate*>(this)); |
||
153 | return editor; |
||
154 | } |
||
155 | |||
156 | void MyDEDelegate::setEditorData( |
||
157 | |||
158 | const QModelIndex &index) const { |
||
159 | QDate value = index.model()->data( |
||
160 | index, Qt::EditRole).toDate(); |
||
161 | QDateEdit *de = static_cast<QDateEdit*>(editor); |
||
162 | de->setDate(value); |
||
163 | } |
||
164 | |||
165 | void MyDEDelegate::setModelData( |
||
166 | |||
167 | QAbstractItemModel *model, |
||
168 | const QModelIndex& index) const { |
||
169 | QDateEdit *de = static_cast<QDateEdit*>(editor); |
||
170 | de->interpretText(); |
||
171 | QDate value = de->date(); |
||
172 | model->setData(index, value); |
||
173 | } |
||
174 | |||
175 | void MyDEDelegate::updateEditorGeometry( |
||
176 | |||
177 | const QStyleOptionViewItem &option, |
||
178 | const QModelIndex& /* index */) const { |
||
179 | editor->setGeometry(option.rect); |
||
180 | } |
||
181 |