[SOLVED] Need help in solving multiple clicking of dialog boxes



  • I have a login box where the checking is done in main.cpp. The values for user and password will be passed from login.cpp to main.cpp and then back to login.cpp. Message box will pop up upon clicking the button but I have to click at least 3-4 times before it goes away! Can someone help to debug on this?

    login.cpp:
    @

    void Login::on_login_clicked()
    {

    QString user = ui->username->text();
    QString passwd = ui->password->text();
    

    if ((!ui->username->text().isEmpty()) && (!ui->password->text().isEmpty()))
    {

        //call checking from main.cpp
        MainWindow mw;
        mw.checkLogin(user, passwd);
    
        if (mw.checkLogin(user, passwd) == 0)
        {
            qDebug() << "Success! Close dialog box";
            QDialog::close();
        }
    }
    else
     {
        qDebug() << "Invalid credential provided!";
     }
    

    }
    @

    main.cpp:
    @
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    MainWindow w;
    w.showMaximized();
    w.show();
    
    
    Login MyLogIn;
    

    //i seriously don't know what this is for but it needs to be there for the login form to open
    if (MyLogIn.exec() == QDialog::Accepted)
    {

    }
    

    return a.exec();

    }

    int MainWindow::checkLogin(QString a, QString b)
    {
    QMessageBox msgBox;
    QString aa = a;
    QString bb = b;

    if((aa=="admin") && (bb=="1234"))
    {
         qDebug() << "Login success!";
         msgBox.setText("Login success!"); //i have to click this like 3-4 times before it goes away!
         msgBox.exec&#40;&#41;;
         return 0;
    }
    else
    {
        qDebug(&#41; << "Login FAILED!";
        msgBox.setText("Login FAILED!"); //same with this
        msgBox.exec(&#41;;
    
        return 1;
    }
    

    }
    @



  • You call checkLogin twice so you have message box twice.
    @
    mw.checkLogin(user, passwd); // <-- First message box

    if (mw.checkLogin(user, passwd) == 0) // <-- Second
    @

    Also you create MainWindow twice.
    It is not a probem now, but later if the MainWindow will have some complicated logic it will be a big problem.

    And you don't need to show a window twice
    @
    w.showMaximized(); // <-- this line is enough
    w.show();
    @



  • Hi,
    IMHO Please, please, please, first read a good C++ book. Then a good Qt book and then start programming.
    You posted code before because you didn't understand the dialog stuff, this is almost the same.



  • Thanks for the help, appreciate it.


Log in to reply
 

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