Редакция 91 | Редакция 93 | К новейшей редакции | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS
Редакция 91 | Редакция 92 | ||
---|---|---|---|
1 | #include "mainwindow.h"
|
1 | #include "mainwindow.h"
|
2 | #include "ui_mainwindow.h"
|
2 | #include "ui_mainwindow.h"
|
3 | 3 | ||
4 | MainWindow::MainWindow(QWidget *parent) : |
4 | MainWindow::MainWindow(QWidget *parent) : |
5 | QMainWindow(parent), |
5 | QMainWindow(parent), |
6 | ui(new Ui::MainWindow) |
6 | ui(new Ui::MainWindow) |
7 | {
|
7 | {
|
8 | ui->setupUi(this); |
8 | ui->setupUi(this); |
9 | 9 | ||
10 | MainWindow::readModel(); |
10 | MainWindow::readModel(); |
11 | 11 | ||
12 | //QString model_str;
|
12 | //QString model_str;
|
13 | MainWindow::creatModelString(); |
13 | MainWindow::creatModelString(); |
14 | int i; |
14 | int i; |
15 | i++; |
15 | i++; |
- | 16 | QFile file_tmp("temp.txt"); |
|
- | 17 | bool ok; |
|
- | 18 | ok = file_tmp.open(QIODevice::ReadWrite | QIODevice::Text); |
|
- | 19 | QTextStream out(&file_tmp); |
|
- | 20 | out << MainWindow::modelString; |
|
- | 21 | file_tmp.close(); |
|
16 | 22 | ||
17 | }
|
23 | }
|
18 | 24 | ||
19 | MainWindow::~MainWindow() |
25 | MainWindow::~MainWindow() |
20 | {
|
26 | {
|
21 | delete ui; |
27 | delete ui; |
22 | }
|
28 | }
|
23 | 29 | ||
24 | void MainWindow::changeEvent(QEvent *e) |
30 | void MainWindow::changeEvent(QEvent *e) |
25 | {
|
31 | {
|
26 | QMainWindow::changeEvent(e); |
32 | QMainWindow::changeEvent(e); |
27 | switch (e->type()) { |
33 | switch (e->type()) { |
28 | case QEvent::LanguageChange: |
34 | case QEvent::LanguageChange: |
29 | ui->retranslateUi(this); |
35 | ui->retranslateUi(this); |
30 | break; |
36 | break; |
31 | default: |
37 | default: |
32 | break; |
38 | break; |
33 | }
|
39 | }
|
34 | }
|
40 | }
|
35 | 41 | ||
36 | 42 | ||
37 | bool MainWindow::readModel(){ |
43 | bool MainWindow::readModel(){ |
38 | bool result; |
44 | bool result; |
39 | QString model_str;
|
45 | QString model_str;
|
40 | QFile file(":/model.txt"); |
46 | QFile file(":/model.txt"); |
- | 47 | ||
41 | rootItem1 = new QTreeWidgetItem(ui->treeWidget); |
48 | rootItem1 = new QTreeWidgetItem(ui->treeWidget); |
42 | rootItem1->setText(0, tr("Óðîâåíü 1")); |
49 | rootItem1->setText(0, tr("Óðîâåíü 1")); |
43 | // rootItem2 = new QTreeWidgetItem(rootItem1);
|
50 | // rootItem2 = new QTreeWidgetItem(rootItem1);
|
44 | // rootItem2->setText(0, tr("óðîâåíü 2"));
|
51 | // rootItem2->setText(0, tr("óðîâåíü 2"));
|
45 | rootItem1->setCheckState(0,Qt::Checked); |
52 | rootItem1->setCheckState(0,Qt::Checked); |
46 | // rootItem2->setCheckState(0,Qt::Checked);
|
53 | // rootItem2->setCheckState(0,Qt::Checked);
|
47 | 54 | ||
48 | 55 | ||
49 | 56 | ||
50 | result = file.open(QIODevice::ReadOnly); |
57 | result = file.open(QIODevice::ReadOnly); |
51 | if (result) { |
58 | if (result) { |
52 | model_str = QString(tr(file.readAll())); |
59 | model_str = QString(tr(file.readAll())); |
53 | }
|
60 | }
|
54 | 61 | ||
55 | setupModelData(model_str.split(QString("\n")), rootItem1); |
62 | setupModelData(model_str.split(QString("\n")), rootItem1); |
56 | 63 | ||
57 | 64 | ||
58 | return result; |
65 | return result; |
59 | 66 | ||
60 | }
|
67 | }
|
61 | 68 | ||
62 | 69 | ||
63 | void MainWindow::setupModelData(const QStringList &lines, QTreeWidgetItem *parent) |
70 | void MainWindow::setupModelData(const QStringList &lines, QTreeWidgetItem *parent) |
64 | {
|
71 | {
|
65 | QList<QTreeWidgetItem*> parents; |
72 | QList<QTreeWidgetItem*> parents; |
66 | QList<int> indentations; |
73 | QList<int> indentations; |
67 | parents << parent; |
74 | parents << parent; |
68 | indentations << 0; |
75 | indentations << 0; |
69 | 76 | ||
70 | int number = 0; |
77 | int number = 0; |
71 | 78 | ||
72 | while (number < lines.count()) { |
79 | while (number < lines.count()) { |
73 | int position = 0; |
80 | int position = 0; |
74 | while (position < lines[number].length()) { |
81 | while (position < lines[number].length()) { |
75 | if (lines[number].mid(position, 1) != " ") |
82 | if (lines[number].mid(position, 1) != " ") |
76 | break; |
83 | break; |
77 | position++; |
84 | position++; |
78 | }
|
85 | }
|
79 | 86 | ||
80 | QString lineData = lines[number].mid(position).trimmed(); |
87 | QString lineData = lines[number].mid(position).trimmed(); |
81 | 88 | ||
82 | if (!lineData.isEmpty()) { |
89 | if (!lineData.isEmpty()) { |
83 | // Read the column data from the rest of the line.
|
90 | // Read the column data from the rest of the line.
|
84 | QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts); |
91 | QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts); |
85 | QList<QVariant> columnData; |
92 | QList<QVariant> columnData; |
86 | for (int column = 0; column < columnStrings.count(); ++column) |
93 | for (int column = 0; column < columnStrings.count(); ++column) |
87 | columnData << columnStrings[column]; |
94 | columnData << columnStrings[column]; |
88 | 95 | ||
89 | if (position > indentations.last()) { |
96 | if (position > indentations.last()) { |
90 | // The last child of the current parent is now the new parent
|
97 | // The last child of the current parent is now the new parent
|
91 | // unless the current parent has no children.
|
98 | // unless the current parent has no children.
|
92 | 99 | ||
93 | if (parents.last()->childCount() > 0) { |
100 | if (parents.last()->childCount() > 0) { |
94 | parents << parents.last()->child(parents.last()->childCount()-1); |
101 | parents << parents.last()->child(parents.last()->childCount()-1); |
95 | indentations << position; |
102 | indentations << position; |
96 | }
|
103 | }
|
97 | } else { |
104 | } else { |
98 | while (position < indentations.last() && parents.count() > 0) { |
105 | while (position < indentations.last() && parents.count() > 0) { |
99 | parents.pop_back(); |
106 | parents.pop_back(); |
100 | indentations.pop_back(); |
107 | indentations.pop_back(); |
101 | }
|
108 | }
|
102 | }
|
109 | }
|
103 | 110 | ||
104 | // Append a new item to the current parent's list of children.
|
111 | // Append a new item to the current parent's list of children.
|
105 | // // // parents.last()->appendChild(new QTreeWidgetItem(columnData, parents.last()));
|
112 | // // // parents.last()->appendChild(new QTreeWidgetItem(columnData, parents.last()));
|
106 | QTreeWidgetItem* itm_tmp; |
113 | QTreeWidgetItem* itm_tmp; |
107 | itm_tmp = new QTreeWidgetItem( parents.last()); |
114 | itm_tmp = new QTreeWidgetItem( parents.last()); |
108 | 115 | ||
109 | itm_tmp->setText(0, QString(columnData.at(0).toString())); |
116 | itm_tmp->setText(0, QString(columnData.at(0).toString())); |
110 | if (columnData.at(1).toString() == "true") { |
117 | if (columnData.at(1).toString() == "true") { |
111 | itm_tmp->setCheckState(0,Qt::Checked); |
118 | itm_tmp->setCheckState(0,Qt::Checked); |
112 | }
|
119 | }
|
113 | else itm_tmp->setCheckState(0,Qt::Unchecked); |
120 | else itm_tmp->setCheckState(0,Qt::Unchecked); |
114 | itm_tmp->setText(1, QString(columnData.at(2).toString())); |
121 | itm_tmp->setText(1, QString(columnData.at(2).toString())); |
115 | itm_tmp->setText(2, QVariant(number).toString()); // íîìåð ñòðîêè â ôàéëå-ìîäåëè îòîáðàæåíèÿ |
122 | itm_tmp->setText(2, QVariant(number).toString()); // íîìåð ñòðîêè â ôàéëå-ìîäåëè îòîáðàæåíèÿ |
116 | itm_tmp->setText(3, QVariant(indentations.size()).toString()); |
123 | itm_tmp->setText(3, QVariant(indentations.size()).toString()); |
117 | }
|
124 | }
|
118 | 125 | ||
119 | number++; |
126 | number++; |
120 | }
|
127 | }
|
121 | }
|
128 | }
|
122 | 129 | ||
123 | 130 | ||
124 | /*
|
131 | /*
|
125 | void MainWindow::openBase()
|
132 | void MainWindow::openBase()
|
126 | {
|
133 | {
|
127 | 134 | ||
128 | db = QSqlDatabase::addDatabase("QMYSQL");
|
135 | db = QSqlDatabase::addDatabase("QMYSQL");
|
129 | pdb = &db;
|
136 | pdb = &db;
|
130 | raportFrm.pdb = &db;
|
137 | raportFrm.pdb = &db;
|
131 | 138 | ||
132 | pdb->setHostName(hostName);
|
139 | pdb->setHostName(hostName);
|
133 | pdb->setDatabaseName(baseName);
|
140 | pdb->setDatabaseName(baseName);
|
134 | pdb->setUserName(userName);
|
141 | pdb->setUserName(userName);
|
135 | pdb->setPassword(password);
|
142 | pdb->setPassword(password);
|
136 | bool ok = pdb->open();
|
143 | bool ok = pdb->open();
|
137 | if (!ok) {
|
144 | if (!ok) {
|
138 | QMessageBox::critical( // Äèàëîã ñ ñîîáùåíèåì îá îøèáêå.
|
145 | QMessageBox::critical( // Äèàëîã ñ ñîîáùåíèåì îá îøèáêå.
|
139 | this, // Ðîäèòåëüñêèé âèäæåò.
|
146 | this, // Ðîäèòåëüñêèé âèäæåò.
|
140 | QObject::tr("Database Error"), // Çàãîëîâîê.
|
147 | QObject::tr("Database Error"), // Çàãîëîâîê.
|
141 | pdb->lastError().text()); // Òåêñò ñîîáùåíèÿ.
|
148 | pdb->lastError().text()); // Òåêñò ñîîáùåíèÿ.
|
142 | }
|
149 | }
|
143 | if (ok)
|
150 | if (ok)
|
144 | {lineEdit-> insert(tr("Áàçà óñïåøíî îòêðûòà"));
|
151 | {lineEdit-> insert(tr("Áàçà óñïåøíî îòêðûòà"));
|
145 | }
|
152 | }
|
146 | else {lineEdit-> insert(tr("Áàçà íåäîñòóïíà. Êîä îøèáêè: "));
|
153 | else {lineEdit-> insert(tr("Áàçà íåäîñòóïíà. Êîä îøèáêè: "));
|
147 | lineEdit-> insert(pdb->lastError().text());
|
154 | lineEdit-> insert(pdb->lastError().text());
|
148 | }
|
155 | }
|
149 | 156 | ||
150 | 157 | ||
151 | 158 | ||
152 | model = new QSqlTableModel(this);
|
159 | model = new QSqlTableModel(this);
|
153 | model->setTable(tableName);
|
160 | model->setTable(tableName);
|
154 | 161 | ||
155 | model->setEditStrategy(QSqlTableModel::OnManualSubmit);
|
162 | model->setEditStrategy(QSqlTableModel::OnManualSubmit);
|
156 | model->setSort(0, Qt::AscendingOrder);
|
163 | model->setSort(0, Qt::AscendingOrder);
|
157 | 164 | ||
158 | model->select();
|
165 | model->select();
|
159 | 166 | ||
160 | 167 | ||
161 | QSqlField field(tr("age"), QVariant::Int);
|
168 | QSqlField field(tr("age"), QVariant::Int);
|
162 | field.setValue(QString(tr("123")));
|
169 | field.setValue(QString(tr("123")));
|
163 | 170 | ||
164 | bool okey;
|
171 | bool okey;
|
165 | int index;
|
172 | int index;
|
166 | QSqlRecord record;
|
173 | QSqlRecord record;
|
167 | 174 | ||
168 | 175 | ||
169 | tableView->setModel(model);
|
176 | tableView->setModel(model);
|
170 | view.setModel(model);
|
177 | view.setModel(model);
|
171 | 178 | ||
172 | 179 | ||
173 | tableView->setAlternatingRowColors(true);
|
180 | tableView->setAlternatingRowColors(true);
|
174 | view.setAlternatingRowColors(true);
|
181 | view.setAlternatingRowColors(true);
|
175 | 182 | ||
176 | tableView->resizeColumnsToContents();
|
183 | tableView->resizeColumnsToContents();
|
177 | 184 | ||
178 | 185 | ||
179 | view.resizeColumnsToContents();
|
186 | view.resizeColumnsToContents();
|
180 | 187 | ||
181 | tableView->show();
|
188 | tableView->show();
|
182 | 189 | ||
183 | 190 | ||
184 | 191 | ||
185 | initTreeWidget();
|
192 | initTreeWidget();
|
186 | }
|
193 | }
|
187 | */
|
194 | */
|
188 | 195 | ||
189 | 196 | ||
190 | void MainWindow::creatModelString(){ |
197 | void MainWindow::creatModelString(){ |
191 | 198 | ||
192 | MainWindow::modelString.clear(); // î÷èùàåì ñòðîêó ìîäåëè |
199 | MainWindow::modelString.clear(); // î÷èùàåì ñòðîêó ìîäåëè |
193 | // for (int i =0; i < rootItem1->childCount(); ++i )
|
200 | // for (int i =0; i < rootItem1->childCount(); ++i )
|
194 | 201 | ||
195 | {MainWindow::addChildsString(rootItem1, 0);} |
202 | {MainWindow::addChildsString(rootItem1, 0);} |
196 | 203 | ||
197 | int root_item_child_count; |
204 | int root_item_child_count; |
198 | int i; |
205 | int i; |
199 | root_item_child_count = rootItem1->childCount(); |
206 | root_item_child_count = rootItem1->childCount(); |
200 | i++; |
207 | i++; |
201 | 208 | ||
202 | }
|
209 | }
|
203 | 210 | ||
204 | 211 | ||
205 | void MainWindow::addChildsString(QTreeWidgetItem *parentItem, int level){ |
212 | void MainWindow::addChildsString(QTreeWidgetItem *parentItem, int level){ |
206 | int child_count; |
213 | int child_count; |
207 | child_count = parentItem->childCount(); |
214 | child_count = parentItem->childCount(); |
208 | for (int i=0; i < child_count; ++i){ |
215 | for (int i=0; i < child_count; ++i){ |
209 | for (int m=0; m < level; ++m){ |
216 | for (int m=0; m < level; ++m){ |
210 | modelString.append(tr(" ")); // ÷åòûðå ïðîáåëà - ïðèçíàê òîãî, ÷òî ýòî íîâûé óðîâåíü âëîæåíèÿ |
217 | modelString.append(tr(" ")); // ÷åòûðå ïðîáåëà - ïðèçíàê òîãî, ÷òî ýòî íîâûé óðîâåíü âëîæåíèÿ |
211 | }
|
218 | }
|
212 | modelString.append(parentItem->child(i)->text(0)); // â ïåðâîé êîëîíêå - èìÿ êëàññà |
219 | modelString.append(parentItem->child(i)->text(0)); // â ïåðâîé êîëîíêå - èìÿ êëàññà |
213 | modelString.append(tr("\t\t\t\t")); // ÷åòûðå òàáóëÿöèè - ïðèçíàê íîâîãî ñòîëáöà |
220 | modelString.append(tr("\t\t\t\t")); // ÷åòûðå òàáóëÿöèè - ïðèçíàê íîâîãî ñòîëáöà |
214 | if (parentItem->child(i)->checkState(0) == Qt::Checked) {modelString.append(tr("true"));} // åñëè ãàëî÷êà ñòîèò, çàíîñèì "true", ýëåìåíòû êëàññà â ïðåäñòàâëåíèè îòîáðàæàòüñÿ áóäóò |
221 | if (parentItem->child(i)->checkState(0) == Qt::Checked) {modelString.append(tr("true"));} // åñëè ãàëî÷êà ñòîèò, çàíîñèì "true", ýëåìåíòû êëàññà â ïðåäñòàâëåíèè îòîáðàæàòüñÿ áóäóò |
215 | else {modelString.append(tr("false"));} // åñëè êëàññ íå âûáðàí - çàíîñèì "false", ýëåìåíòû êëàññà â ïðåäñòàâëåíèè îòîáðàæàòüñÿ íå áóäóò |
222 | else {modelString.append(tr("false"));} // åñëè êëàññ íå âûáðàí - çàíîñèì "false", ýëåìåíòû êëàññà â ïðåäñòàâëåíèè îòîáðàæàòüñÿ íå áóäóò |
216 | modelString.append(tr("\t\t\t\t")); // ÷åòûðå òàáóëÿöèè - ïðèçíàê íîâîãî ñòîëáöà |
223 | modelString.append(tr("\t\t\t\t")); // ÷åòûðå òàáóëÿöèè - ïðèçíàê íîâîãî ñòîëáöà |
217 | modelString.append(parentItem->child(i)->text(1)); // âî âòîðîé êîëîíêå - ID êëàññà |
224 | modelString.append(parentItem->child(i)->text(1)); // âî âòîðîé êîëîíêå - ID êëàññà |
218 | modelString.append(tr("\n")); // êîíåö ñòðîêè |
225 | modelString.append(tr("\n")); // êîíåö ñòðîêè |
219 | if (parentItem->child(i)->childCount() > 0) { MainWindow::addChildsString(parentItem->child(i), level+1);} |
226 | if (parentItem->child(i)->childCount() > 0) { MainWindow::addChildsString(parentItem->child(i), level+1);} |
220 | 227 | ||
221 | }
|
228 | }
|
222 | int i; |
229 | int i; |
223 | i++; |
230 | i++; |
224 | }
|
231 | }
|
225 | 232 |