Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

I can't change pages from a Qwidget to a Qmainwindow



  • I am building a application that have some pages. I want to use Main window so that i can get the toolbar. Let's say i have a page 1 which is a widget now using a push button i want to go to the main window but it seems i cannot access the main window when i click the push button the main window doesn't appear.
    I have tried modal approach and modal less approach
    Can someone help me?
    I am new to qt so it will be helpful if you can describe me fully...


  • Lifetime Qt Champion

    Hi,

    Please be more descriptive about what your application should do.

    Usually when someone is talking about pages it makes sense to explore QWizard however if you are not building a wizard, you likely will be interested in QStackedWidget rather than trying to go back and forth with several instances of QMainWindow.



  • @Mahin-Abid
    You would have to show your code, there is nothing special about QMainWindow so you should be able to do whatever you are trying to achieve. There is no "modal/modeless" in this case, that only applies to QDialogs, and QMainWindow is not one of those.

    Meanwhile, I always wonder when people start talking about showing/hiding the main window. It is more usual that the main window would never be hidden once shown initially. If you have "pages" you wish to display, one possibility is to use a QStackedWidget as a widget inside the main window, that can show any one of a number of different widgets at any one time, That may or may not fit in with what you are looking for.



  • @SGaist Hi, I want to make a diary app that have a sign in sign up page and then a page where the user can write their diary. As i need mainwindow for menu bar and toolbar so i tried to use stacked widget but then the problem was the menu bar and toolbar is shown in every pages. I need them to show just in one page. It will be also helpful if you can help me by telling me how to add a menu bar or a tool bar in a widget which is not a mainwindow.



  • @JonB Hi, Sorry that i didn't said it right at first. it was not a widget i was using QDialog .I want to make a diary app that have a sign in sign up page and then a page where the user can write their diary. As i need mainwindow for menu bar and toolbar so i tried to use stacked widget in the mainwindow and then add pages for sign in and sign ups.But then the problem was the menu bar and toolbar is shown in every pages. I need them to show just in one page. It will be also helpful if you can help me by telling me how to add a menu bar or a tool bar in a widget or in a Qdialog which is not a mainwindow.



  • @Mahin-Abid
    Then it sounds for the logon like you want a modal QDialog to be exec()ed from your main() before you go on to display the QMainWindow. Do that rather than trying to put the logon on the QMainWindow/in a QStackedWidget.



  • @JonB hi, I have similar problem with my application. my problem is i am completely new to this so i can't even undersatnd your solution. it will be very helpful if you can demonstrate it or can give me solution like what should i code and what should i code.



  • @JonB do you mean
    MyDialog MyDialog;
    MyDialog.setModal(true);
    MyDialog.exec();
    this code?


  • Lifetime Qt Champion

    Yes, that's what @JonB is suggesting. You'll have to then check the result of the result of the login process before continuing.


  • Lifetime Qt Champion

    @Mahin-Abid
    yes in main.cpp before showing Mainwindow.
    the exec() is a so called blocking function so it will stay in there until user press close or ok. Your Dialog should validate the credentials.

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
       MyDialog Dialog;  
     if ( Dialog.exec() ==  QDialog::Accepted)
        w.show();
    else
      QMessageBox::warning(this, tr("My Application"),tr("invalid login.\n");
    
        ... ask again or quit the app-                              
        return a.exec();
    }
    
    


  • @SGaist problem is it's showing there is no membar called setModal


  • Lifetime Qt Champion

    @Mahin-Abid
    But is MyDialog based on QDialog class ?



  • @SGaist If i am not clear i want to go to a mainwindow from a Qdialog using a push button . I tried went to the slot of the push button and write the code but it's saying there is no member called setModal. I also tried by creating pointer Which is modal approach in header file of the dialog then the problem was the mainwindow pops for 1 sec and the whole application gets terminated.


  • Lifetime Qt Champion

    @Mahin-Abid
    well without the actual code, hard to guess what went wrong.
    But is the goal not to pop the winow before showing Dialog to get login and if ok, then
    show MainWindow ?


  • Lifetime Qt Champion

    Hi
    Here is sample of login / then show mainwindow.
    In main.cpp it opens LoginDialog and ask for password.
    iF password is 1234 then show main window, else show message and quit application.

    https://www.dropbox.com/s/ricjl8t9ocjmd27/LoginTest.zip?dl=0



  • @Mahin-Abid said in I can't change pages from a Qwidget to a Qmainwindow:

    @SGaist problem is it's showing there is no membar called setModal

    That's because only a QDialog --- or something derived from it --- can be shown modally. "Modal" means it displays itself and then waits until the user does something on it to cause it to close. You can only do that on a "dialog". QMainWindow does not derive from QDialog, it just a "plain" window. It is not the main window you wish to be modal, it is the dialog asking for the username/password.

    Take a breath and look at @mrjj's sample code above, I've put in a couple of comments:

    int main(int argc, char *argv[])
    {
        # Create a `QApplication`
        # nothing visual happens at this point
        # but this is required in order to make Qt calls
        QApplication a(argc, argv);
        # Declare the main window
        # Again, nothing visual happens here yet
        MainWindow w;
        # and similarly declare a dialog
        MyDialog Dialog;  
        # Display the dialog, and wait for the user to interact with it (fill in name/password), and click `OK`
        # This all happens because we call `QDialog::exec()`
        # You don't even need to call `QDialog::setModal(true)` because `exec()` does that automatically for you
        # Have the dialog code check the username + password
        # If they are good have it call `QDialog::accept()`, which returns `QDialog::Accepted` here
        # else if they are bad have it call  `QDialog::reject()`
        if ( Dialog.exec() ==  QDialog::Accepted)
           # Only now do we finally show the `QMainWindow`
           # we simply `show()` it, nothing "modal" here
           w.show();
        else
           QMessageBox::warning(this, tr("My Application"),tr("invalid login.\n");
    
        ... ask again or quit the app-      
        # we must call `QApplication::exec()` to have the Qt event loop running
        # note that this `exec()` has nothing to do with `QDialog::exec()` used earlier                    
        return a.exec();
    }
    

Log in to reply