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); itemModel->setItem(itemModel->rowCount(),0,items.last()); } setModel(itemModel);
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 model()->removeRow(row); showPopup();
And my questions:
- Need I that QVector with QStandardItems or can I only have items in model?
- Have I to delete all items in QStandardItemModel or QStandardItemModel deletes items like QVector ( I haven't do anything )
- model()->removeRow(row) <- really delete row or only change parent like takeAt in QGridLayout?
-
Hi
You can get the pushbutton with
https://doc.qt.io/qt-5/qabstractitemview.html#indexWidget-
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. -
The model owns the items and will delete them so no manually deleting is needed.
-
https://doc.qt.io/qt-5/qstandarditem.html#removeRow
"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); itemModel->setItem(itemModel->rowCount(),0,tmp); }
EDIT: And one more:
What with QPushButtons in model? Have I delete them?