Results 1 to 2 of 2

Thread: Get selected index of a QListView by using installEventFilter its viewport

  1. #1
    Join Date
    Jan 2011
    Location
    Munich, Germany
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Get selected index of a QListView by using installEventFilter its viewport

    Hello,

    I am currently programming a widget [called CameraWidget] which contains a QListView [called m_cameraListView] and a QTreeView [called m_parameterTreeView]. The m_parameterTreeView should update according to the selection made in the m_cameraListView. My current strategy is to let the CameraWidget control all events on its children by using installEventFilter:

    Qt Code:
    1. CameraWidget::CameraWidget(QWidget *parent, Qt::WFlags flags)
    2. {
    3. m_model = new CameraTreeModel();
    4.  
    5. m_mainLayout = new QVBoxLayout();
    6. this->setLayout(m_mainLayout);
    7.  
    8. m_grabButton = new QPushButton("Grab image");
    9. m_mainLayout->addWidget(m_grabButton);
    10.  
    11. m_cameraListView = new QListView();
    12. m_cameraListView->setModel(m_model);
    13. m_mainLayout->addWidget(m_cameraListView);
    14.  
    15. m_parameterTreeView = new QTreeView();
    16. // [...]
    17. m_mainLayout->addWidget(m_parameterTreeView);
    18.  
    19. m_cameraListView->viewport()->installEventFilter(this);
    20. }
    To copy to clipboard, switch view to plain text mode 

    This is how I have seen it done in several tutorials (citation needed?) and I hope that the general approach is correct... However, in my event filter, I can't seem to get the selected item when a list item of m_cameraListView is clicked! It seems that the viewport() doesn't have the SelectedIndexes() method.

    Being relatively new to Qt programming, I am kinda stuck here... I would appreciate any advice very much! Thanks in advance!

    --
    Johannes


    Added after 20 minutes:


    Just to provide some useful information on what I'm trying to do:

    Qt Code:
    1. bool CameraWidget::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. if(event->type() == QEvent::MouseButtonRelease)
    4. {
    5. // extract the selected index of m_cameraListView and do something with it! (e.g. update m_parameterTreeWidget)
    6. // [...]
    7. return true;
    8. }
    9. return QWidget::eventFilter(obj,event);
    10. }
    To copy to clipboard, switch view to plain text mode 

    How would I do that? Thanks!
    Last edited by jbecker; 11th January 2011 at 16:03.

  2. #2
    Join Date
    Mar 2008
    Location
    Marslev, Denmark
    Posts
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Get selected index of a QListView by using installEventFilter its viewport

    event filters should only be used in emergencies, and this is not one of them. You need to connect to the signals of the views instead.
    Bo Thorsen, Viking Software
    Qt applications on Linux and Windows

Similar Threads

  1. Replies: 14
    Last Post: 2nd October 2012, 03:10
  2. Replies: 2
    Last Post: 4th December 2010, 08:09
  3. Can i get index list that showed in viewport?
    By jerry7 in forum Qt Programming
    Replies: 1
    Last Post: 26th November 2010, 20:58
  4. QListview set selected item
    By Freeman551 in forum Qt Programming
    Replies: 1
    Last Post: 25th December 2009, 00:17
  5. Connect button and pass QListView current index
    By been_1990 in forum Qt Programming
    Replies: 3
    Last Post: 30th November 2009, 16: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.