I can't change pages from a Qwidget to a Qmainwindow
-
@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 modalQDialog
to beexec()
ed from yourmain()
before you go on to display theQMainWindow
. Do that rather than trying to put the logon on theQMainWindow
/in aQStackedWidget
. -
@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? -
Yes, that's what @JonB is suggesting. You'll have to then check the result of the result of the login process before continuing.
-
@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
-
@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.
-
@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 ? -
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 fromQDialog
, 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(); }