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