Search code examples
c++qtclickqt-creatorqlistwidget

QListWidget send doubleClicked signal with no items


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 & index) 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?


Solution

  • 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.