1
votes

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

1
What do you mean when you say the function terminates?vipw
All Parts after "s_serviceComboBox->removeItem(i) will not be executeAndreas
You should rephrase your question to ask why does QComboBox::removeItem() crash. The answer will be that you can't call the removeItem() call from an signal caused by the item. That's because it's not safe to destroy the signal's source from a slot.vipw

1 Answers

2
votes

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));
}