Results 1 to 8 of 8

Thread: QTableView item selected signal?

  1. #1
    Join Date
    Jul 2010
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QTableView item selected signal?

    Hi Folks...

    I'm having an issue figuring out what signals are emitted by a QTableView when the user performs various actions for selecting a row (my table is configured for row selection mode, single selection)...

    I'm connecting to the clicked() signal and can pick up on row selection when the user clicks on a row - all fine and well. However, if the user then uses the up/down arrow keys to select new rows, the clicked() signal is not emitted - as I suppose would be expected.

    Why is it that there just doesn't seem to be a signal letting us know that the set of selected items has changed? Am I just overlooking this somewhere in the docs?

    Thanks!

    Btw, I'm using Qt 4.6 on Windows

    -G

  2. #2
    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: QTableView item selected signal?

    Quote Originally Posted by sherbs View Post
    Am I just overlooking this somewhere in the docs?
    Yup, look at QAbstractItemView::selectionModel() and QItemSelectionModel.
    Last edited by saa7_go; 30th July 2010 at 14:38. Reason: updated contents

  3. #3
    Join Date
    Jul 2010
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView item selected signal?

    Thanks... that helps a lot!

  4. #4
    Join Date
    Jul 2010
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView item selected signal?

    I finally got around to implementing this and ran into an unexpected issue:

    QObject::connect: Cannot connect (null)::currentRowChanged(QModelIndex,QModelIndex) to MainWindow::on_tableViewTriggerSelectionModel_curr entRowChanged(QModelIndex,QModelIndex)

    My code for doing the connection is as follows:


    QItemSelectionModel *sm = ui->tableViewTrigger->selectionModel();
    connect(sm, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)) ,
    this, SLOT(on_tableViewTriggerSelectionModel_currentRowC hanged(QModelIndex,QModelIndex)));



    Any idea why the selection model is coming back as null? I'm using QtCreator to design/build... in that I'm setting selectionMode to SingleSelection and selectionBehavior to SelectRows

    Do I need to explicitly create the selection model?

    Thanks for any help!

  5. #5
    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: QTableView item selected signal?

    Quote Originally Posted by sherbs View Post
    Qt Code:
    1. QItemSelectionModel *sm = ui->tableViewTrigger->selectionModel();
    2. connect(sm, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
    3. this, SLOT(on_tableViewTriggerSelectionModel_currentRowChanged(QModelIndex,QModelIndex)));
    To copy to clipboard, switch view to plain text mode 
    I guess you set the model after the code above. Try to set the model first.

    Qt Code:
    1. ui->tableViewTrigger->setModel(yourModel);
    2. QItemSelectionModel *sm = ui->tableViewTrigger->selectionModel();
    3. connect(sm, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
    4. this, SLOT(on_tableViewTriggerSelectionModel_currentRowChanged(QModelIndex,QModelIndex)));
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jul 2010
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView item selected signal?

    Here is the order in which things are happening (simplified somewhat for clarity):

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. {
    3. ui->setupUi(this);
    4. this->_initializeTables();
    5. }
    To copy to clipboard, switch view to plain text mode 

    setupUi() is doing the following:
    Qt Code:
    1. tableViewTrigger->setSelectionMode(QAbstractItemView::SingleSelection);
    2. tableViewTrigger->setSelectionBehavior(QAbstractItemView::SelectRows);
    To copy to clipboard, switch view to plain text mode 

    and my initialization routine for my tables does this:
    Qt Code:
    1. void MainWindow::_initializeTables()
    2. {
    3.  
    4. QItemSelectionModel *sm = ui->tableViewTrigger->selectionModel();
    5. connect(sm, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
    6. this, SLOT(on_tableViewTriggerSelectionModel_currentRowChanged(QModelIndex,QModelIndex)));
    7. }
    To copy to clipboard, switch view to plain text mode 

    I was under the impression that setting the SelectionMode and SelectionBehavior would be something that manipulated a default sort of SelectionModel...

    Apparently these two things are not the same and that a SelectionModel must be created by the programmer to do this? In a way, I find that kind of surprising since selecting items from a view of any sort is a common task.

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTableView item selected signal?

    You don't have to create a selection model by yourown. Qt does it as soon as you set a (data)model to the view. Qt does it at that time because if the view is empty no selection model is needed. As soon as it is needed it creates one with you specified mode and behavior.

  8. #8
    Join Date
    Jul 2010
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView item selected signal?

    Thanks much!

    That was the problem...

    I was setting the model for the table view *after* making that connection... Setting the model prior did the trick.

    -G

Similar Threads

  1. QTableView Signal when nothing is selected
    By djjkotze in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2010, 15:00
  2. popupmenu for QTreeWidget's item when a particular item selected.
    By vinod sharma in forum Best Practices in Qt Programming
    Replies: 1
    Last Post: 22nd January 2010, 10:45
  3. Replies: 1
    Last Post: 20th January 2010, 08:38
  4. QListview set selected item
    By Freeman551 in forum Qt Programming
    Replies: 1
    Last Post: 25th December 2009, 00:17
  5. How to set selected item of a QListWidget?
    By Lawand in forum Qt Programming
    Replies: 9
    Last Post: 5th April 2009, 11:23

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.