Редакция 2 | Редакция 4 | К новейшей редакции | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS
Редакция 2 | Редакция 3 | ||
---|---|---|---|
1 | #include <QtGui>
|
1 | #include <QtGui>
|
2 | #include <QtSql>
|
2 | #include <QtSql>
|
3 | #include "mainwindow.h"
|
3 | #include "mainwindow.h"
|
4 | #include "dbconfigdialog.h"
|
4 | #include "dbconfigdialog.h"
|
5 | 5 | ||
6 | MainWindow::MainWindow(QWidget *parent) |
6 | MainWindow::MainWindow(QWidget *parent) |
7 | : QMainWindow(parent) |
7 | : QMainWindow(parent) |
8 | {
|
8 | {
|
9 | QLabel *dbLabel = new QLabel("Database"); |
9 | QLabel *dbLabel = new QLabel("Database"); |
10 | QComboBox *dbCombo = new QComboBox; |
10 | QComboBox *dbCombo = new QComboBox; |
11 | dbCombo->addItem("default"); |
11 | dbCombo->addItem("default"); |
12 | dbCombo->setEnabled(false); |
12 | dbCombo->setEnabled(false); |
13 | QPushButton *dbPrefButton = new QPushButton("Config"); |
13 | QPushButton *dbPrefButton = new QPushButton("Config"); |
14 | QPushButton *dbConnectButton = new QPushButton("Connect"); |
14 | QPushButton *dbConnectButton = new QPushButton("Connect"); |
15 | QHBoxLayout *dbLayout = new QHBoxLayout; |
15 | QHBoxLayout *dbLayout = new QHBoxLayout; |
16 | dbLayout->addWidget(dbLabel); |
16 | dbLayout->addWidget(dbLabel); |
17 | dbLayout->addWidget(dbCombo); |
17 | dbLayout->addWidget(dbCombo); |
18 | dbLayout->addWidget(dbPrefButton); |
18 | dbLayout->addWidget(dbPrefButton); |
19 | dbLayout->addWidget(dbConnectButton); |
19 | dbLayout->addWidget(dbConnectButton); |
20 | 20 | ||
21 | queryEdit = new QTextEdit; |
21 | queryEdit = new QTextEdit; |
22 | QPushButton *queryExecButton = new QPushButton("Exec"); |
22 | QPushButton *queryExecButton = new QPushButton("Exec"); |
23 | QHBoxLayout *queryLayout = new QHBoxLayout; |
23 | QHBoxLayout *queryLayout = new QHBoxLayout; |
24 | queryLayout->addWidget(queryEdit); |
24 | queryLayout->addWidget(queryEdit); |
25 | queryLayout->addWidget(queryExecButton); |
25 | queryLayout->addWidget(queryExecButton); |
26 | 26 | ||
27 | resultTable = new QTableView; |
27 | resultTable = new QTableView; |
28 | resultModel = new QSqlQueryModel; |
28 | resultModel = new QSqlQueryModel; |
29 | 29 | ||
30 | QWidget *mainWidget = new QWidget; |
30 | QWidget *mainWidget = new QWidget; |
31 | QVBoxLayout *mainLayout = new QVBoxLayout; |
31 | QVBoxLayout *mainLayout = new QVBoxLayout; |
32 | mainLayout->addLayout(dbLayout); |
32 | mainLayout->addLayout(dbLayout); |
33 | mainLayout->addLayout(queryLayout); |
33 | mainLayout->addLayout(queryLayout); |
34 | mainLayout->addWidget(resultTable); |
34 | mainLayout->addWidget(resultTable); |
35 | 35 | ||
36 | mainWidget->setLayout(mainLayout); |
36 | mainWidget->setLayout(mainLayout); |
37 | setCentralWidget(mainWidget); |
37 | setCentralWidget(mainWidget); |
38 | 38 | ||
39 | connect(queryExecButton,SIGNAL(clicked()),this,SLOT(queryExec())); |
39 | connect(queryExecButton,SIGNAL(clicked()),this,SLOT(queryExec())); |
40 | connect(dbPrefButton, SIGNAL(clicked()),this,SLOT(settingsDB())); |
40 | connect(dbPrefButton, SIGNAL(clicked()),this,SLOT(settingsDB())); |
41 | connect(dbConnectButton, SIGNAL(clicked()),this,SLOT(connectDB())); |
41 | connect(dbConnectButton, SIGNAL(clicked()),this,SLOT(connectDB())); |
- | 42 | statusBar()->showMessage(tr("Ready")); |
|
42 | }
|
43 | }
|
43 | 44 | ||
44 | MainWindow::~MainWindow() |
45 | MainWindow::~MainWindow() |
45 | {
|
46 | {
|
46 | 47 | ||
47 | }
|
48 | }
|
48 | 49 | ||
49 | void MainWindow::queryExec() |
50 | void MainWindow::queryExec() |
50 | {
|
51 | {
|
51 | resultModel->setQuery(queryEdit->toPlainText()); |
52 | resultModel->setQuery(queryEdit->toPlainText()); |
52 | resultTable->setModel(resultModel); |
53 | resultTable->setModel(resultModel); |
53 | resultTable->show(); |
54 | resultTable->show(); |
- | 55 | statusBar()->showMessage(resultModel->lastError().text()); |
|
54 | }
|
56 | }
|
55 | 57 | ||
56 | void MainWindow::settingsDB() |
58 | void MainWindow::settingsDB() |
57 | {
|
59 | {
|
58 | DbConfigDialog *dbc = new DbConfigDialog(); |
60 | DbConfigDialog *dbc = new DbConfigDialog(); |
59 | dbc->exec(); |
61 | dbc->exec(); |
60 | }
|
62 | }
|
61 | 63 | ||
62 | void MainWindow::connectDB() |
64 | void MainWindow::connectDB() |
63 | {
|
65 | {
|
64 | QSettings settings;
|
66 | QSettings settings;
|
65 | 67 | ||
66 | QSqlDatabase db = QSqlDatabase::addDatabase(settings.value("DB/driver").toString()); |
68 | QSqlDatabase db = QSqlDatabase::addDatabase(settings.value("DB/driver").toString()); |
67 | db.setDatabaseName(settings.value("DB/database").toString()); |
69 | db.setDatabaseName(settings.value("DB/database").toString()); |
68 | if (!db.open()) |
70 | if (!db.open()) |
69 | QMessageBox::critical(this, tr("Connection error"), tr("Can't open database\n%1").arg(db.lastError().text()), QMessageBox::Ok); |
71 | QMessageBox::critical(this, tr("Connection error"), tr("Can't open database\n%1").arg(db.lastError().text()), QMessageBox::Ok); |
70 | else
|
72 | else
|
71 | QMessageBox::information(this,tr("Connection complete"), tr("Connection complete!"),QMessageBox::Ok); |
73 | QMessageBox::information(this,tr("Connection complete"), tr("Connection complete!"),QMessageBox::Ok); |
72 | }
|
74 | }
|
73 | 75 |