Answer the question
In order to leave comments, you need to log in
Why is the mainwindow.cpp constructor run first?
Hello.
there is an autoriz file that should be launched first, and when you click on the button near its form, mainwindow opens.
But at startup, the mainwindow constructor is loaded first. how to edit it or is it possible to hang up the events that are in the mainwindow constructor (connecting to the database and loading settings from QSettings) in any event when the form is opened??
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
/* код на подключение БД и настроек*/
}
autoriz::autoriz(QDialog *parent) :
QDialog(parent),
ui(new Ui::autoriz)
{
ui->setupUi(this);
QTextCodec::setCodecForLocale(QTextCodec::codecForName("Windows-1251"));
mainW = new MainWindow(); // Инициализируем окно для диспетчера
// подключаем к слоту запуска окна авторизация по кнопке в окне диспетчера
connect(mainW, &MainWindow::firstWindow, this, &autoriz::show);
}
mainW->show(); //показать окно регистрации
this->close(); // Закрываем основное окно
#include "autoriz.h" //авторизация
#include "ui_autoriz.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
autoriz au;
au.show();
return a.exec();
}
Answer the question
In order to leave comments, you need to log in
I would rewrite the architecture of the program.
1. You have a modal window in front of you. So call it as a modal - au.exec ().
2. To check that the authorization is correct, you need to connect. So make a non-visual "connection" object and drag it into exec. Well, or create in exec.
3. And there collect the main program.
QApplication a(argc, argv);
FmAutoriz au;
std::unique_ptr<SomeConnection> connection = au.exec(); // SomeConnection и exec придётся написать
if (connection) {
FmMain m(connection);
m.show();
return a.exec();
} else {
return 0;
}
std::unique_ptr<SomeConnection>FmAutoriz::exec()
{
bool b = QDialog::exec();
if (b) {
return std::move(this->connection);
// connection — поле FmAutoriz.
// Такой же unique_ptr, в обработчиках кнопок пытаемся создать соединение.
} else {
return nullptr;
}
}
void FmAutorize_on_btOk_click()
{
connection = new SomeConnection();
if (connection.connect(someLogin, somePassword)) {
accept();
} else {
// сообщи об ошибке
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question