Search code examples
c++qtqt5qcombobox

combo box connect signal should not be triggered when the combo box is empty


I have connected a combo box to a slot on currentIndexChanged value.

The issue i am facing is that when i clear all the items from the combo box than also the event gets triggered.

Whenever i clear all the items from the combo box i do not want the function to execute.

what i though of was to add a if condition when the number of elements of the combo box is 0 , but i could not find any member function in the combo box that would return the number of elements in the combo box.

If there is a better way to do this please let me know.

 connect(comboBoxFunctions, qOverload<int>(&QComboBox::currentIndexChanged), 
 this, &WavefrontRenderer::AddContainerFunctionProperty);

Solution

  • A simple solution is to block the signals with blockSignals() while cleaning the QComboBox:

    comboBoxFunctions->blockSignals(true);
    comboBoxFunctions->clear();
    comboBoxFunctions->blockSignals(false);