Swap from one ui. Form to another on button click



  • Hello all,

    I am very new to -QT- Qt Creator and -QT- Qt and c++ in general. I am trying to learn how to develop desktop applications and have encountered a slight problem thats held me up for a while now.

    I want to be able to swap which .ui form is being displayed via the click of a button. What i want to achieve is, having a log in screen and once the user clicks log in they move to the logged in screen. I am going to add database validation of users etc but at the moment i just want to get the swapping of forms working.

    My project layout looks like this

    ->Headers
    -- mainwindow.h

    ->Sources
    -- main.cpp
    --mainwindow.cpp

    ->Forms
    --logged.ui
    --mainwindow.ui

    ->Resources
    --Resources.qrc

    On the applicatin load it displays the mainwindow.ui and i need it to display the logged.ui on the button click. I have created the button and added a Signal, aswell as adding the relevant code into the mainwindow.h file
    @public slots:
    void LoginClicked();@

    In my mainwindow.cpp file i then have

    @void MainWindow::LoginClicked() {

    }@

    I just need to figure out what code goes in that function to change the form thats visible, i.e. hide the mainwindow.ui and show logged.ui

    Many Thanks

    David



  • The way I handle it in my app is to make the login screen a member of the main window: in the main window's constructor I create and show the login screen and hide the main window itself. I also connect the accept() signal from the dialog to a function that validates the input and shows the main window if it checks out. I'm sure there are other ways of handling it, but this was the simplest I could think of.



  • Switching the UI file itself is not needed in this case. Simply make two separate widgets, and create and show the login window when needed (that is, if the login window is a dialog).

    If the login screen should be in the main window, you can use a QStackedWidget, add both pages, and programmatically switch which one should be shown.



  • if the login details are ok, send a signal that connects to some slot that creates + shows the logged.ui form, and then hide() the mainwindow

    [code]
    void mainwindow::tryLogin( /* some details */)
    {
    bool login_is_ok = some_checking_method(/details/);
    if (login_is_ok)
    {
    emit signal_do_login();
    hide();
    }
    }
    [/code]


Log in to reply
 

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