I have a QListWidget on a dialog that I want to do something (for example, open a QFileDialog window) when a user double-clicks on the QListWidget. Unfortunately, the void doubleClicked (const QModelIndex & in开发者_运维技巧dex)
only fires when there are items in the list.
Is it possible to get the widget to fire the signal whenever a double-click event is received, anywhere within the widget? Or is a different approach required?
You can install an event filter to the listwidget's viewport widget, something like this:
listWidget->viewport()->installEventFilter(this); // "this" could be your window object.
In the eventFilter method check for the QEvent::MouseButtonDblClick
event:
bool YourWindowClass::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseButtonDblClick)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
qDebug("Mouse double click %d %d", mouseEvent->x(), mouseEvent->y());
return true;
}
else
{
return QMainWindow::eventFilter(obj, event);
}
}
I hope this helps.
精彩评论