Question

I want to remove all items from the QComboBox. I tryed it with this code:

void refreshServiceComboBox(std::vector<QString> service){
   if ( !s_serviceComboBox->isVisible() ){
      s_serviceComboBox->setVisible( true );
   }

   int numberOfItems = s_serviceComboBox->count();

   for (int i = (numberOfItems-1); i >= 0 ; i--){
       s_serviceComboBox->removeItem(i);
   }


   for (int u = 0; u < service.size(); u++){
       std::cout << "Service: " << service[u].toStdString() << std::endl;   
   }

   // 
   for (unsigned int n = 0; n < service.size(); n++){
       s_serviceComboBox->addItem(service[n]);
   }
}

First call of the method works but on the secound call it doesn't work. The function terminates by call "s_serviceComboBox->removeItem(i);" s_serviceComboBox is a class element and was created with new. Somebody knows a solution?

Edit:

Hi again, problem was that on the call of s_serviceComboBox->addItems the program jumps to the connect(Widget, SIGNAL(), Widget, SLOT()) execute this and after this jump back to the postion of s_serviceComboBox->addItems... During this jump they override some stuff. When I use s_serviceComboBox->blockSignal(true) by enter the function and s_serviceComboBox->blockSignal(false) it works withou problems Thanks for help! Btw. I use your kind of Method to refresh the QComboBox

Was it helpful?

Solution

You should try clear combobox with method(slot) QComboBox::clear() description clear

And then simply append all items with QComboBox::addItems description addItems

void refreshServiceComboBox(const std::list<QString> &service) // better QStringList
{
    if (!s_serviceComboBox->isVisible())
        s_serviceComboBox->setVisible(true);

    s_serviceComboBox->clear();
    s_serviceComboBox->addItems(QList::fromStdList(service));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top