简体   繁体   English

QListWidget发送没有项目的doubleClicked信号

[英]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. 我在对话框上有一个QListWidget,当用户双击QListWidget时,我想执行某项操作(例如,打开QFileDialog窗口)。 Unfortunately, the void doubleClicked (const QModelIndex & index) only fires when there are items in the list. 不幸的是,仅当列表中有项目时, void doubleClicked (const QModelIndex & index)才会触发。

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的视口小部件中,如下所示:

listWidget->viewport()->installEventFilter(this); // "this" could be your window object.

In the eventFilter method check for the QEvent::MouseButtonDblClick event: 在eventFilter方法中,检查QEvent::MouseButtonDblClick事件:

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. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM