2 Attachment(s)
Select First Row as default in QTableView ?
Hi everyone, I Load a Form and tableview show data(mysql) but tableview not select as default a row like image below:
Attachment 7182
How to make Form loaded and tableview selected a row like image below:
Attachment 7183
Thanks for Help.
hohoanganh205 !
Re: Select First Row as default in QTableView ?
Call setCurrentIndex() or, if you want the row in the selection, then call selectRow().
1 Attachment(s)
Re: Select First Row as default in QTableView ?
Quote:
Originally Posted by
ChrisW67
Call setCurrentIndex() or, if you want the row in the selection, then call selectRow().
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 )
Attachment 7185
Re: Select First Row as default in QTableView ?
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.
3 Attachment(s)
Re: Select First Row as default in QTableView ?
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 ) :
Attachment 7186
I must to add a event function clicked of tableview ( on_tabview_clicked(); ), Then data displayed:
Attachment 7187
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:
Attachment 7188
I reading yours sent but it really hard to understand for me :(
Thanks for Helpping and Reading !
hohoanganh205 !
Re: Select First Row as default in QTableView ?
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:
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"
Re: Select First Row as default in QTableView ?