How to properly handle user input with multiple fields



  • Greetings community,

    I am attempting to add a user input window that appears when a user clicks the File menu and selects the new option. This is my first time attempting such a feat, and I was following the Input Panel example in the tutorials. I'm getting bogged down with the multiple elements in the example, but it has the additional element of the non-keyboard input panel.

    End game, what I would like is a panel that pops up, allows the user to input 3 values in text boxes. I'd like to have each value verified as the user types (IE if they type a letter when a number is expected, it declines the input and pops up a message), and submit all of them to a newly created tab (tab is created when they click New, but will be deleted if they cancel or immediately moved out of focus for the input window) when they press ok.

    Would I be right in saying that I should create a QInputPanel : QWidget Object, insert QDialog items for each input, and tie signal/slots to the submit button? How would I implement the validation? I know there are built in validation functions (validateint, validatedouble, etc..) that will work for what I need, but do they need to have each char sent to them as it is typed and then be stored in a variable for transmission to the calling window?

    I'll be giving this a shot regardless, but I thought I might ask and save some time. Thanks for any responses!!



  • Add a new QDialog based widget. Add your widgets (probably labels and line edits) to it in designer or design mode of qt-creator. In the C++ code, set a suitable validator on the line edits. The line edits take care of passing each character to the validator for you.



  • 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.



  • 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!



  • Afaik, it works like this:

    @
    buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
    @

    EDIT: fixed a code bug :-) Gerolf



  • 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 );
    

    @



  • 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).



  • You're not editing the auto-generated ui code are you by any chance?



  • Yes, I am. Where do I need to be putting this?



  • 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...



  • 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 );
    @



  • 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!!!!!!



  • @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?



  • @MhM93

    If 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.



  • @Ni-Sumi
    really thanks. It solved:

    1. Modal dialogs are started with exec(), they block the program flow while a nested event loop runs.
    2. 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();
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.