Results 1 to 10 of 10

Thread: QSpinBox / QDoubleSpinBox and valueChanged signal

  1. #1
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSpinBox / QDoubleSpinBox and valueChanged signal

    I have a few QSpinBox and QDoubleSpinBox within a form which I use the signal "valueChanged()" in order to tell if the user changed the value in the spinbox. I noticed however that this signal also fires if I change the value within my program.

    Is there a way to differentiate between the user changing the value, and the program changing the value itself?

    for instance on a QLineEdit, I can use the "textEdited()" signal, which does not fire when the program sets the text, or on a QComboBox I can use "activated()" which doesn't fire unless the user changes the value.

  2. #2
    Join Date
    Jan 2008
    Posts
    56
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSpinBox / QDoubleSpinBox and valueChanged signal

    Hi,

    I had the same problem. There is no special signal as you mention for QLineEdit. I used the editingFinished signal in a derived class which is fired when the SpinBox loses focus or return is pressed and fired my own valueChangedOnEdit signal.

    Regards,

    Rainer

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

    tpf80 (23rd October 2008)

  4. #3
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSpinBox / QDoubleSpinBox and valueChanged signal

    Ah thanks, I didn't see that one in the long list of items with the derived classes. This is a good start.

  5. #4
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSpinBox / QDoubleSpinBox and valueChanged signal

    the editingFinished signal didn't work for me because it does not activate when the user clicks the up and down arrows on the spin box, nor does it activate if the user edits the field, but does not click on another field. In the end I used the valueChanged signal, but I made it work how I wanted to in this way:

    1) I created a global flag true/false, set default to false.
    2) when I change the field value without user interaction, I set the flag to true during the time the program is populating the spinbox, then when the program is done populating, it sets the flag back to false.
    3) on the slot that is called by valueChanged, it checks if this flag is false, and if it is, then we can go and run the code in the slot. If the flag is true, then we know the program is in the process of populating the field, and so do not execute the slot code.
    4) it is set up so that the user can not edit the field at the same time as the program, so that there is no possibility that the valueChanged signal would fire from a user edit, while the program edit is in progress. (not likely anyway because it happens so fast, but just in case)

  6. #5
    Join Date
    Jan 2010
    Posts
    10
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Re: QSpinBox / QDoubleSpinBox and valueChanged signal

    I have a quite similar problem... hope somebody can help me:

    i've got a QSpinBox which sends its value to a server. Naturally I chose the valueChanged-Signal at first. But I didn't like the fact that when typing 155 the signal emits first the 1 then the 15 and at last the 155. So instead I tried using editingFinished. I don't have a problem with using the return-key to emit the signal after typing. BUT I want the Up and Down-Button to work without the return.

    Is there any way to achieve this ?

    I know it's possible to get to the QLineEdit of the SpinBox , is there also a way to get to the signal of the buttons ?
    Or if I send the signal valueChanged to a function I create myself: Can I somehow find out in which way the signal was emitted (if it was only a number typed which i would ignore until the return button or if it was a mouse-click/ button-pressed) ??

    I'd really appreciate the help !!

  7. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QSpinBox / QDoubleSpinBox and valueChanged signal

    I think disabling keyboard tracking will produce the results that you want. Check out "setKeyboardTracking(bool)" in the docs.

  8. The following 3 users say thank you to norobro for this useful post:

    abdallagafar (26th December 2010), Eos Pengwern (31st January 2010), invisible_uli (11th January 2010)

  9. #7
    Join Date
    Jan 2010
    Posts
    10
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Thumbs up Re: QSpinBox / QDoubleSpinBox and valueChanged signal

    Quote Originally Posted by norobro View Post
    I think disabling keyboard tracking will produce the results that you want. Check out "setKeyboardTracking(bool)" in the docs.
    Thanks so much norobro. I should have benn able to find that myself ...
    "If keyboard tracking is disabled, the spinbox doesn't emit the valueChanged() signal while typing. It emits the signal later, when the return key is pressed, when keyboard focus is lost, or when other spinbox functionality is used, e.g. pressing an arrow key."

    Anyway, thanks again for the quick answer !!!!

  10. #8
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSpinBox / QDoubleSpinBox and valueChanged signal

    Quote Originally Posted by tpf80 View Post
    Is there a way to differentiate between the user changing the value, and the program changing the value itself?
    There is a generic trick for this sort of problems. Say you have xSpinBox that controls the value of variable x. the idea is to set the spinbox AFTER you set the variable:
    x = newx; xSpinBox->setValue(x);
    the first line in the valueChanged slot should then be like this:
    if (value == x) return;
    This elliminates the need for flags, and allows changing the variable at any time inside the program.

  11. #9
    Join Date
    Mar 2010
    Location
    Volcano, Big Island, Hawaii
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSpinBox / QDoubleSpinBox and valueChanged signal

    Quote Originally Posted by tpf80 View Post
    I have a few QSpinBox and QDoubleSpinBox within a form which I use the signal "valueChanged()" in order to tell if the user changed the value in the spinbox. ... Is there a way to differentiate between the user changing the value, and the program changing the value itself?
    I had the same problem, which I solved in this manner: In my slot, I check to see if my QSpinBox hasFocus(). If it doesn't, then I know that the change in value is programmatic rather than user input. It seems to work...

  12. #10
    Join Date
    Jun 2018
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSpinBox / QDoubleSpinBox and valueChanged signal

    The easiest way is to use QObject::blockSignals:
    Qt Code:
    1. spinBox->blockSignals(true);
    2. spinBox->setValue(5); // now the valueChanged signal isn't emitted
    3. spinBox->blockSignals(false);
    To copy to clipboard, switch view to plain text mode 

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.