How to change QTableView data based on selection in QComboBox.
-
I have a QTableView which gets its data from QAbstractTableModel and is a 2d array. Below the TableView I have two comboboxes. User first selects the row in the TableView. Now I want that when the user selects an option from the combobox, I want the selected option to update the display in the TableView selection.
Shown below is the UI. For example if the User Selects REF DES in the class combo box I want the selected row to update its class name to REF DES.
How do I achieve this?
.
-
@jaivardhanf
Hello and welcome.Attach a slot to the void QComboBox::currentTextChanged(const QString &text) signal. In the slot find which row is selected in the model and
setData()
the class column to the value from the combobox. -
So below is my code. I don't know why it is not updating. I implemented a slot called textchanged which gets emitted when currenttextchanged in my combo box.
void GetOptionsClass::TextChanged(const QString& text) {
int rowidx = ui->line_tableView->selectionModel()->currentIndex().row();
QModelIndex nIndex = model->index(rowidx, 2);
model->setData(nIndex, text, Qt::EditRole);
}Below is my setData and flags methods.
bool LineModel::setData(const QModelIndex& index, const QVariant& value, int role) {
if (role == Qt::EditRole && index.column() == 2 && index.isValid())
{
Line_map[index.row()][index.column()] = value.toString();
QModelIndex top = createIndex(index.row(), 0);
QModelIndex bottom = createIndex(index.row(), 3);emit dataChanged(top, bottom); return true; } return false;
}
Qt::ItemFlags LineModel::flags(const QModelIndex& index) const
{
return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
}This is how I connect the signal and slot
connect(ui->lineClass, &QComboBox::currentTextChanged, this, &GetOptionsClass::TextChanged);
Can you please point out what I am doing wrong
-
@jaivardhanf said in How to change QTableView data based on selection in QComboBox.:
So below is my code.
Something seems to have gone wrong with the code tags, making it hard to read and potentially missing code.
Qt::ItemFlags LineModel::flags(const QModelIndex& index) const
{
return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
}Adding the ItemIsEditable flag doesn't matter if the goal isn't to edit the item through the view.
This is how I connect the signal and slot
connect(ui->lineClass, &QComboBox::currentTextChanged, this, &GetOptionsClass::TextChanged);
Verify that the slot is called when it should be, and that the arguments are as expected.