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. Switching menus
QtWS25 Last Chance

Switching menus

Scheduled Pinned Locked Moved Unsolved General and Desktop
menuc++
10 Posts 2 Posters 7.9k 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.
  • mandruk1331M Offline
    mandruk1331M Offline
    mandruk1331
    wrote on last edited by mandruk1331
    #1

    I want to amke a Monitoring student system, and the first menu is the Log in, the person enters the ID and password and if he's a teacher he get one UI and if he's a student he gets the other, my question is, how I can close the Log In screen and open the Students UI?
    because when I open the students interface the Log In is still open

    Mandruk1331

    kshegunovK 1 Reply Last reply
    0
    • mandruk1331M mandruk1331

      I want to amke a Monitoring student system, and the first menu is the Log in, the person enters the ID and password and if he's a teacher he get one UI and if he's a student he gets the other, my question is, how I can close the Log In screen and open the Students UI?
      because when I open the students interface the Log In is still open

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @mandruk1331
      Isn't this the same question? Please don't double-post.
      As to your question, make a dialog and show the login form, after successful login change the main window and destroy the dialog. Something like this:

      class LoginDialog : public QDialog
      {
          // ...
          //< Call accept() when login was successful, or reject() on cancel buton or close event
      };
      
      class MainWindow : public QMainWindow
      {
          // ...
      };
      
      int main(int argc, char  ** argv)
      {
          QApplication app(argc, argv);
      
          MainWindow mainWindow;
          LoginDialog login;
          login.show();
      
          QObject::connect(&login, SIGNAL(accepted()), &login, SLOT(close()));
          QObject::connect(&login, SIGNAL(accepted()), &mainWindow, SLOT(show()));
          QObject::connect(&login, SIGNAL(rejected()), &app, SLOT(quit()));
          
          return QApplication::exec();
      }
      

      Kind regards.

      Read and abide by the Qt Code of Conduct

      mandruk1331M 1 Reply Last reply
      1
      • kshegunovK kshegunov

        @mandruk1331
        Isn't this the same question? Please don't double-post.
        As to your question, make a dialog and show the login form, after successful login change the main window and destroy the dialog. Something like this:

        class LoginDialog : public QDialog
        {
            // ...
            //< Call accept() when login was successful, or reject() on cancel buton or close event
        };
        
        class MainWindow : public QMainWindow
        {
            // ...
        };
        
        int main(int argc, char  ** argv)
        {
            QApplication app(argc, argv);
        
            MainWindow mainWindow;
            LoginDialog login;
            login.show();
        
            QObject::connect(&login, SIGNAL(accepted()), &login, SLOT(close()));
            QObject::connect(&login, SIGNAL(accepted()), &mainWindow, SLOT(show()));
            QObject::connect(&login, SIGNAL(rejected()), &app, SLOT(quit()));
            
            return QApplication::exec();
        }
        

        Kind regards.

        mandruk1331M Offline
        mandruk1331M Offline
        mandruk1331
        wrote on last edited by
        #3

        @kshegunov Yes it's the same, but no one has answered, so it just went down. Thank you for your reply. And if I use smth like this:
        RegForm (it's a class) Reg;
        Reg.setModal(true);
        Reg.exec();
        Will this be good?

        Mandruk1331

        kshegunovK 1 Reply Last reply
        0
        • mandruk1331M mandruk1331

          @kshegunov Yes it's the same, but no one has answered, so it just went down. Thank you for your reply. And if I use smth like this:
          RegForm (it's a class) Reg;
          Reg.setModal(true);
          Reg.exec();
          Will this be good?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @mandruk1331
          I don't think so, since you don't have a running event loop at that point. Also QDialog::exec is only for modal dialogs anyway, no need to call QDialog::setModal.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • mandruk1331M Offline
            mandruk1331M Offline
            mandruk1331
            wrote on last edited by
            #5

            And If i'll do smth like this? I just want to learn the correct way of programming
            static S_UI StudentS;
            StudentS.show();
            QMainWindow::close();

            Mandruk1331

            kshegunovK 1 Reply Last reply
            0
            • mandruk1331M mandruk1331

              And If i'll do smth like this? I just want to learn the correct way of programming
              static S_UI StudentS;
              StudentS.show();
              QMainWindow::close();

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @mandruk1331
              Nope. You don't need and shouldn't use a static variable in this case. Additionally, you should call only static methods as static, and QMainWindow::close is not static so it requires an object. Don't take this the wrong way, but it seems to me you could use a bit of reading on C++, it'll help you to understand the basics and will immensely help you with Qt.

              Kind regards.

              Read and abide by the Qt Code of Conduct

              mandruk1331M 1 Reply Last reply
              1
              • kshegunovK kshegunov

                @mandruk1331
                Nope. You don't need and shouldn't use a static variable in this case. Additionally, you should call only static methods as static, and QMainWindow::close is not static so it requires an object. Don't take this the wrong way, but it seems to me you could use a bit of reading on C++, it'll help you to understand the basics and will immensely help you with Qt.

                Kind regards.

                mandruk1331M Offline
                mandruk1331M Offline
                mandruk1331
                wrote on last edited by mandruk1331
                #7

                @kshegunov I have made it static because it's in a function (I forgot to mention it), and after the execution the window opens and closes because it goes out of the scope, that's I made it static

                void MLogIn::on_pushButton_clicked() // LogIn
                {
                if(ui->radioButton->isChecked()){ //Student
                static S_UI StudentS;
                StudentS.show();
                QMainWindow::close();

                }
                else if(ui->radioButton_2->isChecked()){ //Teacher
                    T_UI Teacher;
                    Teacher.exec();
                
                
                
                }
                

                }

                Mandruk1331

                kshegunovK 1 Reply Last reply
                0
                • mandruk1331M mandruk1331

                  @kshegunov I have made it static because it's in a function (I forgot to mention it), and after the execution the window opens and closes because it goes out of the scope, that's I made it static

                  void MLogIn::on_pushButton_clicked() // LogIn
                  {
                  if(ui->radioButton->isChecked()){ //Student
                  static S_UI StudentS;
                  StudentS.show();
                  QMainWindow::close();

                  }
                  else if(ui->radioButton_2->isChecked()){ //Teacher
                      T_UI Teacher;
                      Teacher.exec();
                  
                  
                  
                  }
                  

                  }

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @mandruk1331
                  Hello,
                  Whatever the reason you made it static, you shouldn't! QObject instances (widgets included) are not supposed to be created before the QApplication instance is available, otherwise you could run into all sorts of weird behavior. So read carefully the example I've provided in my first post and try to adapt it to your case. And I reiterate, you mustn't use non-static functions as static ones like this: QMainWindow::close();.
                  Provide the login functionality inside your dialog and call accept() or reject() depending on the acceptance of the credentials.

                  Kind regards.

                  Read and abide by the Qt Code of Conduct

                  mandruk1331M 1 Reply Last reply
                  1
                  • kshegunovK kshegunov

                    @mandruk1331
                    Hello,
                    Whatever the reason you made it static, you shouldn't! QObject instances (widgets included) are not supposed to be created before the QApplication instance is available, otherwise you could run into all sorts of weird behavior. So read carefully the example I've provided in my first post and try to adapt it to your case. And I reiterate, you mustn't use non-static functions as static ones like this: QMainWindow::close();.
                    Provide the login functionality inside your dialog and call accept() or reject() depending on the acceptance of the credentials.

                    Kind regards.

                    mandruk1331M Offline
                    mandruk1331M Offline
                    mandruk1331
                    wrote on last edited by
                    #9

                    @kshegunov
                    It has to be like this:

                    MAIN:
                    int main(int argc, char *argv[])
                    {
                    QApplication a(argc, argv);
                    MLogIn w;
                    w.show();
                    MRegForm Reg;
                    QObject::connect(&w,SIGNAL(accept()),&Reg,SLOT(show()));
                    return a.exec();
                    }

                    LOGIN.h
                    class MLogIn : public QMainWindow
                    {
                    Q_OBJECT

                    public:

                    explicit MLogIn(QWidget *parent = 0);
                    ~MLogIn();
                    

                    signals:
                    void accept();
                    private slots:
                    void on_pushButton_2_clicked();

                    void on_pushButton_clicked();
                    

                    private:
                    Ui::MLogIn *ui;
                    };

                    LOGIN.cpp
                    void MLogIn::on_pushButton_2_clicked() //Register
                    {
                    emit accept();
                    }

                    Mandruk1331

                    kshegunovK 1 Reply Last reply
                    0
                    • mandruk1331M mandruk1331

                      @kshegunov
                      It has to be like this:

                      MAIN:
                      int main(int argc, char *argv[])
                      {
                      QApplication a(argc, argv);
                      MLogIn w;
                      w.show();
                      MRegForm Reg;
                      QObject::connect(&w,SIGNAL(accept()),&Reg,SLOT(show()));
                      return a.exec();
                      }

                      LOGIN.h
                      class MLogIn : public QMainWindow
                      {
                      Q_OBJECT

                      public:

                      explicit MLogIn(QWidget *parent = 0);
                      ~MLogIn();
                      

                      signals:
                      void accept();
                      private slots:
                      void on_pushButton_2_clicked();

                      void on_pushButton_clicked();
                      

                      private:
                      Ui::MLogIn *ui;
                      };

                      LOGIN.cpp
                      void MLogIn::on_pushButton_2_clicked() //Register
                      {
                      emit accept();
                      }

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by kshegunov
                      #10

                      @mandruk1331
                      QDialog already provides all the signals (accepted() and rejected()) and slots (accept() and reject()) you'd need. You only need to call accept() when you've checked that the login is successful (meaning in the button slot). See my example in post #1 how to connect them! As to the login dialog implementation, something like this:

                      class MLogIn : public QDialog
                      {
                          Q_OBJECT
                      public:
                          explicit MLogIn(QWidget *parent = 0);
                          ~MLogIn();
                      
                      private slots:
                          void on_pushButton_2_clicked();
                          void on_pushButton_clicked();
                      private:
                          Ui::MLogIn *ui;
                      };
                      
                      void MLogIn::on_pushButton_2_clicked()
                      {
                          //< Try logging the user in
                          if (loginSuccessful)
                              accept();
                          else
                              QMessageBox::warning("No such user/password");
                      }
                      

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      1

                      • Login

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