开发者

QListWidget send doubleClicked signal with no items

开发者 https://www.devze.com 2023-02-20 14:46 出处:网络
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 QModelI

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消