Results 1 to 10 of 10

Thread: Some QComboBox and custom QValidator background

  1. #1
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Some QComboBox and custom QValidator background

    I'm trying to understand how the validator framework works with a combo box.

    The documentation of QComboBox explains how to set a validator, and the QValidator documentation explains how a validator would work (and also how to subclass one).

    That's great but now that I have my custom validator and it's set to the combo box, I can't understand how the combo box interacts with it. Is the "validate" method I wrote called behind the scenes? If so, when? Or am I supposed to cast the QValidator to my own custom type and run the custom validate method on my own?

    If anyone can help out with a little background and hopefully a little code to illustrate, I'd be grateful.

    Thanks,
    Frankie

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Some QComboBox and custom QValidator background

    Do you understand what virtual method is?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Some QComboBox and custom QValidator background

    Yes, I understand that.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Some QComboBox and custom QValidator background

    Well if you do, then you should be able to answer that:
    I can't understand how the combo box interacts with it. Is the "validate" method I wrote called behind the scenes?
    Since QCombox will call the validate() method of the QValidator instance you gave it, and if is your custom QValidator it will be your version of it.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. The following user says thank you to high_flyer for this useful post:

    frankiefrank (11th October 2012)

  6. #5
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Some QComboBox and custom QValidator background

    First of all, the documentation does not state which data is being validated and when. Does the validation logic fire when I'm leaving focus, when I'm selecting an item from the existing list? When I'm calling addItem / insertItem programatically?

    And since you brought up the virtual method: I do have a follow up question. My custom validator validates a string but isn't based on regular expression or on numeric validation. I inherited from RegExpValidator but I don't really understand why I don't see it called:

    Qt Code:
    1. // Header
    2. class ValuesRangeValidator : public QRegExpValidator
    3. {
    4. public:
    5. ValuesRangeValidator(QObject * parent);
    6. QValidator::State validate(const QString &valueToValidate, int &pos);
    7. };
    8.  
    9. // Implementation
    10. ValuesRangeValidator::ValuesRangeValidator(QObject * parent)
    11. {
    12. }
    13.  
    14. QValidator::State ValuesRangeValidator::validate(const QString &valueToValidate, int &pos)
    15. {
    16. bool valid = someValidationCode(valueToValidate);
    17. return valid ? Acceptable : Invalid;
    18. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Some QComboBox and custom QValidator background

    First of all, the documentation does not state which data is being validated and when.
    I don't agree with that statement.
    If you read the docs for QComboBox::validator() you will see:
    Returns the validator that is used to constrain text input for the combobox.
    So it tells me, that during text input (which es every time you edit the text) the validator will be applied, which, makes sense too.

    Now:
    Does the validation logic fire when I'm leaving focus, when I'm selecting an item from the existing list? When I'm calling addItem / insertItem programatically?
    That is also in the docs, but you have to pay attention:
    Since we are dealing with an editable QComboBox, it has a QLineEdit.
    So it means, when you input text, you are doing the input in to it QLineEdit.
    That means, that your validator is applied by the QLineEdit.
    Lets see what we can find there:
    You can change the text with setText() or insert(). The text is retrieved with text(); the displayed text (which may be different, see EchoMode) is retrieved with displayText(). Text can be selected with setSelection() or selectAll(), and the selection can be cut(), copy()ied and paste()d. The text can be aligned with setAlignment().

    When the text changes the textChanged() signal is emitted; when the text changes other than by calling setText() the textEdited() signal is emitted; when the cursor is moved the cursorPositionChanged() signal is emitted; and when the Return or Enter key is pressed the returnPressed() signal is emitted.

    When editing is finished, either because the line edit lost focus or Return/Enter is pressed the editingFinished() signal is emitted.

    Note that if there is a validator set on the line edit, the returnPressed()/editingFinished() signals will only be emitted if the validator returns QValidator::Acceptable.
    Ok, I agree, that it doesn't answer your question in a nice one block, and that you have to apply some thought to see how this relates to your case.
    But you can't expect the documentation to answer everything in the right way for each case.
    But as you can see, the information is there, one just needs to invest in reading it ;-)

    I inherited from RegExpValidator but I don't really understand why I don't see it called:
    Hmm..
    How did you check to know if its being called?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. The following user says thank you to high_flyer for this useful post:

    frankiefrank (3rd March 2011)

  9. #7
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Some QComboBox and custom QValidator background

    First of all thank you for your time and attention, and for bringing up the relevant documentation.

    I'm sorry but even after reading about the different types of signals emitted from the lineEdit control I can't "guess" the behavior of an editable QComboBox just because it has a lineEdit member. For example, where does it say that validation is even called upon leaving focus of the combo's lineEdit?

    I don't expect the documentation to answer "every question" for "every case". I stand by what I said that the documentation about the integration of a validator and a combox is almost not there (unless you know how the combo box wraps the signals emitted from its lineEdit members).

    I put a breakpoint in my validate method to see if it's called at any point during my UI/programmatic interaction. I'm not seeing any calls so I'm guessing something in my wiring is wrong.

    This piece of documentation:

    "Note that if there is a validator set on the line edit, the returnPressed()/editingFinished() signals will only be emitted if the validator returns QValidator::Acceptable."
    still leaves me wondering why I'm not seeing the call.

  10. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Some QComboBox and custom QValidator background

    For example, where does it say that validation is even called upon leaving focus of the combo's lineEdit?
    Here (QLineEdit):
    When editing is finished, either because the line edit lost focus or Return/Enter is pressed the editingFinished() signal is emitted.
    And since the validator is applied during text input, and because finishing the text input through focus change is part of the input, the validator should be called as result of focus leave.

    I put a breakpoint in my validate method to see if it's called at any point during my UI/programmatic interaction. I'm not seeing any calls so I'm guessing something in my wiring is wrong.
    Ok, that is the right way to do it.
    Hmm...
    Can you post the code where you set your validator?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #9
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Some QComboBox and custom QValidator background

    Here's the combo box related code:

    Qt Code:
    1. // Initizalize Y axis controls
    2. mYScalePresets = QStringList()
    3. << "some"
    4. << "presets"
    5. << "here";
    6.  
    7. ui.cmbYAxis->setDuplicatesEnabled(false);
    8. ui.cmbYAxis->addItems(mYScalePresets);
    9.  
    10. ui.cmbYAxis->insertSeparator(ui.cmbYAxis->count()); // Insert separator at the end
    11. ui.cmbYAxis->setValidator(new ValuesRangeValidator(ui.cmbYAxis));
    12. ui.cmbYAxis->setInsertPolicy(QComboBox::InsertAtBottom);
    13.  
    14. ui.cmbYAxis->setCurrentIndex(0);
    To copy to clipboard, switch view to plain text mode 

    I'lll clarify the two issues I have:
    1. I want to see that the validator code is executed when I'm putting text in and clicking Enter.
    2. The behavior I would eventually want to achieve is to interferen to at the point of leaving focus. I want to add the item to the combo if its text is valid.

    Thanks again for your assistance.


    Added after 20 minutes:


    OK, I've found the reason for the 1st issue: and it did come down to the virtual method after all!

    I've used the wrong signature:
    Qt Code:
    1. // WRONG:
    2. // QValidator::State validate(const QString &valueToValidate, int &pos);
    3.  
    4. // Correct:
    5. QValidator::State validate(QString &valueToValidate, int &pos) const;
    To copy to clipboard, switch view to plain text mode 

    But now I see that perhaps this whole approach is wrong for me because it would be very hard for me to code all the conditions for the "Intermediate" state. Can you try and help out with the 2nd issue?


    Added after 22 minutes:


    Another scenario that require an intervention point that I still don't know to find:
    When manually entering data, to limit the amount of items (for example, if passed 10, remove the first).

    Is there a solution I'm not seeing or does this require subclassing of the combobox?
    Last edited by frankiefrank; 3rd March 2011 at 20:03.

  12. #10
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Some QComboBox and custom QValidator background

    Just wanted to update with what I ended up doing, in case someone has a similar situation.

    First: I decided that the validator framework is not right for me, since my intermediate state is too complex to be defined by a reg ex.
    Second: I also decided not to do anything when leaving focus, instead only deal with entering new/existing data in the combo's line-edit OR selecting an existing option from the combo.

    SO - to make sure I'm validating the entered input, I've used an eventFilter method to catch pressing on Enter/Return, and wired the method to the combo. Now I can control validation and decide if I want to add the item / or select an existing item, or ignore the whole thing altogether.

    Thanks for the help!

Similar Threads

  1. QComboBox and QValidator
    By mrandreas in forum Newbie
    Replies: 3
    Last Post: 27th September 2010, 18:29
  2. How to change QComboBox background with a picture?
    By mulei in forum Qt Programming
    Replies: 3
    Last Post: 24th September 2010, 07:59
  3. custom display in a QComboBox
    By scarleton in forum Qt Programming
    Replies: 2
    Last Post: 3rd September 2010, 22:13
  4. QComboBox Custom Widget has missing designer properties
    By vieraci in forum Qt Programming
    Replies: 5
    Last Post: 9th December 2009, 13:30
  5. Can't compile custom QValidator
    By vieraci in forum Qt Programming
    Replies: 1
    Last Post: 22nd August 2009, 15:12

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.