QTableView addressing data
-
I have a table view and model:
QStandardItemModel* mpsiSDmodel; QTableView* mptvSDrecs;
I create the table view and model and set model.
mptvSDrecs = new QTableView(this); mptvSDrecs->setSelectionBehaviour(QAbstractItemView::SelectRows); mptvSDrecs->setStyleSheet(DataSets::mscszTableViewStyle); mpsiSDmodel = new QStandardItemModel(this);
I've hidden the vertical headers and set style sheet for horizontal headers. I've connected a slot to the table view to manage double clicks. I've populated the table with rows and columns.
What I am struggling with is how to get access to the data in the slot when a row is double clicked, my lambda slot:
[this](const QModelIndex& crIndex) { QItemSelectionModel* pobjSModel(mptvSDrecs->selectionModel()); QModelIndexList objSRows(pobjSModel->selectedRows()); if ( objSRows.count() == 0 ) { return; } }
I'm finding it difficult to establish how I address a cell in the table or a specific column header. I've been looking around for examples of which there is lots of code but nothing specific.
-
Hi,
@SPlatten said in QTableView addressing data:
objSRows
As the documentation of selectedRows says:
Returns the indexes in the given column for the rows where all columns are selected.
By default it's the column zero, from the looks of it you are trying to use it as if your where given the complete rows which is not the case.
As already pointed in one of your other threads: use QModelIndex::siblingAtRow.
@SGaist , Yes, I can see whats happening now and I've checked the documentation it doesn't say anything that I could see.
When I call:
QModelIndexList objIndexes = pobjSDModel->selectedIndexes();
The actual index returned is base 1, the first row of data after the headers has a row index of 1, although you cannot use 1 to address the row in the QModelIndex::index function.
It may be that I've done something wrong that would cause this behaviour.
Have rewritten slot now to:
QItemSelectionModel* pobjModel(mptvSDrecs->selectionModel()); QModelIndexList objIndexes(pobjModel->selectedIndexes()); for( int intCol=0; intCol<obIndexes.length(); intCol++ ) { QModelIndex objIndex(objIndexes[intCol]), objCell = mpsiSDmodel->index(objIndex.row(), intCol); qDebug() << objCell; }
Now it works!
-
-
I have a table view and model:
QStandardItemModel* mpsiSDmodel; QTableView* mptvSDrecs;
I create the table view and model and set model.
mptvSDrecs = new QTableView(this); mptvSDrecs->setSelectionBehaviour(QAbstractItemView::SelectRows); mptvSDrecs->setStyleSheet(DataSets::mscszTableViewStyle); mpsiSDmodel = new QStandardItemModel(this);
I've hidden the vertical headers and set style sheet for horizontal headers. I've connected a slot to the table view to manage double clicks. I've populated the table with rows and columns.
What I am struggling with is how to get access to the data in the slot when a row is double clicked, my lambda slot:
[this](const QModelIndex& crIndex) { QItemSelectionModel* pobjSModel(mptvSDrecs->selectionModel()); QModelIndexList objSRows(pobjSModel->selectedRows()); if ( objSRows.count() == 0 ) { return; } }
I'm finding it difficult to establish how I address a cell in the table or a specific column header. I've been looking around for examples of which there is lots of code but nothing specific.
@SPlatten
Hi
Well when you are using Views, then data is in the model so you talk to the model to get it.https://doc.qt.io/qt-5/qstandarditemmodel.html#data
So you just use the ModelIndex with the data function to get or set data as you wish.
hehe ninja'd. Mr Ehrlicher was faster :)
-
@Christian-Ehrlicher said in QTableView addressing data:
I've added to my slot:
const int cintColumns(mpsiSDmodel->columnCount()); // 5 columns for( int intCol=0; intCol<cintColumns; intCol++ ) { QVariant varData(mpsiSDmodel->data(objSRows.at(intCol))); qDebug << varData; }
The first iteration of the above works and displays he correct data, the second call results in a crash, with:
ASSERT failure in QList<T>::at "index out of range", file [path]/qlist.h line 541
All of the data and the headers are displayed correctly.
-
And why do you tell us this instead checking the size of objSRows by yourself?
-
And why do you tell us this instead checking the size of objSRows by yourself?
@Christian-Ehrlicher , because I've spent hours doing just that and not getting anywhere as I said the data is displayed correctly I'm not getting anywhere.
The debugger isn't very useful, if I expand the QList<QModelIndex> it contains one item, thats as far as I get, I don't know why or how to fix it so it contains more...thats why I'm asking for assistance.
-
I really don't understand your problem. QList tells you that you give them an index which is out of it's size. So the only place where you access such a list is here
objSRows.at(intCol)
So the only reason can be that intCol is greater or equal the size of objSRows... basic c++ stuff.
-
Hi,
@SPlatten said in QTableView addressing data:
objSRows
As the documentation of selectedRows says:
Returns the indexes in the given column for the rows where all columns are selected.
By default it's the column zero, from the looks of it you are trying to use it as if your where given the complete rows which is not the case.
As already pointed in one of your other threads: use QModelIndex::siblingAtRow.
-
Hi,
@SPlatten said in QTableView addressing data:
objSRows
As the documentation of selectedRows says:
Returns the indexes in the given column for the rows where all columns are selected.
By default it's the column zero, from the looks of it you are trying to use it as if your where given the complete rows which is not the case.
As already pointed in one of your other threads: use QModelIndex::siblingAtRow.
@SGaist , in my code I've specified that a whole row is selected and visually when I click on the row the whole row is selected, the row contains 5 columns.
In the slot when I double click as my slot code shows I call the selectedRows function which returns an instance of QModelIndexList, however this only contains 1 item so is that the selected row ?
How to I expand on that 1 item to get the list of columns?
-
Hi,
@SPlatten said in QTableView addressing data:
objSRows
As the documentation of selectedRows says:
Returns the indexes in the given column for the rows where all columns are selected.
By default it's the column zero, from the looks of it you are trying to use it as if your where given the complete rows which is not the case.
As already pointed in one of your other threads: use QModelIndex::siblingAtRow.
-
Hi,
@SPlatten said in QTableView addressing data:
objSRows
As the documentation of selectedRows says:
Returns the indexes in the given column for the rows where all columns are selected.
By default it's the column zero, from the looks of it you are trying to use it as if your where given the complete rows which is not the case.
As already pointed in one of your other threads: use QModelIndex::siblingAtRow.
@SGaist , Yes, I can see whats happening now and I've checked the documentation it doesn't say anything that I could see.
When I call:
QModelIndexList objIndexes = pobjSDModel->selectedIndexes();
The actual index returned is base 1, the first row of data after the headers has a row index of 1, although you cannot use 1 to address the row in the QModelIndex::index function.
It may be that I've done something wrong that would cause this behaviour.
Have rewritten slot now to:
QItemSelectionModel* pobjModel(mptvSDrecs->selectionModel()); QModelIndexList objIndexes(pobjModel->selectedIndexes()); for( int intCol=0; intCol<obIndexes.length(); intCol++ ) { QModelIndex objIndex(objIndexes[intCol]), objCell = mpsiSDmodel->index(objIndex.row(), intCol); qDebug() << objCell; }
Now it works!
-
The method returns the indexes of the column requested for the rows that are completely selected. It's a "vertical" list since it's for a column.