O
O
Oleg Seledets2018-07-02 15:47:03
Qt
Oleg Seledets, 2018-07-02 15:47:03

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 constructor
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    /* код на подключение БД и настроек*/
}
constructor
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);
}
transition from authorization to mainwindow
mainW->show(); //показать окно регистрации
        this->close(); // Закрываем основное окно
main.cpp
#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

1 answer(s)
M
Mercury13, 2018-07-02
@oleja1ee7

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 {
      // сообщи об ошибке
   }
}

I see a side effect of this (but maybe you don’t need to get rid of it) - on the taskbar, the animation changes from the button with the authorization dialog to the button with the main window.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question