Combobox in Qtableview
-
Creating a combobox in Qtableview2 column 1 and passing values from Qtableview1 column1
so i am storing column1 table1 values in Qstring and passing to combobox add item but combobox values are not updating i something i missed code is shared or need to create any slot to update combobox values based on user input on table1 column1QAbstractItemModel* table1 = ui.tableView->model(); QAbstractItemModel* table2 = ui.tableView_2->model(); for (int i = 0, maxI = table1->rowCount();i <= maxI;++i) { QComboBox* combo = new QComboBox(); for (int r= 0, maxI = table1->rowCount();r < maxI;++r) { QString list1[20] = { table1->data(table1->index(r, 1)).toString() }; for (int j = 0; j< 10; j++) { combo->addItem(list1[j]); } } ui.tableView_2->setIndexWidget(ui.tableView_2->model()->index(i, 1), combo); }
Thanks in advance
-
-
Your approach is wrong
If you still want to use it, as in your other questions, you have to connect a slot to the
rowsInserted
signal -
using this rowinserted signal even not going on this slot
connect(ui.tableView->model(), SIGNAL(rowsInserted), ui.tableView_2->model(), SLOT(changedvalue())); void tool::changedvalue() { QAbstractItemModel* table1 = ui.tableView->model(); QAbstractItemModel* table2 = ui.tableView_2->model(); for (int i = 0, maxI = table2->rowCount();i <= maxI;++i)//combobox in table2 col1 { for (int r = 0, maxI = table1->rowCount();r < maxI;++r)//getvalue fromtable1 { QComboBox* combo = new QComboBox(); QString list1[10] = { table1->data(table1->index(r, 0)).toString() };//store value in list qDebug() << "combox values1" ; for (int j = 0; j < 10; j++) { combo->addItem(list1[j]);/pass list to combobox qDebug() << "combox values" << list1[j];/ } ui.tableView_2->setIndexWidget(ui.tableView_2->model()->index(i, 1), combo); } } }
-
@n-2204 said in Combobox in Qtableview:
connect(ui.tableView->model(), SIGNAL(rowsInserted), ui.tableView_2->model(), SLOT(changedvalue()));
This is wrong - again: use the new signal/slot syntax to catch such errors on compile time. And if you don't want then watch the output to stdout during debugging!
-
@Christian-Ehrlicher
I, and you, have repeatedly advised @n-2204 to change from the old macros to the new style, and provided the necessary links (e.g. https://forum.qt.io/topic/126193/spinbox-input-add-rows, I think we posted 4 times there to this effect). Precisely because the user keeps getting the connections wrong. It seems the user is not interested in making the effort to change their ways, and prefers to make the same mistakes and keep posting for others to sort out.... -
-
but signal/slot should work for both the ways
It does but in case you make a mistake the new syntax lets you know straight away when you try to compile and it's not just a warning in the console while the program runs.
Also the new connection is fasterthere is change in column 1 row values and @VRonin you told to use signal rowinserted() ?
In that case you would use the conveniently named
dataChanged
signalvoid tool::changedvalue()
please do no rebuild all the widgets everytime something changes.
setIndexWidget
is already super inefficient, if you keep recreating them your program will probably stutter a lot -
So when i am passing my values in a Qstring that time its coming in combobox but same when i try using table1 column1 rows value that time only last row value is adding in Combobox values looks like all values not appending
Where need to make changes?connect(ui.tableView->model(), &QAbstractItemModel::dataChanged, this, &Gearcycle_model::changedvalue); void tool::changedvalue() { QAbstractItemModel* table1 = ui.tableView->model(); QAbstractItemModel* table2 = ui.tableView_2->model(); for (int i = 0, maxI = table2->rowCount();i <= maxI;++i)//combobox in table2 col1 { for (int r = 0, maxI = table1->rowCount();r < maxI;++r)//getvalue fromtable1 { QComboBox* combo = new QComboBox(); //QString list1[10] = { table1->data(table1->index(r, 0)).toString() };//store value in list QString list1[4] = { "input 1","input 2","input3","input 4" }; qDebug() << "table index" << r; for (int j = 0; j < 3; j++) { combo->addItem(list1[j]); qDebug() << "combox values" << list1[j];//pass list to combobox qDebug() << "list j=" << j; } ui.tableView_2->setIndexWidget(ui.tableView_2->model()->index(i, 1), combo); qDebug() << "row value i=" << i; } } }
-
@n-2204 said in Combobox in Qtableview:
So when i am passing my values in a Qstring that time its coming in combobox but same when i try using table1 column1 rows value that time only last row value is adding in Combobox values looks like all values not appending
Where need to make changes?connect(ui.tableView->model(), &QAbstractItemModel::dataChanged, this, &Gearcycle_model::changedvalue); void tool::changedvalue() { QAbstractItemModel* table1 = ui.tableView->model(); QAbstractItemModel* table2 = ui.tableView_2->model(); for (int i = 0, maxI = table2->rowCount();i <= maxI;++i)//combobox in table2 col1 { for (int r = 0, maxI = table1->rowCount();r < maxI;++r)//getvalue fromtable1 { QComboBox* combo = new QComboBox(); //QString list1[10] = { table1->data(table1->index(r, 0)).toString() };//store value in list
when passing this that time only last row value is coming in combobox
QString list1[4] = { "input 1","input 2","input3","input 4" };
this is working when i am passing QString list1[4] = { "input 1","input 2","input3","input 4" }; in combobox
for (int j = 0; j < 3; j++) { combo->addItem(list1[j]); qDebug() << "combox values" << list1[j];//pass list to combobox qDebug() << "list j=" << j; } ui.tableView_2->setIndexWidget(ui.tableView_2->model()->index(i, 1), combo); qDebug() << "row value i=" << i; } } }
yes, looks like some small change will be needed during addItem in combobox
-
ya it works thankyou.
void tool::rowvalue1() { QAbstractItemModel* table1 = ui.tableView->model(); QAbstractItemModel* table2 = ui.tableView_2->model(); for (int i = 0, maxI = table2->rowCount();i < maxI;++i)//combobox in table2 col1 { QComboBox* combo = new QComboBox(); combo->setDuplicatesEnabled(false); for (int r = 0, maxI = table1->rowCount();r < maxI;++r)//getvalue fromtable1 { QString list1[10] = { table1->data(table1->index(r, 0)).toString() };//store value in list for (int j = 0; j < 1; ++j) {
--using uniqueSet trying to add only unique values(no duplicate values) in Combobox but its not working is this correct way ? or any other way to do this ?
QSet<QString> uniqueSet; if (!uniqueSet.contains(list1[j]))//add only unique values in combobox { uniqueSet.insert(list1[j]); combo->addItem(list1[j]); //add list to combobox qDebug() << " values " << list1[j]; } } ui.tableView_2->setIndexWidget(table2->index(i, 1), combo); } } }