Combobox in Qtableview
-
@VRonin said in Combobox in Qtableview:
for (int r = 0, maxI = table1->rowCount(); r < maxI; ++r) { //for all value rows
guesslist.append( table1->data(table1->index(r, 0)).toString() );//store value in stringlist
}guesslist is always updated,
for (int i = 0, maxI = table2->rowCount(); i < maxI; ++i)//for all rows { const QModelIndex idx = table2->index(i, 1); QComboBox* combo = qobject_cast<QComboBox*>(ui.tableView_2->indexWidget(idx)); if (!combo) { combo = new QComboBox(); // make combo ui.tableView_2->setIndexWidget(idx, combo);// add combo } colvallist1.removeDuplicates(); // strip dubs clear dublicates colvallist1.removeAll(QString("")); combo->setPlaceholderText(QString(" ")); combo->addItems(guesslist); // adding gueslist will add item value everytime so no need to pass this right ?? combo->removeItem();
iterate over the items in the combo, remove those that don't appear in guesslist and add those that appear in guesslist but are not already in the combo
item should store in another Qstringlist and then check with initial list ?
-
edited
In prev code i add these code lines, and now selection not changed when added new items in tableview column and updating in col combobox valuesfor (int i = 0; i < guesslist.count(); i++) { if (combo->findText(guesslist[i]) != -1)//-1 means "not found" { continue; } else { combo->addItem(guesslist[i]); //Add to QCombobox }
but not able to apply for "remove those t don't appear in guesslist (when col1 row values chnaged)" ??
-
@n-2204
So you have to one of:- Have another loop/iterator through all the items which are in the combo box and remove any which are not in the guess list now; or
- Clear out all the items in the combo box first, and then only add back in those which are now in guess list.
-
@JonB said in Combobox in Qtableview:
Clear out all the items in the combo box first, and then only add back in those which are now in guess list.
when i'm clearing all first then, whatever my prev selection in combobox it will be gone combo->clear();
Have another loop/iterator through all the items which are in the combo box and remove any which are not in the guess list now
tried this but not working correctly where to chnge ?,when I'm changing value in guesslist then the combo selection got changed
QStringList itemsInComboBox; for (int index = 0; index < guesslist.count(); index++) { itemsInComboBox << combo->itemText(index); if ( guesslist[index]!=itemsInComboBox[index] ) combo->removeItem(index); }
-
@n-2204 said in Combobox in Qtableview:
when i'm clearing all first then, whatever my prev selection in combobox it will be gone
combo->clear();Yes, and, so? If you want to preserve what was selected, note it and reselect.
Your loop for removing is no good. For one thing, you only check the same index in both guess list and combo box. If you do not clear the items, implement what I wrote:
Have another loop/iterator through all the items which are in the combo box and remove any which are not in the guess list now
That is just basic algorithmic stuff.
-
Have another loop/iterator through all the items which are in the combo box and remove any which are not in the guess list now
I tired this but not working correctly.. now if item in guesslist then also it got removed ???
QStringList itemsInComboBox; for (int index = 0; index < guesslist.count(); ++index) { //check in list for (int item = 0; item < combo->count(); ++item) { //check in combobox itemsInComboBox << combo->itemText(item); qDebug() << "before remove " << itemsInComboBox; if ( guesslist[index] != itemsInComboBox[item] ) combo->removeItem(item); } }
-
Hi,
adding those that appear in guesslist code-for (int i = 0; i < guesslist.count(); i++) { if (combo->findText(guesslist[i]) != -1)//-1 means "not found" { continue; } else { combo->addItem(guesslist[i]); //Add to QCombobox }
///remove which are not in the guess list code below I tried multiple times but this is not working correctly ? I don't want to change my selection when any item added / removed / replaced ..Plz help😞
QStringList itemsInComboBox; for (int index = 0; index < guesslist.count(); ++index) { //check in list for (int item = 0; item < combo->count(); ++item) { //check in combobox itemsInComboBox << combo->itemText(item); qDebug() << "before remove " << itemsInComboBox; if ( guesslist[index] != itemsInComboBox[item] ) combo->removeItem(item); } }
Thanks in advance
-
-
@n-2204 https://doc.qt.io/qt-5/qcombobox.html#currentIndex-prop or currentText() if you can't relay on the order of the items or itemData() if you use roles, later on you can find the same text with different index, if that's the case, using findText() or findData().
Do you read any documentation at all? It's all there in clear text.
-
@JonB said in Combobox in Qtableview:
The whole thing, for what I think you say you want:
QString selected = combo->currentText();
combo->clear();
combo->addItems(guesslist);
combo->setCurrentText(selected);
edited
thanks it works,, there some other mistake in code -
@artwaw said in Combobox in Qtableview:
currentText() if you can't relay on the order of the items or itemData() if you use roles, later on you can find the same text with different index, if that's the case, using findText() or findData().Do you read any documentation at all? It's all there in clear text.
Thanks,,i trying to implement but thing is removeitem+selection from combobox not working correctly
-
@n-2204 said in Combobox in Qtableview:
currentText() if you can't relay on the order of the items or itemData() if you use roles, later on you can find the same text with different index, if that's the case, using findText() or findData().
From my code all you need to restore the selection is the
combo->setCurrentText(selected);
.