-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnectiondialog.cpp
executable file
·141 lines (108 loc) · 4.92 KB
/
connectiondialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "connectiondialog.h"
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QDialogButtonBox>
#include <QGridLayout>
#include <QtSql>
#include <QDebug>
#include <QMessageBox>
//--------------------------------------------------------------------------------------
ConnectionDialog::ConnectionDialog(QWidget *parent) :
QDialog(parent)
{
setModal(true);
setWindowTitle(tr("Настройки соединения"));
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
buttonOk = new QPushButton(tr("Ok"));
buttonCancel = new QPushButton(tr("Отмена"));
buttonCheckConnection = new QPushButton(tr("Проверить соединение"));
hostLabel = new QLabel(tr("Хост:"));
portLabel = new QLabel(tr("Порт:"));
dbnameLabel = new QLabel(tr("База данных:"));
userLabel = new QLabel(tr("Логин:"));
passwordLabel = new QLabel(tr("Пароль:"));
hostEdit = new QLineEdit;
portEdit = new QLineEdit;
dbnameEdit = new QLineEdit;
userEdit = new QLineEdit;
passwordEdit = new QLineEdit;
QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal);
buttonBox->addButton(buttonOk, QDialogButtonBox::ActionRole);
buttonBox->addButton(buttonCancel, QDialogButtonBox::ActionRole);
QGridLayout *gridLayout = new QGridLayout;
gridLayout->addWidget(hostLabel, 0, 0);
gridLayout->addWidget(hostEdit, 0, 1);
gridLayout->addWidget(portLabel, 1, 0);
gridLayout->addWidget(portEdit, 1, 1);
gridLayout->addWidget(dbnameLabel, 2, 0);
gridLayout->addWidget(dbnameEdit, 2, 1);
gridLayout->addWidget(userLabel, 3, 0);
gridLayout->addWidget(userEdit, 3, 1);
gridLayout->addWidget(passwordLabel, 4, 0);
gridLayout->addWidget(passwordEdit, 4, 1);
gridLayout->addWidget(buttonCheckConnection, 5, 0, 1, 5, Qt::AlignCenter);
QGridLayout *mainLayout = new QGridLayout;
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
mainLayout->addLayout(gridLayout, 1, 0, Qt::AlignCenter);
mainLayout->addWidget(buttonBox, 2, 0, Qt::AlignCenter);
setLayout(mainLayout);
connect(buttonOk, SIGNAL(clicked()), this, SLOT(accept()));
connect(buttonCancel, SIGNAL(clicked()), this, SLOT(reject()));
connect(buttonCheckConnection, SIGNAL(clicked()), this, SLOT(checkConnection()));
buttonOk->setDefault(true);
}
//--------------------------------------------------------------------------------------
void ConnectionDialog::checkConnection()
{
setEnabled(false);
repaint();
QSqlDatabase db = QSqlDatabase::database();
db.setHostName(host());
db.setPort(port());
db.setDatabaseName(dbname());
db.setUserName(user());
db.setPassword(password());
QMessageBox msg(this);
if (db.open()) {
msg.setText(tr("Соединение с бд успешно установлено."));
msg.setIcon(QMessageBox::Information);
} else {
msg.setText(QString(tr("Ошибка установления соединения с бд.\nПричина:\n%1")).arg(db.lastError().text()));
msg.setIcon(QMessageBox::Critical);
}
msg.exec();
db.close();
setEnabled(true);
repaint();
}
//--------------------------------------------------------------------------------------
const QString ConnectionDialog::host() const
{ return hostEdit->text(); }
//--------------------------------------------------------------------------------------
int ConnectionDialog::port() const
{ return portEdit->text().toInt(); }
//--------------------------------------------------------------------------------------
const QString ConnectionDialog::dbname() const
{ return dbnameEdit->text(); }
//--------------------------------------------------------------------------------------
const QString ConnectionDialog::user() const
{ return userEdit->text(); }
//--------------------------------------------------------------------------------------
const QString ConnectionDialog::password() const
{ return passwordEdit->text(); }
//--------------------------------------------------------------------------------------
void ConnectionDialog::setHost(const QString &str)
{ hostEdit->setText(str); }
//--------------------------------------------------------------------------------------
void ConnectionDialog::setPort(int port)
{ portEdit->setText(QString().number(port)); }
//--------------------------------------------------------------------------------------
void ConnectionDialog::setDBName(const QString &str)
{ dbnameEdit->setText(str); }
//--------------------------------------------------------------------------------------
void ConnectionDialog::setUser(const QString &str)
{ userEdit->setText(str); }
//--------------------------------------------------------------------------------------
void ConnectionDialog::setPassword(const QString &str)
{ passwordEdit->setText(str); }