QTableView - add named row
-
30-06-2021 - |
Question
Im using a using a QTableView
in conjuction with table model derived from QAbstaractTableModel
. Now I want to insert rows/columns (which represent states/input chars) but I need to pass a string as a label (which is then shown in the HeaderView) for state/input char down to my data classes.
bool TransTableModel::insertRows(int position, int rows, const QModelIndex &index)
{
beginInsertRows(index, position, position+rows-1);
garage->addUsedState("q6");
endInsertRows();
}
TransTableModel
is my custom model. My problem: how do I obtain this string from user? In this case I hardcoded "q6"
as a label, but I need this to be user-supplied. Is there a way? Should I open an input dialog? From the model class?
Solution
If the model needs that data, it should be provided by the view which could then open an input dialog.
You can add a member function to your model to pass that extra parameter and call it from the view class instead of calling the "regular" insertRows
(you don't have to call insertRows
in that new function either, just beginInsertRows
and endInsertRows
).