Results 1 to 4 of 4

Thread: Validate Data in QDialog

  1. #1
    Join Date
    Jul 2007
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Validate Data in QDialog

    Here's an easy one...I'm running Qt 4.3 on Red Hat. My dialog contains a QTextEdit. When the user presses OK, I need to make sure that the data in the QTextEdit is valid. Since I used Qt Designer to make the dialog, it has a QDialogButtonBox. I have connected the "clicked" signal to a slot which checks the size of the QTextEdit. If the size is adequate, I call "accept()" otherwise the slot just returns. The problem is that the dialog disappears even when accept() is not called. If I use the reject() function, the dialog still disappears.

    Below is some code...thanks for any help or ideas.
    Joel

    Qt Code:
    1. DataSourceDlg :: DataSourceDlg()
    2. {
    3. connect(buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(checkValues(QAbstractButton*)));
    4. }
    5.  
    6. void DataSourceDlg :: checkValues(QAbstractButton* button)
    7. {
    8.  
    9. if (button->text() == "Cancel")
    10. {
    11. accept(); // dismiss dialog with user click on Cancel
    12. } else {
    13. if (nodeLineEdit->text() > 3) {
    14. accept(); // dismiss dialog with valid data
    15. }
    16. }
    17. return; // invalid data do not dismiss dialog
    18. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 13th July 2007 at 23:09. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Validate Data in QDialog

    Probably because the "OK" button is the default dialog button, therefore is connected by default to the accept() slot.

    The solution is to delete the default button box(with OK and Cancel) created by designer and to create your own.

    Regards

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Validate Data in QDialog

    How about a slot that validates contents of the dialog and enables/disables the button correspondingly? This slot would be connected to various content change signals of entry widgets in the dialog. It is usually a good idea to prevent insensible user actions.
    J-P Nurmi

  4. #4
    Join Date
    Jul 2007
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Smile Re: Validate Data in QDialog

    I contacted Trolltech support. In case anyone searches this thread for a similar problem, I thought I'd send the resolution. It is not necessary to create one's own ok/cancel buttons, although that would probably work (I didn't try that). It is also not necessary to enable/disable the ok/cancel buttons, but that is a good idea (I didn't try that either). A simpler solution is to override the done() method and do the validation there. To make the dialog disappear, just call the parent's done() method. To keep the dialog on the screen, simply return. Here's some code...

    Thanks for the ideas,
    Joel

    Qt Code:
    1. void DataSourceDlg::done(int r)
    2. {
    3. if(QDialog::Accepted == r) // ok was pressed
    4. {
    5. if(nodeLineEdit->text().size() > 3) // validate the data somehow
    6. {
    7. QDialog::done(r);
    8. return;
    9. }
    10. else
    11. {
    12. statusBar->setText("Invalid data in text edit...try again...");
    13. return;
    14. }
    15. }
    16. else // cancel, close or exc was pressed
    17. {
    18. QDialog::done(r);
    19. return;
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 24th July 2007 at 01:38. Reason: missing [code] tags

Similar Threads

  1. QSqlQueryModel data update
    By psi in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2012, 03:59
  2. QByteArray with network data
    By merlvingian in forum Qt Programming
    Replies: 1
    Last Post: 1st June 2007, 17:53
  3. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53
  4. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 16:17
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.