[Solved]How can I launch a QWizardPage with a key?
-
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. -
@kshegunov said:
Nope, your buttons have names too, so access them by their name:
Yes i know every widget has a name but as those buttons were created by Qt when I created the ui, I see them in a buttonBox and i don't see their names separately ( I can do ui.buttonBox->Ok for example but not sure if its the Ok button) and even being able to do that, I can't put ui.buttonBox->Ok on the connect.
Well if I press Enter on the Ok button it closes all so it seems it works correctly and closes everything, I don't see the difference with or without connects... :S
-
@roseicollis
Honestly, I have no idea how the button box works. I have never used it myself. If I had to guess it directly connects to the dialog in its constructor, but I have to check the source to be sure. You could access its button with QDialogButtonBox::button if you need.Edit:
Indeed the button box will connect the buttons to an internal handler, which in turn handles the actions and emit the signals. Or not, in any case it doesn't really matter why it works, only that it works. :DKind regards.
-
@kshegunov I'm freaking out... I had 3 ways of getting what I wanted: 2 with QMessageBox and one with QDialog and all them worked fine on the pc... the QMessageBox had the title bar even if I put the flag to avoid it but decided it was not important if it appears...
Today I wanted to test the program in the HW where it goes and found that the title bar wasnt there (which is cool) BUT... When the dialog/message appears, there is no focus on the Yes/Cancel buttons as you have them in the Pc (And also the borders and more things are missing). My mate told me that as the HW has been limited it's so possible that there are some dlls missed and they don't want to put them so my problem now is... how can I access those buttons to make them have the focus (in QDialog. in QMessageBox i'm looking how the buttonbox works )? I've tried some things but nothing appears to work...
-
Add more info:
I've tried to not put the default buttons and put mines in a QMessageBox.
They appear correctly but again, in pc they have the focus but in the HW they don't (And I can see behind the message a QPushButton from the WP focused.. dunno if this is the problem that there can be only 1 focus, or if they are 2 different focus (one for the wp and another for the messagebox))QMessageBox box( QMessageBox::Warning, //< Icon QString::fromUtf8("Confirm"), QString::fromUtf8("Really want to quit?"), QMessageBox::NoButton , NULL, //< or this <-- dunno what that does Qt::Dialog | Qt::CustomizeWindowHint //< yeah! no title bar on the HW while I can see it in the pc... what a troll.. ); QPushButton *yesButton = box.addButton(tr("Yes"), QMessageBox::YesRole); QPushButton *noButton = box.addButton(tr("No"), QMessageBox::NoRole); yesButton->setFocus();
-
@roseicollis
Hello,I'm freaking out...
From my experience this is also very Spanish of you ... ;) Take a breath, there's nothing to freak about.
My mate told me that as the HW has been limited it's so possible that there are some dlls missed and they don't want to put them
Nope, when you don't put a parent for the dialog (or any widget for that matter) it is created as a native widget. This means that the window styles, decorations and such are managed by the window manager. The window manager can choose to ignore the flags you pass it, so there are really no guarantees. Putting a parent to the dialog/widget, thus Qt creating it as alien widget, have other implications though so I think it'd be best to stick to dialogs that have no parent.
I've tried to not put the default buttons and put mines in a QMessageBox.
No need to do that for "Yes/No" buttons, as this is already provided to you by the message box.
dunno if this is the problem that there can be only 1 focus, or if they are 2 different focus (one for the wp and another for the messagebox))
As far as I know you can have only one widget focused. In any case I don't think the focus is the problem. I think it might be that on your (other) HW the default button is not set properly.
So to your code snippet:QMessageBox box( QMessageBox::Warning, QString::fromUtf8("Confirm"), QString::fromUtf8("Really want to quit?"), QMessageBox::Yes | QMessageBox::No, //< Leave those in peace, no need to change them. NULL, //< This is the dialog's parent widget, NULL for no parent Qt::Dialog | Qt::CustomizeWindowHint //< Depends on the window manager's mood! ); QPushButton * yesButton = qobject_cast<QPushButton *>(box.button(QMessageBox::Yes)); //< Get the "Yes" button Q_ASSERT(yesButton); //< Only ensures that yesButton is not NULL, not that it should be. yesButton->setDefault(true); //< Set the "Yes" button to be the default for the dialog.
What applies to the message box, also applies to any dialog. You can get your buttons in a similar fashion (look up the docs as I'm lazy-ish) - there's a function get the buttons from a button box. It's done exactly as I've shown here for the
QMessageBox
.Try what happens if you set the default button manually, and let me know.
Kind regards.