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