Results 1 to 9 of 9

Thread: QDialog, editingFinished, and Cancel Button.

  1. #1
    Join Date
    May 2009
    Location
    Memphis, TN
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QDialog, editingFinished, and Cancel Button.

    I have a form with a lineedit widget that calls a function to validate the data on the signal editingFinished.

    ... but I do not want the lineedit to be validated if the cancel button is pressed. (I just want cancel to mean "Cancel -- Abort" no data validation.)

    Qt Code:
    1. connect(this->pushButton_Cancel, SIGNAL(clicked()), this, SLOT(myReject()));
    2. connect(this->lineEdit_DriverCode, SIGNAL(editingFinished()), this, SLOT(checkDriverCode()));
    To copy to clipboard, switch view to plain text mode 



    current processing:
    1. Enter invalid data in the lineedit widget.
    2. Press the cancel button. (I think this actually happens after line 3.)
    3. The lineedit widget loses focus and causes the editingFinished signal to be emitted. (checkDriverCode called)
    4. I can not tell the cancel button was pressed from checkDriverCode because the editingFinished is called before the cancel button pressed registers.
    Any ideas to help.

    Thank you, Jeff.

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDialog, editingFinished, and Cancel Button.

    i think eventFilter() can help you.

    Qt Code:
    1. MyDialog::MyDialog(QWidget* p):QDialog(p)
    2. {
    3. ....
    4. ....
    5. //do not connect the editingFinished() signal
    6. connect(this->pushButton_Cancel, SIGNAL(clicked()), this, SLOT(myReject()));
    7. //connect(this->lineEdit_DriverCode, SIGNAL(editingFinished()), this, SLOT(checkDriverCode()));
    8.  
    9. //this->pushButton_Cancel->installEventFilter(this);
    10. //this->lineEdit_DriverCode->installEventFilter(this);
    11. qApp->installEventFilter(this);//i think this is better than calling installEventFilter() on every widget
    12. }
    13.  
    14. bool MyDialog::eventFilter(QObject* o,QEvent* e)
    15. {
    16. static bool focusLostFromLineEdit=false;
    17.  
    18. //check if we lost focus from lineedit
    19. if(e->type()==QEvent::FocusOut)
    20. {
    21. if(o==this->lineEdit_DriverCode)
    22. focusLostFromLineEdit=true;
    23. else
    24. focusLostFromLineEdit=false;
    25. }
    26.  
    27. //we got a focusIn in some widget... if the previous widget is lineEdit and the new widget is
    28. // cancel button than do not validate.
    29. if(e->type()==QEvent::FocusIn)
    30. {
    31. if(focusLostFromLineEdit)
    32. {
    33. if(o!=this->pushButton_Cancel)//the focus was lost from lineedit and some other widget got it
    34. checkDriverCode();
    35. focusLostFromLineEdit=false;//just over cautious
    36. }
    37. }
    38.  
    39. return QDialog::eventFilter(o,e);
    40.  
    41. }
    To copy to clipboard, switch view to plain text mode 

    This is just a quick idea.. please check for errors

    Edit: Few problems:-
    One problem with this code is that it does not handle the case when you pressed() the cancel button but do not clicked() it. In other words, just press the mouse button on the cancel button but do not release it(moved the mouse in pressed state out of cancel button).

    Second problem is that if you get to cancel button using the Tab key. (this can be handled easily though, just by not placing the lineedit and button next to each other)

    Third problem: I have not tested this code
    Last edited by nish; 2nd June 2009 at 03:30.

  3. The following user says thank you to nish for this useful post:

    jeffpogo (2nd June 2009)

  4. #3
    Join Date
    May 2009
    Location
    Memphis, TN
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDialog, editingFinished, and Cancel Button.

    Thank you for the idea!

    Your suggestion was a good idea, but I can not get it to work correcly for the problems you already mentioned.

    This problem seems like a common issue and saw others asking similar questions, but no good solutions are posted.

    I will try a new design unless someone has other ideas.

    Jeff.

  5. #4
    Join Date
    May 2009
    Posts
    62
    Thanks
    2
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDialog, editingFinished, and Cancel Button.

    It depends what you need the validation for:

    • Don't allow the user to enter invalid values -> Use QValidator
    • Don't allow the user to click on OK with an invalid value entered. -> Validate on signal QLineEdit::textEdited(QString) and enabled/disable OK button.
    • Just do the validation after the user clicked on OK. -> Validate on signal QDialog::accepted()


    Or did I miss a point?

  6. #5
    Join Date
    May 2009
    Location
    Memphis, TN
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDialog, editingFinished, and Cancel Button.

    Problem:

    The user enters some invalid data in a lineedit and presses the cancel button.

    The cancel button should allow the user to abort the dialog entry without complaining about the invalid data in the lineedit.

    but ... the lineitem editingFinished signal is triggered before the program knows the cancel button was pressed.

    I need a way to tell the slot function called using the editingFinished signal to ignore the validation checks because the cancel button was pressed.

  7. #6
    Join Date
    May 2009
    Posts
    62
    Thanks
    2
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDialog, editingFinished, and Cancel Button.

    I woudn't use the editingFinished signal to validate.

    If you want to validate only when the OK button is clicked, do it like this:

    Qt Code:
    1. MyDialog::MyDialog(QWidget *parent) :
    2. QDialog(parent),
    3. {
    4. // ...
    5. connect(pushButton_OK, SIGNAL(clicked()), this, SLOT(validate()));
    6. connect(pushButton_Cancel, SIGNAL(clicked()), this, SLOT(reject()));
    7. }
    8.  
    9. void MyDialog::validate()
    10. {
    11. if (lineEdit->text() != "Valid data")
    12. {
    13. QMessageBox::critical(this, tr("Invalid Data"), tr("You entered invalid data"));
    14. return;
    15. }
    16. accept();
    17. }
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDialog, editingFinished, and Cancel Button.

    Quote Originally Posted by jeffpogo View Post
    Thank you for the idea!

    Your suggestion was a good idea, but I can not get it to work correcly for the problems you already mentioned.

    This problem seems like a common issue and saw others asking similar questions, but no good solutions are posted.

    I will try a new design unless someone has other ideas.

    Jeff.
    All the problems can be solved by just some hacking here and there... previously you were stuck with the editfinish() signal emiting before the cancel button.. that was solved by eventfilter... now as far as pressed() goes...

    i think your problem can be solved by just extending the code a bit more... i can give you some hints..

    1. button has a pressed() signal
    2. a button subclass can monitor mouseEnter, mousePress, mouseRelease
    3. events can be sent back to parent widget
    4. you can generate any event (QEvent*) and sent it to any widget.

    using subclasses, eventfilter, and lil bit here an there u can overcome the problem... its may take less than a day. but something tells me it is possible.

    but i like to tell u that the behaviour you want to achieve is very uncommon and i as a user also do not like it... do it the standard way as suggested by someone before.

  9. #8
    Join Date
    May 2009
    Location
    Memphis, TN
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDialog, editingFinished, and Cancel Button.

    For now...

    I am going with the web page style solution...check all the lineedit fields in the form after the ok button is pressed.

    I wanted to only enable the ok button after all the fields had valid data but I will change the logic to enable when all the required fields "are filled-in" and do the database look ups at accept().

    Thank you for the help!

  10. #9
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDialog, editingFinished, and Cancel Button.

    Quote Originally Posted by jeffpogo View Post
    For now...

    I am going with the web page style solution...check all the lineedit fields in the form after the ok button is pressed.

    I wanted to only enable the ok button after all the fields had valid data but I will change the logic to enable when all the required fields "are filled-in" and do the database look ups at accept().

    Thank you for the help!
    thats the standard way... !! good luck

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.