Results 1 to 7 of 7

Thread: Detecting row selection in a QTableView

  1. #1
    Join Date
    Jun 2010
    Location
    Cincinnati, Ohio, USA
    Posts
    92
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question Detecting row selection in a QTableView

    How does a QTableView signal/notify that the whole row has been selected and unselected? Not just a cell, but when the user clicks on the 'virtual row header' which is to the left of the first cell in the row.

    The goal is this: when a user selects a whole row, enable a button on the form which will allow the user to act upon the selected row. It is also important to know when no row is selected, so the button is not enabled.

    Might there be some way to wire this up between the QTableView's model and the button?

    Sam

  2. #2
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Detecting row selection in a QTableView

    This works for me
    Qt Code:
    1. connect(view, SIGNAL(clicked(const QModelIndex&)), this, SLOT(recordSelected()));
    2.  
    3. //then
    4. void MainWindow::recordSelected()
    5. {
    6. QModelIndex index = view->currentIndex();
    7. QSqlRecord record;
    8. int i = index.row(); // now you know which record was selected
    9. record = model->record(i);
    10. // and I do something with the record
    11. }
    To copy to clipboard, switch view to plain text mode 

    So what do you need the button for then?

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

    Cupidvogel (13th June 2015)

  4. #3
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: Detecting row selection in a QTableView

    maybe, subclassing QTableView can achieve what you want.

    tableview.h :

    Qt Code:
    1. #ifndef TABLEVIEW_H
    2. #define TABLEVIEW_H
    3.  
    4. #include <QTableView>
    5.  
    6. class TableView : public QTableView
    7. {
    8. Q_OBJECT
    9. public:
    10. TableView(QWidget *parent = 0);
    11.  
    12. protected:
    13. void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
    14.  
    15. signals:
    16. void rowSelected(int row_num);
    17. void rowSelected(bool val);
    18. };
    19.  
    20. #endif // TABLEVIEW_H
    To copy to clipboard, switch view to plain text mode 

    tableview.cpp :

    Qt Code:
    1. #include "tableview.h"
    2. #include <QItemSelection>
    3. #include <QHeaderView>
    4.  
    5. TableView::TableView(QWidget *parent) : QTableView(parent)
    6. {
    7. }
    8.  
    9. void TableView::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
    10. {
    11. QTableView::selectionChanged(selected, deselected);
    12.  
    13. if(model() != 0)
    14. {
    15. if(selectedIndexes().count() == 0)
    16. {
    17. emit rowSelected(false);
    18. return;
    19. }
    20.  
    21. int count = 0;
    22. int row = selectedIndexes().at(0).row();
    23.  
    24. foreach(QModelIndex m, selectedIndexes()) {
    25. if(m.row() == row)
    26. count++;
    27. else
    28. return;
    29. }
    30.  
    31. if(count == horizontalHeader()->count())
    32. {
    33. emit rowSelected(true);
    34. emit rowSelected(row);
    35. }
    36. else
    37. {
    38. emit rowSelected(false);
    39. }
    40. }
    41. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp :

    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QVBoxLayout>
    4. #include <QFileSystemModel>
    5. #include "tableview.h"
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication app(argc, argv);
    10.  
    11. QVBoxLayout *lay = new QVBoxLayout(&w);
    12. QPushButton *button = new QPushButton("Go");
    13. button->setEnabled(false);
    14.  
    15. TableView *v = new TableView;
    16. QFileSystemModel model;
    17. model.setRootPath("C:/");
    18. v->setModel(&model);
    19.  
    20. lay->addWidget(v);
    21. lay->addWidget(button);
    22.  
    23. QObject::connect(v, SIGNAL(rowSelected(bool)), button, SLOT(setEnabled(bool)));
    24. w.show();
    25.  
    26. return app.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jun 2010
    Location
    Cincinnati, Ohio, USA
    Posts
    92
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Detecting row selection in a QTableView

    Well, that works for me as long as I click in a cell.

    I am calling the little box to the LEFT of the first cell, where the table displays the row number, the "row header". When I click on this "row header", the slot is never called. I played around a bit more and discovered that selectionBehavior was set to QAbstractItemView::SelectRows. When I changed it to QAbstractItemView::SelectItems, then it was not possible to select the "row header".

    I want to differentiate between clicking on a cell in a row and selecting the whole row. Is that possible?

    Sam

  6. #5
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Detecting row selection in a QTableView

    Take a look at the signals emitted by QHeaderView.
    For instance, you could connect QTableView::verticalHeader()->sectionPressed() to a slot that enables your button.

  7. #6
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: Detecting row selection in a QTableView

    how about connecting the vertical header view to your slot?

    for example:

    Qt Code:
    1. connect(tableView->verticalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(yourCustomSlot(int)));
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to saa7_go for this useful post:

    scarleton (8th June 2010)

  9. #7
    Join Date
    Jun 2010
    Location
    Cincinnati, Ohio, USA
    Posts
    92
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Detecting row selection in a QTableView

    Thank you, that was it, getting the signal off the header.

Similar Threads

  1. Help with QTableView row selection
    By ScabrusMantra in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2010, 21:44
  2. Replies: 2
    Last Post: 26th November 2009, 04:45
  3. QTableView Row Selection
    By ToddAtWSU in forum Qt Programming
    Replies: 8
    Last Post: 12th December 2007, 21:27
  4. QTableView row selection
    By davemar in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2007, 14:39
  5. CheckBox and selection in QTableView
    By Mike Krus in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2006, 20:31

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
  •  
Qt is a trademark of The Qt Company.