Results 1 to 6 of 6

Thread: QDialog check values

  1. #1

    Default QDialog check values

    Hi everyone,

    I'm having a problem with QDialog. In my main() function I am creating an QDialog with multiple lines and checkBoxes where the user has to enter some information. Afterwards he can either click OK or Cancel.
    What I want is: when clicking OK, the program should validate the entered information (e.g. if the necessary boxes were checked). If everything is fine, the information should be accepted and the dialog should close. If not, an error message should appear. After the error message the dialog is shown again with the wrong entered information. So, I don't want to just create a new dialog with empty fields every time, but I want to keep the entered information by the user.
    When Cancel is clicked, the dialog should close.

    I tried many things but none of them worked. I read about the done()-function, but I am not sure how to use it properly. I hope you can help me.

    Thank you!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDialog check values

    The correct and very simple way to do this is to override the accept() method.

    Just reimplement accept() in your dialog class and perform the validation there.
    If you are satisfied you call the base class, i.e. QDialog::accept(), if you are not, just return

    Cheers,
    _

  3. #3

    Default Re: QDialog check values

    Thank you for the very fast answer. I already tried that, but I have a problem. All the variables containing the user information are basically "stored" in the main()-function. But how can I access them if I am writing a new function like:

    void QDialog::accept() {

    ...

    }

    I'm always getting an error when trying to access them the normal way.

    Thanks!

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDialog check values

    So you have information outside the dialog that you need for validating the user input inside the dialog?

    Can't you simply pass it to the dialog before using it?

    Cheers,
    _

  5. #5
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDialog check values

    The dialog-set variables should belong to the dialog. If they have been set OK, pass them outside the dialog and call QDialog::accept(). If they aren't OK, show the error message and return from your accept().
    How to "pass them outside" depends on your code. You are using some such method in fact because the "user information are basically stored in the main()-function".

  6. #6
    Join Date
    Aug 2015
    Location
    Gdansk, Poland
    Posts
    21
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QDialog check values

    The following link will show you the example with code :
    Validate-Data-in-QDialog

    Note :
    # QDialog::Accepted & QDialog::done(r), don't change/remove "QDIALOG" with your dialog class name, or it will return to infinite loop. Just left it that way

    ^^

Similar Threads

  1. Replies: 4
    Last Post: 16th August 2015, 09:42
  2. Replies: 3
    Last Post: 16th April 2015, 22:49
  3. Replies: 5
    Last Post: 27th November 2014, 11:15
  4. How to convert string hex values to its real binary values?
    By AttilaPethe in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2012, 23:47
  5. Replies: 9
    Last Post: 25th March 2011, 22:22

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.