PDA

View Full Version : Get selected index of a QListView by using installEventFilter its viewport



jbecker
11th January 2011, 16:03
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:


CameraWidget::CameraWidget(QWidget *parent, Qt::WFlags flags)
{
m_model = new CameraTreeModel();

m_mainLayout = new QVBoxLayout();
this->setLayout(m_mainLayout);

m_grabButton = new QPushButton("Grab image");
m_mainLayout->addWidget(m_grabButton);

m_cameraListView = new QListView();
m_cameraListView->setModel(m_model);
m_mainLayout->addWidget(m_cameraListView);

m_parameterTreeView = new QTreeView();
// [...]
m_mainLayout->addWidget(m_parameterTreeView);

m_cameraListView->viewport()->installEventFilter(this);
}

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:


bool CameraWidget::eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::MouseButtonRelease)
{
// extract the selected index of m_cameraListView and do something with it! (e.g. update m_parameterTreeWidget)
// [...]
return true;
}
return QWidget::eventFilter(obj,event);
}

How would I do that? Thanks!

bothorsen
12th January 2011, 06:52
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.