QMainWindow MousedBubleclicekevent替代不起作用

发布于 2025-01-18 09:36:45 字数 1769 浏览 0 评论 0原文

我正在使用下面的代码来尝试捕获主窗口中的双击”

import sys
from PySide6 import QtCore
from PySide6.QtWidgets import (
    QApplication,
    QGridLayout,
    QMainWindow,
    QSplitter,
    QTreeView,
    QWidget,
)
from PySide6.QtWebEngineWidgets import QWebEngineView


class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()

        self.windowLayout = QGridLayout()
        self.splitter = QSplitter()

        self.webView = QWebEngineView()
        self.webView.setUrl(QtCore.QUrl("http://127.0.0.1:8080"))
        self.tree = QTreeView()
        self.splitter.addWidget(self.webView)
        self.splitter.addWidget(self.tree)
        self.windowLayout.addWidget(self.splitter)

        self.mainWidget = QWidget()
        self.mainWidget.setLayout(self.windowLayout)
        self.setCentralWidget(self.mainWidget)

    def mouseDoubleClickEvent(self, e):
        print('test')


if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = MainWindow()
    window.resize(1500, 1000)
    window.show()

    app.exec()

并且我得到以下控制台输出:

qt.pointer.dispatch: delivering touch release to same window QWindow(0x0) not QWidgetWindow(0x600003bb8e40, name="MainWindowClassWindow")
qt.pointer.dispatch: skipping QEventPoint(id=1 ts=0 pos=0,0 scn=749.346,451.159 gbl=749.346,451.159 Released ellipse=(1x1 ∡ 0) vel=0,0 press=-749.346,-451.159 last=-749.346,-451.159 Δ 749.346,451.159) : no target window

但是,当我注释掉 self.setCentralWidget(self.mainWidget) 时,我显然不知道没有看到任何内容,但双击会触发。

编辑:更新为最小重现,另请注意,单击 QWebEngineView 内以外的任何位置都会导致 mouseDoubleClickEvent 触发。我单击 QWebEngineView,但当我单击 QTreeView 内部时没有任何反应

I'm using the below code to try and capture double clicks within the main window"

import sys
from PySide6 import QtCore
from PySide6.QtWidgets import (
    QApplication,
    QGridLayout,
    QMainWindow,
    QSplitter,
    QTreeView,
    QWidget,
)
from PySide6.QtWebEngineWidgets import QWebEngineView


class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()

        self.windowLayout = QGridLayout()
        self.splitter = QSplitter()

        self.webView = QWebEngineView()
        self.webView.setUrl(QtCore.QUrl("http://127.0.0.1:8080"))
        self.tree = QTreeView()
        self.splitter.addWidget(self.webView)
        self.splitter.addWidget(self.tree)
        self.windowLayout.addWidget(self.splitter)

        self.mainWidget = QWidget()
        self.mainWidget.setLayout(self.windowLayout)
        self.setCentralWidget(self.mainWidget)

    def mouseDoubleClickEvent(self, e):
        print('test')


if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = MainWindow()
    window.resize(1500, 1000)
    window.show()

    app.exec()

And I get the following console output:

qt.pointer.dispatch: delivering touch release to same window QWindow(0x0) not QWidgetWindow(0x600003bb8e40, name="MainWindowClassWindow")
qt.pointer.dispatch: skipping QEventPoint(id=1 ts=0 pos=0,0 scn=749.346,451.159 gbl=749.346,451.159 Released ellipse=(1x1 ∡ 0) vel=0,0 press=-749.346,-451.159 last=-749.346,-451.159 Δ 749.346,451.159) : no target window

however, when I comment out self.setCentralWidget(self.mainWidget) I obviously don't see anything, but the double click triggers. Any suggestions?

EDIT: update to min repro, also note that clicking anywhere not inside the QWebEngineView correctly results in mouseDoubleClickEvent triggering. However, the above error results when I click in the QWebEngineView, and nothing happens when I click inside the QTreeView

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山色无中 2025-01-25 09:36:45

根据Musicamante的非常有用的评论(请参阅原始帖子),我最终使用的解决方案是

...
     self.tree.doubleClicked.connect(self.treeDoubleClicked)
...
def treeDoubleClicked(self, index):
     self.selectedFilepath = index.model().filePath(index)

As per musicamante's very useful comments (see original post), the solution I ended up using was

...
     self.tree.doubleClicked.connect(self.treeDoubleClicked)
...
def treeDoubleClicked(self, index):
     self.selectedFilepath = index.model().filePath(index)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文