How To Close QDialog and Show MainWindow When User username and password is correct?



  • How To Close QDialog and Show MainWindow When User username and password is correct?

    this is my dialog:

    #include "user_panel.h"
    #include "ui_user_panel.h"
    #include "dal.h"
    #include "QSql"
    #include "QSqlDatabase"
    #include "QDebug"
    #include "mainwindow.h"
    #include "user_all.h"

    user_panel::user_panel(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::user_panel)
    {
    ui->setupUi(this);

    // this->setWindowFlags(Qt::CustomizeWindowHint);
    }

    user_panel::~user_panel()
    {
    delete ui;
    }

    void user_panel::on_login_clicked()
    {
    QString user ="", pass ="";

    user = ui->username->text();
    
    pass = ui->password->text();
    
    user_all* me = new user_all();
    
    if(me->user_searched(user,pass)){
        qDebug() << "YES";
    }else{
        qDebug() << "NO";
    }
    

    }

    void user_panel::on_quitt_clicked()
    {
    this->close();
    }

    this is my function, in my new user_all class:
    bool user_all::user_searched(QString user, QString pass){

    DAL* connect_dal = new DAL();
    
    QSqlQuery query;
    
    int count = 0;
    
    if(query.exec("SELECT * FROM users WHERE USERNAME='"+user+"' AND PASSWORD='"+pass+"'")){
        while(query.next())
            count++;
    
        if(!count){
            return false;
        }else{
            return true;
        }
    }
    

    }

    this main.cpp :

    #include "mainwindow.h"
    #include <QApplication>
    #include "user_panel.h"
    #include "user_all.h"
    #include "QDebug"
    #include "QPair"

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

    MainWindow w;
    
    user_panel* panel = new user_panel();
    
    panel->setModal(true);
    
    if(panel->exec() == QDialog::Rejected){
        panel->close();
    }else{
        // some code when is Dialog is Accepted
        w.show();
    }
    
    return a.exec();
    

    }


  • Qt Champions 2016

    Hi
    Seems ok
    What happens and what is not working?

    if(panel->exec() == QDialog::Rejected){
    panel->close();
    }else{
    // some code when is Dialog is Accepted
    w.show();
    }

    Should show login panel
    and then mainwin



  • @mrjj

    I can not understand what happened to panel.exec() is accepted :/


  • Qt Champions 2016

    @Taz742

    the exec() runs a local event loop and stay there as long as dialog is shown.
    The exec() returns the dialog result. Accepted or rejected.
    So you can use that to indicate if login was ok or not.
    So inside code when you check password simply use
    QDialog::accept() or QDialog::reject().

    What part is unclear to you ?



  • @mrjj
    :( I could not understand. You can write an example? plz


  • Qt Champions 2016

    @Taz742
    sample of ?
    But u have the code?

    its just ( in Login)
    if ( PassWordOK() )
    QDialog::accept();
    else
    QDialog::reject().

    and in main

    --
    MainWindow w;

    user_panel panel; // NO need to new it
    panel.setModal(true);

    if(panel.exec() == QDialog::Accepted){
    panel.close(); // does it need this? Should be closed here.
    w.show(); // show main
    }else{
    // password not ok
    return 1; // leave dont start app
    }



  • @mrjj
    its worked and SOLVED :)) , thank you man.


  • Qt Champions 2016

    @Taz742
    Super :)
    You can use QMessageBox if you want to inform the user that
    no app is started as login was invalid.


Log in to reply
 

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