PDA

View Full Version : Select First Row as default in QTableView ?



hohoanganh205
18th December 2011, 20:08
Hi everyone, I Load a Form and tableview show data(mysql) but tableview not select as default a row like image below:
7182

How to make Form loaded and tableview selected a row like image below:

7183

Thanks for Help.

hohoanganh205 !

ChrisW67
18th December 2011, 22:41
Call setCurrentIndex() or, if you want the row in the selection, then call selectRow().

hohoanganh205
19th December 2011, 05:06
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 )

7185

ChrisW67
19th December 2011, 06:16
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, 08:08
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 ) :

7186

I must to add a event function clicked of tableview ( on_tabview_clicked(); ), Then data displayed:

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:

7188

I reading yours sent but it really hard to understand for me :(

Thanks for Helpping and Reading !

hohoanganh205 !

ChrisW67
20th December 2011, 01:22
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:


#include <QtGui>
#include <QDebug>

class TableView: public QTableView {
Q_OBJECT
public:
TableView(QWidget *p = 0): QTableView(p) {
setSelectionMode(SingleSelection);
setSelectionBehavior(SelectRows);
}
protected slots:
void currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
selectRow(current.row());
}
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

// Some test data
QStandardItemModel model(3,2);
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 2; ++col) {
QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(col));
model.setItem(row, col, item);
}
}

TableView t;
t.setModel(&model);
t.show();
return app.exec();
}
#include "main.moc"

hohoanganh205
22nd December 2011, 09:47
Thanks ChrisW67 !