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

Is there a way to distinguish between programmatic index changes and user selection index changes?



  • I have a QComboBox. I have two use cases. In one use case, the combo box is programmatically changed to have a new index via setCurrentIndex(). In the other use case, the user clicks and selects a new combo box selection with the mouse.

    Both of these use cases trigger the QComboBox::currentIndexChanged(int) signal. This is a major problem for the code I am trying to port. In the old framework (not Qt), a similar callback mechanism would be called only if the user selected an item and not if the index programmatically changed.

    How can I mimic this behavior in Qt?



  • I think you need to combine "void QComboBox::activated(int index)":http://qt-project.org/doc/qt-5/qcombobox.html#activated signal and a knowledge of a current index.



  • Hello,

    maybe when You call setCurrentIndex() You can use "QObject::blockSignals":http://qt-project.org/doc/qt-4.8/qobject.html#blockSignals method to disable emitting the QComboBox::currentIndexChanged(int) signal?

    Here's a little code how to do it:
    @pCombo->blockSignals(true);
    pCombo->setCurrentIndex(1);
    pCombo->blockSignals(false); // important to enable signals
    @

    I used this solution some time ago for similar situation and it works perfectly.

    with best regards,
    poorBob


Log in to reply