CB connection settings dialog
We will store the connection settings using QSettings in a text file, i.e. using QSettings::IniFormat. In the DataBases class, add the bool connectCenralDB() method in which we implement the connection.
If the connection attempt is unsuccessful, we call the connection settings dialog ConnectionSettingDialog in which it is possible to change the settings.
connectionsettingdialog.h
#ifndef CONNECTIONSETTINGDIALOG_H #define CONNECTIONSETTINGDIALOG_H #include <QDialog> namespace Ui { class ConnectionSettingDialog; } class ConnectionSettingDialog : public QDialog { Q_OBJECT public: explicit ConnectionSettingDialog(QWidget *parent = nullptr); ~ConnectionSettingDialog(); private slots: void on_buttonBox_accepted(); void on_buttonBox_rejected(); private: Ui::ConnectionSettingDialog *ui; private: void readConnData(); //Чтение настроек подключения void saveConnData(); //Запись настроек подключения }; #endif // CONNECTIONSETTINGDIALOG_H
connectionsettingdialog.cpp
#include "connectionsettingdialog.h" #include "ui_connectionsettingdialog.h" #include "LoggingCategories/loggingcategories.h" #include "DataBases/databasesettings.h" #include <QSettings> ConnectionSettingDialog::ConnectionSettingDialog(QWidget *parent) : QDialog(parent), ui(new Ui::ConnectionSettingDialog) { ui->setupUi(this); readConnData(); qInfo(logInfo()) << "Открытие диалога настройки подключения."; } ConnectionSettingDialog::~ConnectionSettingDialog() { delete ui; } void ConnectionSettingDialog::readConnData() { QSettings settings(DataBaseSettings::CONFIG_FILE_NAME,QSettings::IniFormat); settings.beginGroup("DATABASE"); ui->lineEditServer->setText(settings.value("HostName").toString()); ui->lineEditDatabase->setText(settings.value("DataBase").toString()); ui->lineEditUser->setText(settings.value("User").toString()); ui->lineEditPassword->setText(settings.value("Password").toString()); settings.endGroup(); } void ConnectionSettingDialog::saveConnData() { QSettings settings(DataBaseSettings::CONFIG_FILE_NAME,QSettings::IniFormat); settings.beginGroup("DATABASE"); settings.setValue("HostName",ui->lineEditServer->text().trimmed()); settings.setValue("DataBase",ui->lineEditDatabase->text().trimmed()); settings.setValue("User",ui->lineEditUser->text().trimmed()); settings.setValue("Password",ui->lineEditPassword->text().trimmed()); settings.endGroup(); } void ConnectionSettingDialog::on_buttonBox_accepted() { saveConnData(); this->accept(); } void ConnectionSettingDialog::on_buttonBox_rejected() { this->reject(); }
Mетод bool connectCenralDB()
bool DataBases::connectCenralDB() { bool result = true; //Указываем имя файла настроек QSettings settings(DataBaseSettings::CONFIG_FILE_NAME, QSettings::IniFormat); //Создаем подключение по умолчанию к базе данных FireBird QSqlDatabase db = QSqlDatabase::addDatabase("QIBASE"); //Установка параметров подключения из файла настроек settings.beginGroup("DATABASE"); db.setHostName(settings.value("HostName").toString()); db.setDatabaseName(settings.value("DataBase").toString()); db.setUserName(settings.value("User").toString()); db.setPassword(settings.value("Password").toString()); settings.endGroup(); //Открываем базу данных, если попытка не удачная вызываем диалог настроек подключения if(!db.open()) { qCritical(logCritical()) << "Не возможно подключиться к базе данных." << endl << "Причина:" << db.lastError().text(); int rez = QMessageBox::question(nullptr, QObject::tr("Ошибка подключения"), "Не могу установить соединение с базой данных.\nПроверить настройки подключения?", QMessageBox::Yes | QMessageBox::No); if(rez == QMessageBox::Yes) { ConnectionSettingDialog *connDlg = new ConnectionSettingDialog(); int result = connDlg->exec(); if(result == QDialog::Accepted) { qInfo(logInfo()) << "Сохранение настроек подключения."; } } //Завершаем работу result = false; } else { qInfo(logInfo()) << "Успешное подключение к центральной базе данных."; result = true; } return result; }
In main.cpp, before calling the main window, add
if(!db->connectCenralDB()){ qInfo(logInfo()) << "Завершение работы. Не удалось подключится к центральной базе."; return 1; }
We launch the application.
The message appears
After clicking the Yes button, the connection settings window will be displayed. We fill it.
The program is closed. A configuration file appears in the project's build directory.
iMpos.config
[DATABASE] HostName=192.168.123.11 DataBase=sunoil User=SYSDBA Password=sunoil00
When you follow the start, the settings are read and connected to the central database.
Project archive:
Такой вид диалоговых окон на десятой винде?
Да, такой вид.