Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. 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?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Taz742T Offline
    Taz742T Offline
    Taz742
    wrote on last edited by Taz742
    #1

    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();
    

    }

    Do what you want.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Taz742T 1 Reply Last reply
      0
      • mrjjM mrjj

        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

        Taz742T Offline
        Taz742T Offline
        Taz742
        wrote on last edited by
        #3

        @mrjj

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

        Do what you want.

        mrjjM 1 Reply Last reply
        0
        • Taz742T Taz742

          @mrjj

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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 ?

          Taz742T 1 Reply Last reply
          0
          • mrjjM mrjj

            @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 ?

            Taz742T Offline
            Taz742T Offline
            Taz742
            wrote on last edited by
            #5

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

            Do what you want.

            mrjjM 1 Reply Last reply
            0
            • Taz742T Taz742

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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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
              }

              Taz742T 1 Reply Last reply
              0
              • mrjjM mrjj

                @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
                }

                Taz742T Offline
                Taz742T Offline
                Taz742
                wrote on last edited by
                #7

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

                Do what you want.

                mrjjM 1 Reply Last reply
                0
                • Taz742T Taz742

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

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved