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. [SOLVED] Login check in main.cpp
QtWS25 Last Chance

[SOLVED] Login check in main.cpp

Scheduled Pinned Locked Moved General and Desktop
13 Posts 3 Posters 4.0k Views
  • 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.
  • S Offline
    S Offline
    sharon_obl82
    wrote on last edited by
    #1

    Hi, I have a mainwindow.ui and then I created a login.ui to prompt for login and password before allowing the mainwindow to be displayed. I checked for verification in login.cpp file but now I want to do the checking on main.cpp instead. I am very confused with the class declaration etc... when I click the button on login.ui form, how do i pass it to main.cpp for checking?

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

      Hi,

      So you want to move the checking code in main ? Then you just have to add two getters to your dialog to retrieve the username and password. Then proceed with the check.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sharon_obl82
        wrote on last edited by
        #3

        Hi SGaist,

        I have this in my login.cpp:

        @
        void Login::on_login_clicked()
        {
        QString user = ui->username->text();
        QString passwd = ui->password->text();
        qDebug() << "User is" << user;
        qDebug() << "Password is" << passwd;
        }
        @

        And in my main.cpp I have this:
        @
        Login log(0);

        if (log.exec&#40;&#41; == QDialog::Accepted) {
        
           qDebug() << "Password is" << Login->username->text();   
          //check function
         a.quit();
        } else {
             qDebug() << "Wrong credential provided!";
        
        }
        

        @

        I can't seem to get the value from login.cpp to main. Do you know what's wrong?

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          hi,
          You're mixing stuff up. Making a variable Log (not a great name IYAM) of class type Login. After the exec() function you request member variables be returned form Login, not from log. Also use getters from like SGaist mentioned! Much better then public member variables!

          I'll rewrite it in main.cpp
          @
          LoginDialog MyLogIn_p = new LoginDialog(this);
          if (MyLogIn_p->exec() == QDialog::Accepted)
          {
          qDebug() << "User: " << MyLogIn_p->GetUserName();
          qDebug() << "PassWord: " << MyLogIn_p->GetPassWord();
          }
          // No need to delete the MyLogIn_p class, because of the parent (this) it will be removed by Qt when it is out of scope.
          @
          Of course does your dialog get two methodes: GetUserName and GetPassWord that return a QString.

          Greetz, Jeroen

          1 Reply Last reply
          0
          • JeroentjehomeJ Offline
            JeroentjehomeJ Offline
            Jeroentjehome
            wrote on last edited by
            #5

            Hmm, if you're still in main.cpp, did you start your application already?
            The eventQueque need to be run to handle the QDialog stuff.

            Greetz, Jeroen

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              exec runs an internal event loop so it should be good from that point of view

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • JeroentjehomeJ Offline
                JeroentjehomeJ Offline
                Jeroentjehome
                wrote on last edited by
                #7

                Oh,
                Also when the QApplication is not running?

                checking docs <<
                Ah, yes, it will start a local event loop ;-)
                Never too old to learn!

                Greetz, Jeroen

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Do you mean something like

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

                  QDialog dialog;
                  dialog.exec();

                  If (some condition) {
                  QWidget widget;
                  widget.show();

                  return app.exec();
                  

                  }

                  return -1;
                  }
                  @

                  ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sharon_obl82
                    wrote on last edited by
                    #9

                    Hi,

                    For the getters, I'm not sure if this is the right way to do but here's what i have in login.cpp

                    @
                    QObject::connect(ui->username, SIGNAL(textChanged(QString)), ui->username, SLOT(GetUserLogin()));
                    QObject::connect(ui->password, SIGNAL(textChanged(QString)), ui->password, SLOT(GetPassWord()));

                    void Login::GetUserLogin(const QString user)
                    {

                    QString userlogin = user;
                     qDebug() << "User is" << userlogin;
                    

                    }

                    void Login::GetPassWord (const QString passwd)
                    {

                    QString passwdlogin = passwd;
                    qDebug() << "passwd is" << passwdlogin;
                    }
                    @

                    It runs but it get this message:

                    QObject::connect: No such slot QLineEdit::GetUserLogin() in ..\ChineseNMT\login.cpp:18
                    QObject::connect: (sender name: 'username')
                    QObject::connect: (receiver name: 'username')
                    QObject::connect: No such slot QLineEdit::GetPassWord() in ..\ChineseNMT\login.cpp:19
                    QObject::connect: (sender name: 'password')
                    QObject::connect: (receiver name: 'password')

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Getters are not slots, just function returning something.

                      You should first take a look at some of the tutorials before going further, that will give you a good base to build on

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        sharon_obl82
                        wrote on last edited by
                        #11

                        Hi,
                        I'm sorry having to ask this again, I got my checking in main.cpp done but i noticed that it's looping twice. I'm not sure of where the loop is and I'm hoping that someone could help shed some light on this. Thanks very much!

                        login.cpp:
                        @
                        Login::Login(QWidget *parent) :
                        QDialog(parent),
                        ui(new Ui::Login)
                        {
                        ui->setupUi(this);
                        this->setWindowTitle("Login");
                        QObject::connect(ui->login, SIGNAL(clicked()), this, SLOT(on_login_clicked()));

                        }

                        void Login::on_login_clicked()
                        {

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

                        if ((!ui->username->text().isEmpty()) && (!ui->password->text().isEmpty()))
                        {
                        QMessageBox msgBox;
                        //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
                        {
                        msgBox.setText("Login FAILED! Try again!");
                        msgBox.exec();
                        }
                        }
                        else
                        {
                        qDebug() << "Invalid credential provided!";
                        }
                        }

                        @

                        main.cpp

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

                        MainWindow w;
                        w.showMaximized();
                        
                        Login MyLogIn;
                        MyLogIn.exec&#40;&#41;;
                        
                        
                        return a.exec&#40;&#41;;
                        

                        }

                        int MainWindow::checkLogin(QString a, QString b)
                        {

                        QString aa = a;
                        QString bb = b;
                        
                        
                        if((aa=="admin") && (bb=="1234"))
                        {
                            qDebug() << "Login success!";
                             return 0;
                        
                        }
                        else
                        {
                            qDebug() << "Login FAILED!";
                            return 1;
                        }
                        

                        }
                        @

                        result:
                        Login success!
                        Login success!
                        Success! Close dialog box
                        Login success!
                        Login success!
                        Success! Close dialog box

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @
                          //call checking from main.cpp
                          MainWindow mw; << it's not the MainWindow from main.cpp
                          mw.checkLogin(user, passwd);
                          if (mw.checkLogin(user, passwd) == 0)
                          @

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            sharon_obl82
                            wrote on last edited by
                            #13

                            thanks SGaist, I have changed my program the other round and now it's working without the double login.
                            Thanks very much for looking into this.

                            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