Use QComboBox::setModel() to share one model with all combo boxes. When changing anything in tab 2, simply modify the model, and the combo boxes will update automatically. For your usecase, it seems like QStringListModel is a good model, although you can of course use more complex ones if needed.
QComboBox dynamic list of items
Question
I have two QTableWidget
s. Let's call them Tab1
and Tab2
. One column of Tab1
contains QComboBox
es where their items are rows from Tab2
. When elements of Tab2
are modified (added/removed/edited) I want all QComboBoxes
to update.
Tab1 concept
LineNo | Some Text | Select |
1 | QString | QComboBox |
2 | QString | QComboBox |
3 | QString | QComboBox |
| ... | ... |
n | QString | QComboBox |
Tab2 concept
LineNo | Some Text |
1 | QString |
2 | QString |
3 | QString |
| ... |
n | QString |
Currently QComboBoxe
s are filled like this:
QStringList s; // List filled with data from Tab2
QTableWidget *tw = (QTableWidget*) ui->Tabspace->currentWidget()->childAt(10, 10);
QComboBox *names = new QComboBox();
names->addItems(s);
tw->setItem(row, 0, new QTableWidgetItem());
tw->setCellWidget(row, 0, names);
This code is being launched every time I add row to Tab1
Easiest way would be change each QComboBox
whenever item content of Tab2 changes. My question is:
Is there a better way to achieve that? Maybe some way to pass pointer to addItems()
or modifying just one QComboBox
and cloning it?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow