- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以,我试图每次左键单击时打印鼠标位置,但它只能在布局之外工作,我不知道如何修复它。我的 GUI 看起来像这样:
import sys
from PyQt5 import QtGui, QtWidgets, QtCore
from untitled import Ui_Dialog
class AppWindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.show()
def mousePressEvent(self, a0: QtGui.QMouseEvent):
print(a0.pos())
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = AppWindow()
w.show()
sys.exit(app.exec_())
这是我的 untitled.py 文件
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.horizontalLayout = QtWidgets.QHBoxLayout(Dialog)
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.graphicsView = QtWidgets.QGraphicsView(Dialog)
self.graphicsView.setObjectName("graphicsView")
self.verticalLayout.addWidget(self.graphicsView)
self.horizontalLayout.addLayout(self.verticalLayout)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton.setText(_translate("Dialog", "PushButton"))
最佳答案
只有当小部件要处理该信息时,才会调用 mousePressEvent 事件,因此,如果您单击的区域中有其他子小部件,则不会收到该事件,因此您只能得到顶部没有其他小部件的点击,因此这些情况的解决方案是通过 eventFilter
监听事件:
import sys
from PyQt5 import QtGui, QtWidgets, QtCore
from untitled import Ui_Dialog
class AppWindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.show()
for w in self.findChildren(QtWidgets.QWidget)+[self]:
w.installEventFilter(self)
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.MouseButtonPress:
if event.buttons() & QtCore.Qt.LeftButton:
print(obj, "global pos:", event.globalPos(),
"local pos", event.pos(),
"position with respect to self",
self.mapFromGlobal(obj.mapToGlobal(event.pos())))
return super(AppWindow, self).eventFilter(obj, event)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = AppWindow()
w.show()
sys.exit(app.exec_())
关于python - mousePressEvent 不适用于子部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53081391/
所以,我试图每次左键单击时打印鼠标位置,但它只能在布局之外工作,我不知道如何修复它。我的 GUI 看起来像这样: import sys from PyQt5 import QtGui, QtWidge
我有两个不同的类继承QGraphicsItem。它们都有各自特定的 mousePressEvent 函数。当这些项目在场景中重叠时,我只想执行其中一个项目的 mousePressEvent。我怎样才能
我正在尝试检测具有多个小部件的区域内任意位置的鼠标点击。为此,我使用以下代码: custom_widget = CustomWidget() custom_widget.mouse_pressed_s
我在使用 QGraphicsView 和 QGraphicsScene 时遇到了一些困难。当我在场景中缩放/取消缩放并使用 mousePressEvent 创建项目时,我在该位置有一个偏移量。如何避免
我想在 dicom 系列(使用 qt 和 vtk)上实现一些功能,并且我想在 qt 窗口和鼠标之间建立一些连接。 这是我的主要设计: 例如,如果我点击 zoombutton,然后我用鼠标左键点击我的图
如何进入方法 on_product_clicked,其中 QLabel 是一个 mousePressEvent? 在当前示例中,我在表单中看到了三个图像。当我点击第二张图片时,我需要在 on_prod
我正在尝试同时运行两个 QWidget::mousePressEvent() 处理程序。我有两个小部件,A 和 B。B继承自QLabel,A继承自QWidget。我已经在它们两个上重载了 QWidge
所以我有一个带有 QGraphicsPolygonItem 的 QraphicsScene ,我将其标记为可移动。我还重写了 MousePressEvent。我的代码片段现在已经出来了。 def mo
我有一个小部件,我想在用户按下鼠标时运行一些功能。但我只想在鼠标按下导致焦点改变时运行它。我尝试了以下方法: void MyWidget::mousePressEvent(QMouseEvent *e
我在使用 mousePressEvent(QGraphicsSceneMouseEvent *event) 时遇到问题,实际上可点击区域看起来很小并且偏离了它链接到的 QGraphicsPixmapI
在 Class Buttons 中,我有一个 btnRightClicked 信号和一个 mousePressEvent 插槽: void Buttons::mousePressEvent(QMous
请考虑以下代码: #include #include #include class Widget : public QWidget { public: void mousePressEv
我有一个包含多个子对象的小部件,用户可以通过单击它们来选择它们。 我想在用户点击小部件外部时清除当前选择,我想知道如何最好地检测这些点击。 一些约束: 通过eventFilter吃掉周围对象的mous
我正在尝试在 pyqt4 中调整照片查看器(我从本网站的另一个问题中获得了代码)。默认情况下,鼠标滚轮缩放,鼠标左键用于平移/拖动(不知道到底该怎么称呼)图像。我想添加左键单击图像时获取像素信息的功能
在最近的项目中,我尝试在 GraphicsView 中单击相应位置后在场景上绘制点,但没有任何反应。我将错误追踪到缺少对 ClickableMap::mousePressEvent(const QMo
我有一个继承自 qquickwindow 的对象,该对象具有重写的 mousePressEvent 方法。 .h class FWindow : public QQuickWindow { Q
我正在尝试子类化 QCheckBox 以使其在禁用时通过鼠标点击。当我的复选框未被禁用时(isEnabled() 为 true),Qt 按预期调用其 mousePressEvent()。当它被禁用时,
我有一个类,它是 QDialog 的子类,没有覆盖 exec()、accept() 或 reject() 和另一个调用 mousePaintEvent 中的 Dialog 类: void Canvas
为了熟悉 Qt 的图形 View ,我在 Qt 中实现了一个简单的棋盘。目前没有棋子。 我使用矩形来表示每个图 block ,所以我创建了 BoardTile 类(继承自 QGraphicsRectI
我在使用 installEventFilter 过滤 "mousePressEvent" 时遇到问题 MyTestxEdit 是一个包含 QTextEdit 的小部件我希望 QTextEdit 的所有
我是一名优秀的程序员,十分优秀!