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