How to get the row value from Qtable widget
-
Actually i am inserting combo box in one column on my table and when one of the combo box from the table is selected i want to get the row value . How this can be done ?
void MainWindow::on_tableWidget_clicked(const QModelIndex &index)
{
qDebug("ROW %d",index.row());
}I tried something like this but it didnt give the row value where i have inserted the combo box and from other places it was returning the row value
-
void MainWindow::on_tableWidget_clicked(const QModelIndex &index) { qDebug() << "ROW: " << index.row(); qDebug() << "TEXT: " << itemFromIndex(index).text(); if(index.column() == COLUMN_COMBOBOX) //Check it is the "ComboBox" column //Do something with value }
-
@ManiRon Ok, here is another approach (I didn't test the code, it mays have errors)
//At initilization QComboBox *cb = new QComboBox; cb.setProperty("row", row); connect(cb, qOverload<int>(&QComboBox::currentIndexChanged), this, &MainWindow::onCbIndexChanged) myTableWidget->setCellWidget(row, column, cb); //... //On Combo Box Index changed void MainWindow::onCbIndexChanged(int index){ QObject *cb = sender(); const int row = cb->property("row"); int comboBoxSelectionIndex = index; qDebug() << "ROW: " << row; qDebug() << "Index: " << index << " value: " << cb.currentText(); }
inspired from here: https://www.qtcentre.org/threads/66573-How-to-get-signal-when-checkbox-in-table-cell-changed
-
Hi
Just as a note
Using a combo box delegate instead of setCellWidget have better performance and overall just feels
nicer than a floating widget. (IMHO)
basic example:
https://github.com/daviddoria/QtExamples/blob/master/Delegates/ComboBoxDelegate/ComboBoxDelegate.cpp -
@ManiRon said in How to get the row value from Qtable widget:
when i click on the combo box nothing is triggred
because
setIndexWidget
is PURE EVILFollow @mrjj 's suggestion. Ther's also a wiki page on this topic: https://wiki.qt.io/Combo_Boxes_in_Item_Views
-