Results 1 to 7 of 7

Thread: Select First Row as default in QTableView ?

  1. #1
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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:
    1.jpg

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

    2.jpg

    Thanks for Help.

    hohoanganh205 !

  2. #2
    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: Select First Row as default in QTableView ?

    Call setCurrentIndex() or, if you want the row in the selection, then call selectRow().

  3. The following user says thank you to ChrisW67 for this useful post:

    hohoanganh205 (19th December 2011)

  4. #3
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Select First Row as default in QTableView ?

    Quote Originally Posted by ChrisW67 View Post
    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 )

    3.jpg

  5. #4
    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: 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.

  6. The following user says thank you to ChrisW67 for this useful post:

    hohoanganh205 (19th December 2011)

  7. #5
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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 ) :

    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 !

  8. #6
    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: 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:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class TableView: public QTableView {
    5. Q_OBJECT
    6. public:
    7. TableView(QWidget *p = 0): QTableView(p) {
    8. setSelectionMode(SingleSelection);
    9. setSelectionBehavior(SelectRows);
    10. }
    11. protected slots:
    12. void currentChanged(const QModelIndex ¤t, const QModelIndex &previous) {
    13. selectRow(current.row());
    14. }
    15. };
    16.  
    17. int main(int argc, char *argv[])
    18. {
    19. QApplication app(argc, argv);
    20.  
    21. // Some test data
    22. QStandardItemModel model(3,2);
    23. for (int row = 0; row < 3; ++row) {
    24. for (int col = 0; col < 2; ++col) {
    25. QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(col));
    26. model.setItem(row, col, item);
    27. }
    28. }
    29.  
    30. TableView t;
    31. t.setModel(&model);
    32. t.show();
    33. return app.exec();
    34. }
    35. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to ChrisW67 for this useful post:

    hohoanganh205 (20th December 2011)

  10. #7
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Select First Row as default in QTableView ?

    Thanks ChrisW67 !

Similar Threads

  1. QTableView select a whole row?
    By qlands in forum Qt Programming
    Replies: 1
    Last Post: 22nd August 2011, 11:22
  2. Replies: 0
    Last Post: 19th January 2011, 03:29
  3. How select next row in QTableView
    By estanisgeyer in forum Qt Programming
    Replies: 8
    Last Post: 17th June 2009, 20:02
  4. Select row in QTableView after insert.
    By fede in forum Newbie
    Replies: 1
    Last Post: 14th April 2009, 15:18
  5. How to select a cell in a QTableView
    By JeanC in forum Qt Programming
    Replies: 6
    Last Post: 6th February 2008, 13:20

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.