![](/img/trans.png)
[英]How to restrain mouse cursor from leaving a QWidget area in PySide2
[英]Pyside2 how to get mouse position?
我想在我的 pyside2 應用程序中獲取鼠標位置。(不是 QCursor 提供的桌面鼠標位置),我嘗試了兩種方法。 波紋管是我的代碼。
import sys
from PySide2 import QtGui, QtWidgets, QtCore
class Palette(QtWidgets.QGraphicsScene):
def __init__(self, parent=None):
super().__init__(parent)
def mousePressEvent(self, event):
print(event.pos()) # always return (0,0)
print(QtWidgets.QWidget.mapToGlobal(QtCore.QPoint(0, 0))) #makes parameter type error
print(QtWidgets.QWidget.mapToGlobal(QtWidgets.QWidget)) # makes parameter type error
print(QtWidgets.QWidget.mapToGlobal(QtWidgets.QWidget.pos())) # makes parameter type error
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
palette = Palette(self)
view = QtWidgets.QGraphicsView(palette, self)
view.resize(500, 500)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_window = MainWindow()
main_window.resize(500, 500)
main_window.show()
app.exec_()
我很想知道我怎樣才能讓我的鼠標位置...
據我了解,如果您單擊小部件中的任意位置,您希望獲得窗口上的位置。
為了解決這個問題,邏輯如下:
對於第一步,如果使用mousePressEvent()
,則event.pos()
返回相對於小部件的位置。
對於第二步,您必須使用mapToGlobal()
將相對於小部件的位置轉換為全局位置。
第三步使用 window 的mapFromGlobal()
。
def mousePressEvent(self, event):
p = event.pos() # relative to widget
gp = self.mapToGlobal(p) # relative to screen
rw = self.window().mapFromGlobal(gp) # relative to window
print("position relative to window: ", rw)
super(Widget, self).mousePressEvent(event)
更新:
QGraphicsScene
不是小部件,也不是可視元素,盡管它是可視元素表示的一部分: QGraphicsView
。 為了你理解我打個比方,假設有一個攝影師在錄制一個場景,在那個例子中QGraphicsScene
是場景, QGraphicsView
是相機記錄的,也就是說,它顯示了QGraphicsScene
,所以可能會有另一個攝影師從另一個點記錄場景,所以它會從另一個角度顯示相同的場景,所以場景的位置取決於相機,所以如果你當前的問題相當於說哪個是位置點 P 相對於第 i 個相機,並且從場景中是不可能的,您應該從相機中獲取它。
因此,總而言之,您不應使用 QGraphicsScene 而是使用 QGraphicsView,以下解決方案使用 2 種不同的方法實現相同的邏輯:
1.創建自定義的QGraphicsView類:
import sys
from PySide2 import QtGui, QtWidgets, QtCore
class GraphicsView(QtWidgets.QGraphicsView):
def mousePressEvent(self, event):
p = event.pos() # relative to widget
gp = self.mapToGlobal(p) # relative to screen
rw = self.window().mapFromGlobal(gp) # relative to window
print("position relative to window: ", rw)
super(GraphicsView, self).mousePressEvent(event)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
scene = QtWidgets.QGraphicsScene(self)
view = GraphicsView(scene, self)
self.setCentralWidget(view)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_window = MainWindow()
main_window.resize(500, 500)
main_window.show()
sys.exit(app.exec_())
2. 使用事件過濾器:
import sys
from PySide2 import QtGui, QtWidgets, QtCore
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
scene = QtWidgets.QGraphicsScene(self)
self._view = QtWidgets.QGraphicsView(scene, self)
self.setCentralWidget(self._view)
self._view.installEventFilter(self)
def eventFilter(self, obj, event):
if obj is self._view and event.type() == QtCore.QEvent.MouseButtonPress:
p = event.pos() # relative to widget
gp = self.mapToGlobal(p) # relative to screen
rw = self.window().mapFromGlobal(gp) # relative to window
print("position relative to window: ", rw)
return super(MainWindow, self).eventFilter(obj, event)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_window = MainWindow()
main_window.resize(500, 500)
main_window.show()
sys.exit(app.exec_())
另一方面mapToGlobal()
是一個必須由實例調用的方法,當你使用QtWidgets.QWidget.mapToGlobal()
時沒有實例,我的問題是,你有什么小部件? 您相對於 self 的位置,因此您必須使用self.mapToGlobal()
,它適用於屬於從QWidget
作為QGraphicsView
繼承的類的對象,但不適用於QGraphicsScene
因為它不是從QWidget
繼承的,它不是上面幾行中指示的小部件。
如果您不想通過子類和事件,我最近發現了一種更通用的獲取光標位置的方法。
# get cursor position
cursor_position = QtGui.QCursor.pos()
print cursor_position
### Returns PySide2.QtCore.QPoint(3289, 296)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.