Rev 123 | Rev 129 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 123 | Rev 126 | ||
---|---|---|---|
Line 58... | Line 58... | ||
58 | : QItemDelegate(parent) |
58 | : QItemDelegate(parent) |
59 | {
|
59 | {
|
60 | }
|
60 | }
|
61 | //! [0]
|
61 | //! [0]
|
62 | 62 | ||
- | 63 | void SpinBoxDelegate::setInctance(QString newInctance){ |
|
- | 64 | inctance = newInctance; |
|
- | 65 | }
|
|
- | 66 | ||
- | 67 | void SpinBoxDelegate::getItems(){ |
|
- | 68 | QMap <QString, QString> result_map; |
|
- | 69 | QSqlQuery q;
|
|
- | 70 | QString query_str;
|
|
- | 71 | bool ok; |
|
- | 72 | ||
- | 73 | if (SpinBoxDelegate::inctance.isEmpty()) |
|
- | 74 | {
|
|
- | 75 | items.clear(); |
|
- | 76 | return; |
|
- | 77 | }
|
|
- | 78 | ||
- | 79 | query_str = tr("select * from `"); |
|
- | 80 | query_str.append(inctance); |
|
- | 81 | query_str.append( tr("`")); |
|
- | 82 | ||
- | 83 | q.prepare(query_str); |
|
- | 84 | ||
- | 85 | ||
- | 86 | ok = q.exec(); |
|
- | 87 | ||
- | 88 | ||
- | 89 | ||
- | 90 | ||
- | 91 | if (!ok) { |
|
- | 92 | /*
|
|
- | 93 | QString error_str;
|
|
- | 94 | error_str = tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ");
|
|
- | 95 | error_str.append(inctance);
|
|
- | 96 | QMessageBox::critical( // Äèàëîã ñ ñîîáùåíèåì îá îøèáêå.
|
|
- | 97 | this, // Ðîäèòåëüñêèé âèäæåò.
|
|
- | 98 | QObject::tr("Database Error"), // Çàãîëîâîê.
|
|
- | 99 | q.lastError().text()); // Òåêñò ñîîáùåíèÿ.
|
|
- | 100 | // tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ")); // Òåêñò ñîîáùåíèÿ.
|
|
- | 101 | ||
- | 102 | */
|
|
- | 103 | items.clear(); |
|
- | 104 | return; |
|
- | 105 | }
|
|
- | 106 | ||
- | 107 | ||
- | 108 | // field_inctance = q.record().indexOf(tr("TableWhithInstance"));
|
|
- | 109 | while(q.next()){ |
|
- | 110 | ||
- | 111 | QString ID;
|
|
- | 112 | QString Name;
|
|
- | 113 | ID = q.value(0).toString(); // îïðåäåëÿåì ID |
|
- | 114 | Name = q.value(1).toString(); // îïðåäåëÿåì Name |
|
- | 115 | items.insert(ID, Name); |
|
- | 116 | }
|
|
- | 117 | ||
- | 118 | ||
- | 119 | // return result_map;
|
|
- | 120 | }
|
|
- | 121 | ||
- | 122 | ||
63 | //! [1]
|
123 | //! [1]
|
64 | QWidget *SpinBoxDelegate::createEditor(QWidget *parent, |
124 | QWidget *SpinBoxDelegate::createEditor(QWidget *parent, |
65 | const QStyleOptionViewItem &/* option */, |
125 | const QStyleOptionViewItem &/* option */, |
66 | const QModelIndex &/* index */) const |
126 | const QModelIndex &/* index */) const |
67 | {
|
127 | {
|
68 | //QSpinBox *editor = new QSpinBox(parent);
|
128 | //QSpinBox *editor = new QSpinBox(parent);
|
69 | //editor->setMinimum(0);
|
129 | //editor->setMinimum(0);
|
70 | //editor->setMaximum(100);
|
130 | //editor->setMaximum(100);
|
- | 131 | QStringList ID_list;
|
|
- | 132 | QStringList Name_list;
|
|
- | 133 | ||
- | 134 | // getItems();
|
|
- | 135 | ||
- | 136 | ||
- | 137 | ||
- | 138 | ||
- | 139 | ||
- | 140 | ID_list = items.keys(); |
|
- | 141 | Name_list = items.values(); |
|
- | 142 | ||
71 | QComboBox *editor = new QComboBox(parent); |
143 | QComboBox *editor = new QComboBox(parent); |
- | 144 | ||
- | 145 | /*
|
|
72 | editor->addItem("0"); |
146 | editor->addItem("0");
|
73 | editor->addItem("1"); |
147 | editor->addItem("1");
|
74 | editor->addItem("2"); |
148 | editor->addItem("2");
|
75 | editor->addItem("3"); |
149 | editor->addItem("3");
|
76 | editor->addItem("4"); |
150 | editor->addItem("4");
|
77 | editor->addItem("5"); |
151 | editor->addItem("5");
|
78 | editor->addItem("6"); |
152 | editor->addItem("6");
|
79 | editor->addItem("7"); |
153 | editor->addItem("7");
|
80 | editor->addItem("8"); |
154 | editor->addItem("8");
|
81 | editor->addItem("9"); |
155 | editor->addItem("9");
|
82 | // editor->addItem("0");
|
156 | */
|
83 | 157 | ||
- | 158 | // editor->addItem("0");
|
|
- | 159 | ||
- | 160 | editor->addItems(Name_list); |
|
84 | return editor; |
161 | return editor; |
85 | }
|
162 | }
|
86 | //! [1]
|
163 | //! [1]
|
87 | 164 | ||
88 | //! [2]
|
165 | //! [2]
|
Line 93... | Line 170... | ||
93 | 170 | ||
94 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
171 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
95 | // spinBox->setValue(value);
|
172 | // spinBox->setValue(value);
|
96 | QString value = index.model()->data(index, Qt::EditRole).toString(); |
173 | QString value = index.model()->data(index, Qt::EditRole).toString(); |
97 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
174 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
- | 175 | ||
- | 176 | QStringList ID_list;
|
|
- | 177 | QStringList Name_list;
|
|
- | 178 | int curr_index; |
|
- | 179 | ||
- | 180 | ID_list = items.keys(); |
|
- | 181 | Name_list = items.values(); |
|
- | 182 | ||
- | 183 | curr_index = ID_list.indexOf(value); |
|
- | 184 | comboBox->setCurrentIndex(curr_index); |
|
- | 185 | /****************************************************
|
|
98 | if (value == "0") comboBox->setCurrentIndex(0); |
186 | if (value == "0") comboBox->setCurrentIndex(0);
|
99 | if (value == "1") comboBox->setCurrentIndex(1); |
187 | if (value == "1") comboBox->setCurrentIndex(1);
|
100 | if (value == "2") comboBox->setCurrentIndex(2); |
188 | if (value == "2") comboBox->setCurrentIndex(2);
|
101 | if (value == "3") comboBox->setCurrentIndex(3); |
189 | if (value == "3") comboBox->setCurrentIndex(3);
|
102 | if (value == "4") comboBox->setCurrentIndex(4); |
190 | if (value == "4") comboBox->setCurrentIndex(4);
|
103 | if (value == "5") comboBox->setCurrentIndex(5); |
191 | if (value == "5") comboBox->setCurrentIndex(5);
|
104 | if (value == "6") comboBox->setCurrentIndex(6); |
192 | if (value == "6") comboBox->setCurrentIndex(6);
|
105 | if (value == "7") comboBox->setCurrentIndex(7); |
193 | if (value == "7") comboBox->setCurrentIndex(7);
|
106 | if (value == "8") comboBox->setCurrentIndex(8); |
194 | if (value == "8") comboBox->setCurrentIndex(8);
|
107 | if (value == "9") comboBox->setCurrentIndex(9); |
195 | if (value == "9") comboBox->setCurrentIndex(9);
|
- | 196 | ******************************************************/
|
|
108 | comboBox->setEditable(true); |
197 | comboBox->setEditable(true); |
109 | 198 | ||
110 | 199 | ||
111 | //comboBox->setItemText(0, value);
|
200 | //comboBox->setItemText(0, value);
|
112 | 201 | ||
Line 120... | Line 209... | ||
120 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
209 | // QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
121 | // spinBox->interpretText();
|
210 | // spinBox->interpretText();
|
122 | // int value = spinBox->value();
|
211 | // int value = spinBox->value();
|
123 | 212 | ||
124 | // model->setData(index, value, Qt::EditRole);
|
213 | // model->setData(index, value, Qt::EditRole);
|
- | 214 | QStringList ID_list;
|
|
- | 215 | QStringList Name_list;
|
|
125 | 216 | ||
126 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
217 | QComboBox *comboBox = static_cast<QComboBox*>(editor); |
127 | int currIndex; |
218 | int currIndex; |
128 | currIndex = comboBox->currentIndex(); |
219 | currIndex = comboBox->currentIndex(); |
129 | QString value = comboBox->itemText(currIndex); |
220 | // QString value = comboBox->itemText(currIndex);
|
- | 221 | ||
- | 222 | ID_list = items.keys(); |
|
- | 223 | Name_list = items.values(); |
|
- | 224 | ||
- | 225 | QString value = ID_list.at(currIndex); |
|
130 | model->setData(index, value, Qt::EditRole); |
226 | model->setData(index, value, Qt::EditRole); |
131 | }
|
227 | }
|
132 | //! [3]
|
228 | //! [3]
|
133 | 229 | ||
134 | //! [4]
|
230 | //! [4]
|