How to properly handle user input with multiple fields
-
wrote on 31 Mar 2011, 13:36 last edited by
What you need is some kind of dialog to get your user interaction.
I did not chance upon QInputPanel in the Qt documentation, but perhaps you are looking for QInputDialog? That class is not suited for what you want, because it only supports a single input value. Instead you should create your own dialog from scratch. Easiest is to use Qt Creator. Simply choose File -> New file or project, Select "Qt" under "Files and Classes" on the left, and choose Qt Designer Form Class in the top right. Follow the wizard (use some sensible names for your class please), and use the Designer to design your dialog.
On the validation side of things: study QValidator. It is probably sufficient for your needs. Just enable or disable the OK button on your form depending on if all three inputs validate properly.
This should get you started, I think.
-
wrote on 31 Mar 2011, 18:35 last edited by
Thanks! This worked. I had far more than I actually needed to get this thing running. One quick question for you though...my buttons are setup in a default button box like this:
@buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok );@
How to I go about disabling (greying out) the Ok button in this context? I would assume I need to get a handle for QDialogButtonBox::Ok, but it's only a parameter and not an object itself. How do I get to the object representing the Ok button?
Thanks! You've already been a major help!
On a side note, is there any reference book you would highly recommend I have in my arsenal for Qt programming?
Thanks! -
wrote on 31 Mar 2011, 18:37 last edited by
Afaik, it works like this:
@
buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
@EDIT: fixed a code bug :-) Gerolf
-
wrote on 31 Mar 2011, 19:05 last edited by
I tried putting that immediately after the definition of the buttonbox (see below), and it doesn't kick up an error (which is always pleasant). When I ran the program, though, the Ok button was still clickable. Does this need to be communicated to the form in another way?
What do I need to put around the code to make it...errr...readable? :)
Thanks!
@
void setupUi(QDialog *Dialog)
{
if (Dialog->objectName().isEmpty())
Dialog->setObjectName(QString::fromUtf8("Dialog"));
Dialog->setEnabled(true);
Dialog->resize(400, 300);
buttonBox = new QDialogButtonBox(Dialog);
buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
buttonBox->setGeometry(QRect(50, 250, 341, 32));
buttonBox->setOrientation(Qt::Horizontal);
buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok );//Test code buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
@
-
wrote on 31 Mar 2011, 19:11 last edited by
You can use "@" symbols to delimit blocks of code in your posts or click on the "Code" button in the editor toolbar (the rightmost one).
-
wrote on 31 Mar 2011, 19:13 last edited by
You're not editing the auto-generated ui code are you by any chance?
-
wrote on 31 Mar 2011, 19:43 last edited by
Yes, I am. Where do I need to be putting this?
-
wrote on 31 Mar 2011, 19:47 last edited by
I'm not sure whether there was an issue with this. Try out doing this in the show() - event of the dialok. I think there was something...
-
wrote on 31 Mar 2011, 20:05 last edited by
I don't quite get what you're saying. I'm currently using this to invoke the dialog (slot triggered by a signal from a clicked main menu item):
@
void VCMainWindow::InvokeMPDDialogWindow(){ /BEGIN FUNCTION INVOKEMPDDIALOGWINDOW/
VCMPDataInputForm NewForm;
NewForm.setWindowTitle( "New Dataset");
NewForm.exec();
} /CLOSE FUNCTION INVOKEMPDDIALOGWINDOW/
@If I try to do it in the form constructor, it tells me invalid use of incomplete type struct pushbutton as well as forward declaration of struct pushbutton.
@
ui->buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
@ -
wrote on 31 Mar 2011, 20:43 last edited by
I figured it out. It was because QPushButton wasn't included in any of the headers in the #include directives. I included it and it works perfectly fine.
Thanks!!!!!!
-
wrote on 16 Jun 2016, 04:55 last edited by MhM93
@mchris357
Hi. I have a problem the same as you. How do you solve your problem? I also have a dialog in my application and I used input panel. when the qdialog show and I want to type in my textbox the input panel appears but the buttons not work. When I googling all pages says because the dialog is modal and should always in top widget so the input panel not work. How do you solve it and can use input panel in your dialog? -
@mchris357
Hi. I have a problem the same as you. How do you solve your problem? I also have a dialog in my application and I used input panel. when the qdialog show and I want to type in my textbox the input panel appears but the buttons not work. When I googling all pages says because the dialog is modal and should always in top widget so the input panel not work. How do you solve it and can use input panel in your dialog?wrote on 16 Jun 2016, 06:04 last edited by Ni.SumiIf it is the same problem, thy have given already the solution . It got solved for them just by including #include <QPushButton>. Try it in your case, Also it might problem og show() / exec(). Please Ckeck this
If it did not solve your problem, please start a new thread, saying your problem.
-
wrote on 16 Jun 2016, 06:07 last edited by
@Ni-Sumi
really thanks. It solved:- Modal dialogs are started with exec(), they block the program flow while a nested event loop runs.
- Modeless dialogs are started with show(), they do not block the program flow.
From http://www.qtforum.org/article/14285/modeless-dialog.html
I use this code :MyDialog *d=new MyDialog(this); d->show(); d->raise();
instead of this code:
MyDialog *d=new MyDialog(this); d.exec();