Call setCurrentIndex() or, if you want the row in the selection, then call selectRow().
hohoanganh205 (19th December 2011)
Can you explain clearly, i can not understand, i am just a beginer, I searched a code :
QModelIndex newIndex = ui.tabview->model()->index(0,2); // index(row, column)
ui.tabview->selectionModel()->select(newIndex, QItemSelectionModel::Select);
But only select a cell, not a row : ( Thanks for Help )
3.jpg
It only selected a cell because that is what you added to the selection: the index of a single cell.
I gave you a quick way to select a whole row.
Selection is governed by a few properties in QAbstractItemView. The selectionMode() defaults to QAbstractItemView::SingleSelection, and the selectionBehavior() is QAbstractItemView::SelectItems. If you want the selection to always include the whole row then change the selectionBehavior() to QAbstractItemView::SelectRows.
BTW: There is a difference between the current cell (i.e. the one you can edit) and the selection also.
hohoanganh205 (19th December 2011)
Hi brother, I changed code:
QModelIndex newIndex = ui.tabview->model()->index(0,2); // index(row, column)
ui.tabview->selectionModel()->select(newIndex, QItemSelectionModel::Select);
to:
QModelIndex index = ui.tabview->model()->index(0,2);
ui.tabview->setCurrentIndex(index);
and it work when Form loaded, 1 row selected but it just select row and data not display on LeniEdit ( not like clicked event is row selected and data display on LineEdit ) :
4.jpg
I must to add a event function clicked of tableview ( on_tabview_clicked(); ), Then data displayed:
5.jpg
BUT i think code not right, although code is work because the colour of row selected when Form Loaded is not like the colour of row clicked:
6.jpg
I reading yours sent but it really hard to understand for me
Thanks for Helpping and Reading !
hohoanganh205 !
The selection and the current cell are two different things and are highlighted different ways.
If you want to keep them in synchronisation then you could do it in the currentIndexChanged() slot of the view.
A complete example:
Qt Code:
#include <QtGui> #include <QDebug> Q_OBJECT public: setSelectionMode(SingleSelection); setSelectionBehavior(SelectRows); } protected slots: selectRow(current.row()); } }; int main(int argc, char *argv[]) { // Some test data for (int row = 0; row < 3; ++row) { for (int col = 0; col < 2; ++col) { model.setItem(row, col, item); } } TableView t; t.setModel(&model); t.show(); return app.exec(); } #include "main.moc"To copy to clipboard, switch view to plain text mode
hohoanganh205 (20th December 2011)
Thanks ChrisW67 !
Bookmarks