質問

I have a Qt application. When executed from the command line I can give a password as argument which is then sent by a QNetworkAccessManager to a server to check it, and the reply is handled by a QObject. After the network request is sent, the application launches a graphical interface.

I would like the graphical interface to launch only if the password is correct, so wait for the action of the QObject to finish.

I think I can only load the graphical interface from the main.cpp file, so I have to check the passwork in this file as well. The QObject could have called code from the main.cpp file but this file is not a class and cannot have methods.

What is the right way to go about it?

役に立ちましたか?

解決

You can easily launch your GUI from anywhere. For example:

class App_starter : public QObject {
  Q_OBJECT
public:
  App_starter(QString password) {
    //performing password check request here
  }

private slots:
  void response() {
    //read response
    if (password_ok) {
      Main_window* mw = new Main_window();
      mw->show();
    } else {
      QApplication::quit();
    }
  }
};

//main()
QApplication app;
App_starter starter(app.arguments()[1]);
return app.exec();

You can even make launcher don't know about GUI:

class App_starter : public QObject {
//...
private slots:
  void response() {
    //read response
    if (password_ok) {
      emit password_ok();
    } else {
      QApplication::quit();
    }
  }

signals:
  void password_ok();
};

//main()
QApplication app;
Main_window window;
App_starter starter(app.arguments()[1]);
connect(&starter, SIGNAL(password_ok()), &window, SLOT(show()));
return app.exec();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top