[Solved]How can I launch a QWizardPage with a key?
-
@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.
-
@kshegunov said:
From my experience this is also very Spanish of you ... ;)
haha yeah, I had to use google translate to find something to say " I can't believe what is happening now..." do you know the english expression? :D
QMessageBox::Yes | QMessageBox::No, //< Leave those in peace, no need to change them.
Are you sure you want to leave them alone? They seem suspicious... hahaha sorry that comment make me laugh.. couldn't resist to make a joke :P
Your code snippet works as all the before test.... Its really pretty with its warning icon... the message... no title bar... and its f**** buttons not focused or able to focus hahahaha
EDIT:
QPushButton * yesButton = qobject_cast<QPushButton *>(box.button(QMessageBox::Yes)); //< Get the "Yes" button
Oh! Thank you for showing me how to get its Yes button! I'm not used to do that cast things so I never think about them (And honestly I don't know so well how to use it yet)
-
@kshegunov said:
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.
Sorry for the second post but I think its necessary (And as I see you online I dont want to cross again with your answer...
I've look about that you said... in the pc I have the focus in WP1's first QPushbutton (lets call it PB1) for example, then I press Esc and the QMessageBox appears having the focus itselft and its yes button and I can see the PB1 not focused anymore....
Nevertheless when I do it in the HW, as I said, I'm in WP1 with PB1 focused, then press Esc and the QMessageBox appears but it does not have a focus, neither its buttons and I can see the focus on PB1 (But can't move or do anything with TAB as it should if I where in WP1)... dunno if i explain it well.. if it is not clear please let me know. Ty! -
do you know the english expression?
As far as I can tell, the one you quoted is a perfectly valid English expression.
Nevertheless when I do it in the HW, as I said, I'm in WP1 with PB1 focused, then press Esc and the QMessageBox appears but it does not have a focus, neither its buttons and I can see the focus on PB1 (But can't move or do anything with TAB as it should if I where in WP1)... dunno if i explain it well.. if it is not clear please let me know.
This is very strange. I don't know what's happening or why the focus is not transferred to the message box. It sounds silly, but did you update your HW program with the last one (with the last code)? Also you could try setting a parent to the message box (substitute
NULL
forthis
in the constructor) and see if that works better (it won't be able to leave the main window area, but hopefully this won't be a problem). You could try also to set all the default properties that the button should have (although this doesn't seem like a good approach, but if it works ...):yesButton->setFocus(Qt::OtherFocusReason); yesButton->setAutoDefault(true); yesButton->setDefault(true);
Additional note (not related to the focus):
Don't useQString::fromUtf8
like you're doing, what you had before (tr("some text")
) was perfectly fine.Kind regards.
-
@kshegunov said:
As far as I can tell, the one you quoted is a perfectly valid English expression.
Ok.. as you said it was something spanish I thought that thas a bad-literal translation...
It sounds silly, but did you update your HW program with the last one (with the last code)?
Yes it might seem silly but it isn't.. so many problems came from here but in this case it is silly because as I've got a lot of problems (releases that doesn't update well.. and so on), what I do every time is deleting the old release and then generate the new one and also I always check the date and time of the file...
I forgot to add on my last post that I also tried with that:
yesButton->setFocus(); yesButton->setFocusPolicy(Qt::TabFocus);
And I've tried with your last lines and still its the same... the focus is stuck in the PB1... u.u'' I'm trying everything I think but nothing.... what a problem (I ran out of time and all those little gnomes/mushrooms/code strange problems doesn't help me so much) xD
Don't use QString::fromUtf8 like you're doing, what you had before (tr("some text")) was perfectly fine.
Which is the difference between "text" and tr("text") ? In my case I have to use QString::fromUtf8 because its the only way I can use accents in my text and ofc my app is in spanish... I just switch it to english to post it here so it will be more understandable to everyone who reads this topic :) So my code its something like QString::fromUtf8("Confirmación");
Kind regards for you too!! :D hehe
-
Also you could try setting a parent to the message box (substitute NULL for this in the constructor) and see if that works better (it won't be able to leave the main window area, but hopefully this won't be a problem).
What about this? I'm really running out, or more correctly have run out, of ideas ...
Which is the difference between "text" and tr("text") ?
"text"
is of typeconst char * const
- a string literal, whiletr("text")
takes the string"text"
and looks it up into the translation tables to create aQString
from it that's translated to the currently set language. However, your next explanation seems like a valid reason to useQString::fromUtf8
instead, since you don't appear to be using the translation framework.Kind regards.
-
@kshegunov said:
try setting a parent to the message box
Sorry didn't saw it. Tried and its the same, the only thing that changes is that, as the default text is red in the parent, the QMessageBox's text is red too. but still the samebehaviour with PB1 focused... the QMessage appears like if you put a stick there....
I'm really running out, or more correctly have run out, of ideas ...
Wow! I thought that would never be possible!! :D Haha you had a lot of different ideas :D My next thought is: how could I do this confirm message without using QMessagebox or QDialog (I still dunno why do you call it "modal").. I'm trying to think how to do it if those classes didn't exist.. but not sure about the options...
"text" is of type const char * const - a string literal, while tr("text") takes the string "text" and looks it up into the translation tables to create a QString from it that's translated to the currently set language.
I think I understand you but do you know any clear example? I think that you mean that if it has to change any letter it will o it but not sure
However, your next explanation seems like a valid reason to use QString::fromUtf8 instead, since you don't appear to be using the translation framework.
Are you sure? Oh wow I did something fine! hahahaha (I'm trying to be positive xd)
Note: I've tried also with:
box.setFocus(); box.setFocusPolicy(Qt::PopupFocusReason);
ofc nothing works...