Switching menus



  • 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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

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



  • @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();
    
    
    
    }
    

    }


  • Qt Champions 2016

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



  • @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();
    }


  • Qt Champions 2016

    @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");
    }
    

Log in to reply
 

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