Important: Please read the Qt Code of Conduct -

How can I remove rows from QComboBox?

  • Hi,

    I have subclass of QComboBox, where I set model ( QStandardItemModel ) with QStandardItems.

    Now I have a vector with QStandardItems ( his name is items ):

    // ( in QComboBox subclass constructor ):
        itemModel = new QStandardItemModel;
        for(auto text: itemsText)
            items.append(new QStandardItem(text));
            items.last()->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
            items.last()->setData(Qt::Unchecked, Qt::CheckStateRole);

    Now I setIndexWidget in view() - QPushButtons. So all my items in QComboBox have widgets on it.

    // for example:
        QModelIndex index = model()->index(0,0);
        view()->setIndexWidget(index, button);

    Next I would like to delete in a proper way item from model and from QComboBox when I clicked on this button and delete that buttons. Now my code is something like:

    int row = getItemRow();    <= here I get row, where is that QPushButton, which I clicked

    And my questions:

    1. Need I that QVector with QStandardItems or can I only have items in model?
    2. Have I to delete all items in QStandardItemModel or QStandardItemModel deletes items like QVector ( I haven't do anything )
    3. model()->removeRow(row) <- really delete row or only change parent like takeAt in QGridLayout?

  • Lifetime Qt Champion

    You can get the pushbutton with

    1. The model owns the items and unless you need to keep track of them for other reasons ( like update text)
      i dont think there is any reason to have a list also besides the model.

    2. The model owns the items and will delete them so no manually deleting is needed.

      "The items that were in the row are deleted."

  • @mrjj Thank you :)

    Only one more question. How can I add items to model not using vector? Something like that:

        for(auto text: itemsText)
            QStandardItem *tmp = new QStandardItem(text);
            tmp->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
            tmp->setData(Qt::Unchecked, Qt::CheckStateRole);

    EDIT: And one more:

    What with QPushButtons in model? Have I delete them?

Log in to reply