[Solved]How can I launch a QWizardPage with a key?
-
Hi!
I'm working on Qt 4.8.5 and doing a QWizard program with its QWizardPages.... I want to launch a especific QWizardPage if user press a key. That means:
No matter where user is, he can press Esc where I quit the application:
bool BasePage::eventFilter(QObject *obj, QEvent *event) { if(event->type() == QEvent::KeyPress) { QKeyEvent *key = static_cast<QKeyEvent *>(event); if (key->key() == Qt::Key_Escape) { QApplication::quit(); } .... } }
Now what I want is to launch a especific QWizard page (let's call it ConfirmEsc) so I can ask the user if he really wants to quit or not. I want to do it with a QWizardPage to be able to have the same style than the other pages.
Any idea of how can I achieve this? Thank you!
-
@roseicollis
Hello,Now what I want is to launch a especific QWizard page (let's call it ConfirmEsc) so I can ask the user if he really wants to quit or not. I want to do it with a QWizardPage to be able to have the same style than the other pages.
Use a modal dialog and style it up the same way. Such input solicitation windows are not wizard pages, so you shouldn't use
QWizardPage
for them.Kind regards.
-
Well... the problem is that this app is used only with a keyboard, no mouse allowed, and it has to have big buttons so I wanted to call a QWizardPage to ask if they want to quit or not and the user has to select a QPushButton bigger than the QWizard own buttons (Back, next...)
-
Well... the problem is that this app is used only with a keyboard, no mouse allowed, and it has to have big buttons so I wanted to call a QWizardPage to ask if they want to quit or not and the user has to select a QPushButton bigger than the QWizard own buttons (Back, next...)
Notwithstanding that, you still should use a modal dialog and not a wizard page.
-
Hi,
I agree with @kshegunov on that point. Use a dialog for that task, the user can then either change the focus of the button using tab or use escape to dismiss the dialog.
-
Ok I'll try it and if I have a problem I'll tell you...I supposse I can do that dialog as big as I want and so his buttons?
-
If you make the dialog modal then there's no risk that the user does something else with the application.
-
@SGaist @kshegunov I'm trying to do it with:
QDialog *a = new QDialog(this,Qt::WindowTitleHint); a->setModal(true); a->show();
And it appears (bigger as expected but I'll see later how to change it) My problem is that this program is used only with a keyboard so no mouse allowed so I don't want the tittle bar or at least I don't want the minimize/maximize/close buttons that appear upside right... how can I hide it?
Another question which is the difference between a message box and a modal dialog? Does message box have only the Ok button?
I just need a little window where it says something like" are you sure you want to exit?" and the buttons: Yes and No.
-
@roseicollis
Hello,
Consider the following example where some flags are passed to the dialog constructor.// (Un)set some window flags for the dialog QDialog dialog(this, (Qt::Dialog | Qt::CustomizeWindowHint) & ~(Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint | Qt::WindowContextHelpButtonHint)); int result = dialog.exec(); //< Use QDialog::exec for modal dialogs
Of course one could notice that this should also work, unless the flags' values are changed at some point:
QDialog dialog(this, Qt::Dialog | Qt::CustomizeWindowHint); dialog.exec();
EDIT:
I missed your second question. Yes, you can use a message box, as it's a dialog itself.I just need a little window where it says something like" are you sure you want to exit?" and the buttons: Yes and No.
For this I'd usually use a
QMessageBox
, however I haven't checked if the system menu and title bar can be disabled, but I see no reason why it shouldn't be possible.Kind regards.
-
@kshegunov said:
QDialog dialog(this, Qt::Dialog | Qt::CustomizeWindowHint);
Oh.. I saw those flags but tried with one and made my dialog not appear so thought it was a bad idea, but I was doing it wrong.
I don't know if its because of the big cold I have or what but I can't see how to add QWidgets to that dialog (a label and the 2 buttons). Should I make my own dialog class ( class MyDialog : public QDialog) or is there an easy way? I douln't find how to do it even in google... o.o
//----------------
I've found another solution with the QMessageBox:QMessageBox::StandardButton dialog; dialog = QMessageBox::warning(this, "Confirm", "<font color=\"#000\">Do you really want to quit?</font>", QMessageBox::Ok | QMessageBox::Cancel); // by default my colour is red so I had to force it to black here with css if( dialog == QMessageBox::Ok) close(); else event->ignore();
But... I can't see how can I take off the title bar in this case, or how can I set some stylesheet for the background of that message bos (I usually do somethink like
dialog->setStyleSheet("background-color: #fb0;");
).So tried with the next code which allows me to change the background color but can't get rid of the title bar and can't find how to close the program with its Ok button :
QMessageBox msgBox(tr("Title"), tr("Do you really want to quit the application?"), QMessageBox::Warning, QMessageBox::Ok, QMessageBox::Cancel, QMessageBox::Escape); msgBox.setStyleSheet("background-color: #AAA;"); msgBox.exec();
And also I want to know how to reach that with the modal dialog to learn that way to do it...
-
Hello,
Oh.. I saw those flags but tried with one and made my dialog not appear so thought it was a bad idea, but I was doing it wrong.
I don't know if its because of the big cold I have or what but I can't see how to add QWidgets to that dialog (a label and the 2 buttons). Should I make my own dialog class ( class MyDialog : public QDialog) or is there an easy way?Deriving from
QDialog
is one option, yes. I personally tend not to do it though. I initialize the forms "from the outside" like this (which you could also use in this case):QDialog dialog(this, Qt::Dialog | Qt::CustomizeWindowHint); // Create the form and initialize the dialog with it. Ui::DialogForm ui; ui.setupUi(&dialog); //< Initialize the dialog with the form // Connect the buttons from the form ... QObject::connect(ui.okButton, SIGNAL(clicked()), &dialog, SLOT(accept())); QObject::connect(ui.cancelButton, SIGNAL(clicked()), &dialog, SLOT(reject())); // ... Fiddle with the dialog more (if needed), add stylesheet w/e // Display the dialog and handle the result int result = dialog.exec(); //< Display a modal dialog. if (result == QDialog::Accepted) { //< The dialog was accepted (ok button clicked) // ... Do more things based on that }
You could do similar things with the
QMessageBox
, but then you shouldn't be displaying it with the static functions.
I hope this helps to get you started.Kind regards.
-
@roseicollis
Hello,
If you edit up your post after I've posted, I won't get a notification and will never know something has changed. I stumbled upon the changes by pure luck, so if you'd forgotten something or wish to explain more, please post it separately.Now, your message box code is wrong, that's why it's not working. Something like this should suffice:
QMessageBox box( QMessageBox::Warning, //< Icon tr("Title"), tr("Do you really want to quit the application?"), QMessageBox::Yes | QMessageBox::No, //< Buttons NULL, //< or this Qt::Dialog | Qt::CustomizeWindowHint //< Window flags ); box.setStyleSheet(""); // You can still set a stylesheet here if you wish int result = box.exec(); if (result == QMessageBox::Yes) { //< Well, the user clicked yes, so do stuff }
The Qt documentation provides a pretty fine set of examples on message boxes. I don't know what
QMessageBox::Escape
is, probably you got it confused from the roles the buttons can have, however for a yes/no message box, these aren't needed.Kind regards.
-
@kshegunov First of all sorry. I didn't meant to edit the post but came after lunch and saw that I forgot to press the Submit message so I did it thinking you didn't saw the last message. I'm so sorry for that. Thanks for seeing it but if you look at it I just added more things I've tried :)
I always look on the Qt documentation but sometimes I miss some info or dunno really how to do it. I was looking for dialog instead of QMessageBox so I didn't the link you have posted, thanks!
Now with your QMessageBox box example I understand clearly how it works.
What I don't get is the modal dialog example where you say
// Create the form and initialize the dialog with it.
Ui::DialogForm ui;
ui.setupUi(&dialog); //< Initialize the dialog with the formI'm creating that "message" in the
::keyPressEvent()
of my QWizard class (class MyWizard: public QWizard
) so... writting your code as is, it says:
error: 'DialogForm' is not a member of 'Ui'
I've tried creating it in the .h:private: Ui::DialogForm *ui;
And then in the constructor:
MyWizard::MyWizard(QWidget *parent) : QWizard(parent), ui(new Ui::DialogForm) {
but then it complains with the .h line saying
error: 'DialogForm' in namespace 'Ui' does not name a type
and if I change the DialogForm for my MyWizard then of course says that can't convert dialog into QWizard in the line ui.setupUi(&dialog);What am I missing here¿?
(I don't have the MyWizard default ui because I don't use it and I take it off... dunno if that has something in common with my problem) -
@roseicollis said:
Hello,I didn't meant to edit the post but came after lunch and saw that I forgot to press the Submit message so I did it thinking you didn't saw the last message. I'm so sorry for that.
No problem, it happens, I'm just suggesting to be sure before editing, so there won't be things missed in the confusion. Editing a typo, a missed word is still fine, as it doesn't change the meaning. :)
I'm creating that "message" in the ::keyPressEvent() of my QWizard class ( class MyWizard: public QWizard) so... writting your code as is, it says:
error: 'DialogForm' is not a member of 'Ui'This means your compiler is complaining, because there's no such class defined. Did you include the form header? Ordinarily when creating a class for a form, you make something like this:
#include <QDialog> #include "ui_mydialogform.h" //< This comes from the uic when your form is compiled class MyDialogForm : public QDialog { Q_OBJECT public: MyDialogForm(QWidget * parent = NULL) : QDialog(parent), ui(new Ui::MyDialogForm) { ui->setupUi(this); } // ... Other things for the dialog ... private: Ui::MyDialogForm * ui; };
correct?
In my example, you don't create a separate class, so you'd have to do what the constructor would have done somewhere, you'd have something like this (in the source where you're using the dialog):#include "ui_mydialogform.h" //< This comes from the uic when your form is compiled // ... Some MyWizard functions ... void MyWizard::keyPressEvent(QKeyPressEvent * event) { QDialog dialog; Ui::MyDialogForm dialogUi; dialogUi.setupUi(&dialog); // Do things with the dialog and the dialogUi object ... }
What am I missing here¿?
Very Spanish of you to ask it like that. :D
Here is described the direct approach, which I use, while you're used to doing it like in the single inheritance approach. A difference of methods, that's all.
Kind regards.
-
@kshegunov said:
Very Spanish of you to ask it like that. :D
Hahahaha :P Yeah! how did you know that ? (I've jsut upload my profile info to show my country :D) Sorry for my bad english :(
In my example, you don't create a separate class,
With that I understand that you don't do another class so no .cpp or .h or .ui added to create that dialog but then you say that I have to:
#include "ui_mydialogform.h" //< This comes from the uic when your form is compiled
But I don't understand from where do you get it. As it is a .h then I understand that you are adding a .h file to the project (not a class because .cpp is not needed maybe) so I have to create this .h and include it, is it right? (If yes I understand that the .h content will be what you wrote up about class myDialogForm: public QDialog...) I'm a little bit confused...
Thank you for all your help, and sorry for making this post so long but I want to learn things the best way (and not just copy-paste and forget how they work if they run as expected)
-
@roseicollis said:
Hahahaha :P Yeah! how did you know that ?
The upside-down question mark is a dead giveaway.
Sorry for my bad english :(
Your English is just fine!
With that I understand that you don't do another class so no .cpp or .h or .ui added to create that dialog but then you say that I have to:
#include "ui_mydialogform.h" //< This comes from the uic when your form is compiled
But I don't understand from where do you get it.When you add a form to your project (you have
FORMS += someform.ui
in the .pro file)qmake
will run the user interface compiler (uic
) on your.ui
file and that produces the aforementioned header. It'll be named as the form is named with prefixui_
and extension.h
. So if you havesomeform.ui
in your project,qmake
will generateui_someform.h
for you.It's mentioned in passing (too casually for my taste to be honest) in the documentation:
The special feature of this file is the FORMS declaration that tells qmake which files to process with uic. In this case, the calculatorform.ui file is used to create a ui_calculatorform.h file that can be used by any file listed in the SOURCES declaration.
-
@kshegunov said:
The upside-down question mark is a dead giveaway.
Ô.ó Right! hahaha Well seen!
When you add a form to your project (you have FORMS += someform.ui in the .pro file) qmake will run the user interface compiler (uic) on your .ui file and that produces the aforementioned header. It'll be named as the form is named with prefix ui_ and extension .h. So if you have someform.ui in your project, qmake will generate ui_someform.h for you.
I see.. interesting... but still lost :P (and while asking I'm trying things too, don't think I'm not just posting and waiting for an answer).
What I had before posting here, was my MyWizard class where I want to add a dialog. And I understand that to use something like
QDialog dialog; Ui::MyDialogForm dialogUi;
I need to add a .h file but I don't get what should I do really.I've just tried adding a ui file with add new > form> dialog with buttons bottom>DialogForm.ui then I've run qmake and as you said I was able to add
#include "ui_DialogForm.h"
but then it complains in theUi::DialogForm ui;
and I have to change it toUi::Dialog ui;
but then ofc it complains with the next line saying thaterror: 'class Ui::Dialog' has no member named 'okButton'
and the same with the cancelButton(I'm trying to understand the documentation link you show me meanwhile ^^)
-
Ah, yes, that. The members are named after the object names provided in the designer. So if your root object is called
Dialog
then the class will be calledUi::Dialog
as well. Here's a screenshot I've used in another thread.
I hope this helps.
-
@kshegunov Ohh I see!!! Ok ok now I understand what it is doing and that yes, I had to create a file (.ui in my case). I've see also in the Qt Designer view that the buttons Ok and Cancel are inside a buttonBox called... buttonBox xD so ofc I can't do
QObject::connect(ui.okButton, SIGNAL(clicked()), &dialog, SLOT(accept()));
But I canQObject::connect(ui.buttonBox, SIGNAL(clicked()), &dialog, SLOT(accept()));
but don't know how to access those buttons.. should I do only one connect with the buttonBox and then in the SLOT try to know which button was pressed??And I have another question, what are those connect exactly for? In my case I can just:
Ui::Dialog ui; ui.setupUi(&dialog); int result = dialog.exec(); if (result == QDialog::Accepted) close();
Right?
And yes, it helped a lot! :D -
@roseicollis said:
But I can QObject::connect(ui.buttonBox, SIGNAL(clicked()), &dialog, SLOT(accept())); but don't know how to access those buttons.. should I do only one connect with the buttonBox and then in the SLOT try to know which button was pressed??
Nope, your buttons have names too, so access them by their name:
Ui::Dialog ui; ui.buttonNameHere; //< This is the button with name "buttonNameHere"
And I have another question, what are those connect exactly for? In my case I can just:
...
Right?Nope,
QDialog::exec()
will block until the dialog is closed. So the connects are there to ensure that clicking on the button will close the dialog with the appropriate status, eitherAccepted
, set by theaccept()
slot, orRejected
, set by thereject()
slot.