Results 1 to 7 of 7

Thread: QTableView and how to set first row to be always the default selected

  1. #1
    Join Date
    Sep 2012
    Posts
    44
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QTableView and how to set first row to be always the default selected

    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?

  2. #2
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Thanks
    17
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QTableView and how to set first row to be always the default selected

    You can set a row as selected using QTableView::selectRow().

  3. #3
    Join Date
    Sep 2012
    Posts
    44
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTableView and how to set first row to be always the default selected

    Strangely but it was the first thing i have tried.Unfortunately giving a parameter 1 for instance nothing happens.No row is selected.

  4. #4
    Join Date
    Sep 2012
    Posts
    44
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTableView and how to set first row to be always the default selected

    I'm wandering that nobody know why this doesn't select any row:
    Qt Code:
    1. ui->tableView_clients->setSelectionMode(QAbstractItemView::SingleSelection);
    2. ui->tableView_clients->selectRow(1);
    To copy to clipboard, switch view to plain text mode 

    HELP PLEASE

  5. #5

    Default Re: QTableView and how to set first row to be always the default selected

    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

  6. #6
    Join Date
    Sep 2012
    Posts
    44
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTableView and how to set first row to be always the default selected

    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.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTableView and how to set first row to be always the default selected

    This small, self-contained example demonstrates that it does work:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QStandardItemModel model(4, 4);
    9. for (int row = 0; row < 4; ++row) {
    10. for (int column = 0; column < 4; ++column) {
    11. QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
    12. model.setItem(row, column, item);
    13. }
    14. }
    15.  
    16. v.setModel(&model);
    17. v.setSelectionMode(QAbstractItemView::SingleSelection);
    18. v.setSelectionBehavior(QAbstractItemView::SelectRows);
    19. v.selectRow(0);
    20. v.show();
    21.  
    22. return app.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 

    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.

Similar Threads

  1. QTableView get selected row
    By raphaelf in forum Qt Programming
    Replies: 8
    Last Post: 12th July 2013, 03:35
  2. QTableView selected a full row
    By pmjz in forum Qt Programming
    Replies: 0
    Last Post: 28th October 2011, 00:22
  3. Selected row bigger than others in QTableView
    By JuanMO in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2010, 16:48
  4. Deleting selected row from a QTableView
    By Ferric in forum Newbie
    Replies: 9
    Last Post: 15th January 2010, 08:32
  5. Set default Selected item in ListView (QSqlQueryModel)
    By jeffpogo in forum Qt Programming
    Replies: 0
    Last Post: 26th May 2009, 22:12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.