Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Detect Whether the index Change in ComboBox is by user or programatically



  • Hi,

    I have a QComboBox. I have connected SIGNAL currentIndexChanged to a SLOT update(). So may is there any way that i can check whether the currentIndexChanged is called because of user interaction on comboBox or programatically.

    Thanks
    Deepak


  • Moderators

    When you change it programatically, "block signals":http://qt-project.org/doc/qt-5.0/qtcore/qobject.html#blockSignals and update by hand. You can pass a boolean value to indicate that, for example. So:
    @
    combobox->blockSignals(true);
    // change the index here. Combobox will not send any signals!
    // so you need to call your slot yourself - here is where you can inform it that it's not user change
    combobox->blockSignals(false);
    @



  • Hi,

    In my piece of code the SIGNAL currentIndexChanged() is called which will call my SLOT update(). So inside that function i need to check whether the index change has been caused by user or programatically.

    Deepak.


  • Moderators

    Yeah, just add a boolean to your update:
    @
    void YourClass::update(bool changeByUser = false);

    /// then
    combobox->blockSignals(true);
    // change the index here. Combobox will not send any signals!
    update(true);
    combobox->blockSignals(false);
    @



  • Some time ago I read you should not use blockSignals but disconnect instead. Is that true?


  • Moderators

    I don't know, I have not heard anything said against blockSignals, please provide a link if you have one - I'm interested in knowing the reason. Documentation does not hint at blockSignals being bad in any way.

    You can disconnect if you prefer, it takes just a bit more typing.



  • Of course if I can find the link again.



  • I have used blockSignals in multiple occasions for combo boxes as well as radio buttons for state changes. It has always worked perfect for me!



  • It is some time ago and I cannot find the link anymore. It is also possible that either the link or I was wrong.
    Apparently if no one knows such a fact it is not correct.


Log in to reply