Hi all, I just started qt, so I don't have enough knowledge.
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
connectDatabase();
}
void MainWindow::connectDatabase()
{
QFile databaseFile
("database.db");
if (!databaseFile.exists()) {
connectionError();
} else
{
db.setHostName("localhost");
db.setDatabaseName("database.db");
if (!db.open()) {
connectionError();
}
}
}
void MainWindow::connectionError()
{
msgBox.setWindowTitle("Connection Error");
msgBox.setText("Program cannot connect the database file which is required to run this software. Would you like to download default database file? Please note that all of the saved process data will be removed.");
int selection = msgBox.exec();
//restore data
} else {
MainWindow::close();
}
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connectDatabase();
}
void MainWindow::connectDatabase()
{
QFile databaseFile("database.db");
if (!databaseFile.exists()) {
connectionError();
} else
{
db.setHostName("localhost");
db.setDatabaseName("database.db");
if (!db.open()) {
connectionError();
}
}
}
void MainWindow::connectionError()
{
QMessageBox msgBox;
msgBox.addButton(QMessageBox::Yes);
msgBox.addButton(QMessageBox::No);
msgBox.setWindowTitle("Connection Error");
msgBox.setIcon(QMessageBox::Critical);
msgBox.setText("Program cannot connect the database file which is required to run this software. Would you like to download default database file? Please note that all of the saved process data will be removed.");
int selection = msgBox.exec();
if (selection == QMessageBox::Yes) {
//restore data
} else {
MainWindow::close();
}
}
To copy to clipboard, switch view to plain text mode
This is what I did. When I delete the database file to see connectionError function, messagebox comes before the MainWindow and MainWindow::close() doesnt work. What should I do?
Bookmarks