1

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

Andreas
  • 636
  • 1
  • 12
  • 29
  • What do you mean when you say the function terminates? – vipw Nov 05 '13 at 10:07
  • All Parts after "s_serviceComboBox->removeItem(i) will not be execute – Andreas Nov 05 '13 at 11:21
  • 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 Nov 05 '13 at 12:27

1 Answers1

2

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));
}
Ruu
  • 1,245
  • 9
  • 9
  • I know what you meen, but I dont came to the addItems part because the method call terminates when the program calls s_serviceComboBox->clear(); So addItems will not be execute.. – Andreas Nov 05 '13 at 09:57
  • Do you mean sigfault under `terminates` ? If this is true, you dereferences NULL-pointer, but you claim that `isVisible()` passed. very very strange :) – Ruu Nov 05 '13 at 10:08
  • NULL-pointer is impossible because I can call the count() method.. It's really strange... – Andreas Nov 05 '13 at 10:13
  • Are you clearing from a slot called by currentIndexChanged() or somesuch, of the same combobox? Then try to delay it (return to the event loop before) See also here: http://stackoverflow.com/questions/4888189/how-delete-and-deletelater-works-wrt-to-signals-and-slots-in-qt/4889395#4889395 – Frank Osterfeld Nov 05 '13 at 10:47