PDA

View Full Version : QTableView and how to set first row to be always the default selected



cpuinthebrain
20th September 2012, 17:15
Hi,

I have a QWidget with QTableView.The program is written on way that if no row is selected the program crushes.
Because of this i want that the first row to be always selected as default.
Any suggestions?

Ashkan_s
20th September 2012, 18:48
You can set a row as selected using QTableView::selectRow().

cpuinthebrain
20th September 2012, 19:34
Strangely but it was the first thing i have tried.Unfortunately giving a parameter 1 for instance nothing happens.No row is selected.

cpuinthebrain
25th September 2012, 19:29
I'm wandering that nobody know why this doesn't select any row:

ui->tableView_clients->setSelectionMode(QAbstractItemView::SingleSelectio n);
ui->tableView_clients->selectRow(1);

HELP PLEASE

huilui
25th September 2012, 21:57
Hm, maybe you also need to define a selection behaviour (QAbstractItemView::setSelectionBehavior), in particular QAbstractItemView::SelectRows. I think the default is QAbstractItemView::SelectItems, and that in combination with QAbstractItemView::SingleSelection could pose a problem when trying to select a row with more than one column.

Also note, that the first row has index 0 and not 1 :)

cpuinthebrain
25th September 2012, 23:17
Thanks for the replay, but as default in the .ui file is set on selectionBehavior - SelectRows.Due to the 1 i know very well that everything in the programming start from 0, i just set to 1.
When opens nothing is selected.

ChrisW67
25th September 2012, 23:41
This small, self-contained example demonstrates that it does work:


#include <QtGui>
#include <QDebug>

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

QStandardItemModel model(4, 4);
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 4; ++column) {
QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
model.setItem(row, column, item);
}
}

QTableView v;
v.setModel(&model);
v.setSelectionMode(QAbstractItemView::SingleSelect ion);
v.setSelectionBehavior(QAbstractItemView::SelectRo ws);
v.selectRow(0);
v.show();

return app.exec();
}


If you select row 1 and there is only one row in the table, row 0, then of course nothing is selected. We are not privy to the data your model is producing or any of the other things your code might be doing to the model or view, so we cannot tell you how to fix your code.


The program is written on way that if no row is selected the program crushes. ... Any suggestions? This is the problem. Fix the program so that if there is no selected row in the table the user cannot trigger the event that would cause the program to "crush". Or, if you cannot stop the user triggering such events, make the offending code check for a selection and do nothing if one is not found (this is good defensive programming anyway). The alternative is to find everywhere a selection could be lost, for example deleting a row or resetting the model, and making sure you select something.