Adding a form that appears before the main window



  • Ihave already created my UI and it runs fine. However i want to add a form that appears before the main window when starting the program...sort of like a form that asks for a password for opening the app. How do I do this?


  • Moderators

    There are many ways to accomplish that. You can run the new form from main.cpp before executing the main window. Or you can include running of this form in main window's constructor. Take a look at QDialog and friends (or, if you don't need interaction, QSplashScreen).



  • You may achieve this by showing a customized QDialog (to input user credentials) in main window constructor to get the password and based on that you can proceed further.



  • I would not do that in the constructor, I would do that with a custom modal dialog directly in main.



  • Dear all,

    I am newbie to Qt. I am using 4.8.3, Windows 7, QtCreator 2.6. Heavily using QtCreator to design a user interface.

    Is there any tutorial to build a modal dialog (for login) using QtCreator 2.6, after I create a mainwindow.ui?
    Do I create login.ui (login.cpp and login.h) first, then include them into myproject.pro?
    How do I pass variable/ value between those cpp program/files?

    Thank you very much in advance.


  • Moderators

    [quote author="yupifourbi" date="1353303780"]Do I create login.ui (login.cpp and login.h) first, then include them into myproject.pro?
    How do I pass variable/ value between those cpp program/files?[/quote]

    Again, there are many valid ways to do it. Yeah, you can create the login.ui + c++ class in your project. Then, one solution would be to instantiate and run the dialog in main.cpp, before the MainWindow is shown:
    @
    Login log(0);
    if (log.exec() != QDialog::Accepted) {
    return 1; // Error, for example
    } else {
    MainWindow mw(0);
    mw.show();
    }

    return app.exec();
    @

    Although now that I wrote it I do have some doubts. Well, you can always try it out and report back if it fails.



  • Dear all,

    Here is my code (heavily created using QtCreator).

    main.cpp
    @
    #include "mainwindow.h"
    #include "login.h"

    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    Login log(0);
    if (log.exec&#40;&#41; != QDialog::Accepted) {
      return 1; // Error, for example
    } else {
      MainWindow m(0);
      m.show();
    }
    
    return a.exec&#40;&#41;;                
    

    }
    @

    login.h
    @
    #ifndef LOGIN_H
    #define LOGIN_H

    #include <QDialog>

    namespace Ui {
    class Login;
    }

    class Login : public QDialog
    {
    Q_OBJECT

    public:
    explicit Login(QWidget *parent = 0);
    int checkLogin();
    ~Login();

    private:
    Ui::Login *ui;
    };

    #endif // LOGIN_H

    @

    login.cpp
    @
    #include <QtGui>
    #include "login.h"
    #include "ui_login.h"

    Login::Login(QWidget parent) :
    QDialog(parent),
    ui (new Ui::Login)
    {
    ui->setupUi(this);
    /
    Connecting push button pbLogin --> checklogin() */
    QObject::connect(ui->pbLogin, SIGNAL(activated()), this, SLOT(checkLogin()));

    }

    int Login::checkLogin()
    {
    // pengguna = username.
    // katakunci = password.

    int _cl =1;
    if ( (!ui->lePengguna->text().isEmpty()) &&
        (!ui->leKatakunci->text().isEmpty())) {
            if((ui->lePengguna->text()=="admin") && (ui->leKatakunci->text()=="1234")) {
                QMessageBox::about(this, tr("Login success"), tr("<p>Welcome...</p>"));
                _cl = 0;
            } else {
                QMessageBox::about(this, tr("Login unsuccess"), tr("<p>Wrong password</p>"));
                _cl = 1;
            }
     }
     return _cl;
    

    }

    Login::~Login()
    {
    delete ui;
    }
    @

    mainwindow.cpp
    @
    #include <QtGui>

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this); /
    MainWindow() /
    /
    connecting menu About --> tampilkan About() */
    QObject::connect(ui->actionSiapa_Dimana, SIGNAL(activated()), this, SLOT(about()));

    }

    void MainWindow::about()
    {
    QMessageBox::about(this, tr("About UP4B - Siapa Dimana"),
    tr("<p>Versi 0.01 skeleton.</p>"));
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    @

    mainwindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    public slots:
    void about();

    private:
    Ui::MainWindow *ui;

    };

    #endif // MAINWINDOW_H
    @

    The problems are:

    1. When I run the program, it shows Login window as expected.

    2. After I entry admin (at lePengguna) and "anything" (at leKatakunci)
      whether the password is correct or not the program disappeared without stopping at any QMessageBox, but it still running in the background, I must force quit the program.

    3. I would like also to ask: how to change the "ui name" in the login.cpp (that created by designer in QtCreator). Can I just type different name directly in coding pane? What is the parameter at designer in QtCreator? Because I am still confuse differentiating between ui of mainwindow and ui of login.

    Please help. Thanks in advance.


  • Moderators

    Oh it's in my code, probably one of the reasons I was in doubt.
    @
    Login log(0);
    if (log.exec() != QDialog::Accepted) {
    a.quit();
    // or this:
    a.metaObject().invokeMethod(&a, "quit", Qt::QueuedConnection);
    } else {
    MainWindow m(0);
    m.show();
    }

    return a.exec&#40;&#41;;
    

    @



  • Dear Sierdzio,

    I modified with line #3, still no luck.

    I just found out that in the Signals & Slots editor, there is
    pbLogin, clicked(), Login, accept().

    However, when I deleted that signals & slots editor, now the pbLogin not working at all.
    Is connect at login.cpp, line 9-11 failed??
    @
    ui->setupUi(this);
    /* Connecting push button pbLogin --> checklogin() */
    QObject::connect(ui->pbLogin, SIGNAL(clicked()), this, SLOT(checkLogin()));
    @

    From my debugger said:
    @
    Debugging starts
    Object::connect: No such slot Login::checkLogin()
    Object::connect: (sender name: 'pbLogin')
    Object::connect: (receiver name: 'Login')
    @

    previously I create void checkLogin () as slot.
    But I confuse with return code so I change it as function int checkLogin();

    Should I use emit ? How to accomplished that?

    Thanks in advance.


  • Moderators

    Well, the message is clear. QPushButton::activated() does not exist. Use ::clicked() or ::toggled() instead.



  • I change int checkLogin() as public slots.

    Now my login.h
    @
    #ifndef LOGIN_H
    #define LOGIN_H

    #include <QDialog>

    namespace Ui {
    class Login;
    }

    class Login : public QDialog
    {
    Q_OBJECT

    public:
    explicit Login(QWidget *parent = 0);
    ~Login();

    public slots:
    int checkLogin();

    private:
    Ui::Login *ui;
    };

    #endif // LOGIN_H
    @

    The checkLogin is called and working now.

    When success login with admin and 1234, QMessageBox "Welcome..." is displayed. But the mainwindow did not appear...

    When not success login, QMessageBox "Wrong password." is displayed. The login window and mainwindow disappeared, but the program is still running.



  • Dear all,

    Here my main.cpp
    @
    #include "mainwindow.h"
    #include "login.h"

    #include <QApplication>
    #include <QMessageBox>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QMessageBox msgBox;

    Login log(0);
    if (log.exec&#40;&#41; != QDialog::Accepted) {
        msgBox.setText("Will stop the application");
        msgBox.exec&#40;&#41;;
        a.quit(); // BUT, the program is still running. Need to force quit.
    } else {
        msgBox.setText("will call the MainWindow");
        msgBox.exec&#40;&#41;;
        MainWindow m; 
        m.show();  // BUT, there is no MainWindow ?! 
    }
    
    msgBox.setText("Will execute a.exec&#40;&#41;");
    msgBox.exec&#40;&#41;;
    return a.exec&#40;&#41;;
    

    }
    @

    I try:
    a. wrong username and password combination. The program shows "Will stop the application.", but the program is still running.

    b. correct combination. The program shows "will call the MainWindow", but the mainwindow only flashed once the gone.

    Both a and b, then program continue to display "will execute a.exec()".

    Is it because there are many files (mainwindow.cpp, login.cpp), instead altogether in 1 file mainwindow?

    Still don't know how to solve it. Please help.


  • Moderators

    App is probably not quitting because quit() is called too early (before the event loop kicks in in a.exec()).

    The MainWindow flash could be due to the fact that you instantiate it inside the block (line 20) and then it goes out of scope in line 22 and is destroyed. Declare the MW outside the block or as a pointer.



  • Dear Sierdzio my man!

    my main.cpp now works as charm, million thanks.
    @
    #include "mainwindow.h"
    #include "login.h"

    #include <QApplication>
    #include <QMessageBox>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    QMessageBox msgBox;
    
    MainWindow m;
    m.show();
    
    Login log(0);
    if (log.exec&#40;&#41; != QDialog::Accepted) {
        msgBox.setText("Wrong credential. The program will stop.");
        msgBox.exec&#40;&#41;;
        a.quit();
    } else {
        msgBox.setText("Ok. Let's go baby!");
        msgBox.exec&#40;&#41;;
        a.exec&#40;&#41;;
    }
    
    return 0;
    

    }
    @

    Is it ok to say just 'return 0;'? As I don't pass anything else after the App/ program ends.
    @
    return 0;
    @


  • Moderators

    [quote author="yupifourbi" date="1353399194"]
    Is it ok to say just 'return 0;'? As I don't pass anything else after the App/ program ends.[/quote]

    Interesting approach. It's probably alright, if the dialog works OK, then it should be fine.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.