【发布时间】:2017-04-12 20:46:58
【问题描述】:
我使用 PyQt4.QMainWindow 作为我的应用程序界面,我想在 QWidget 中获取鼠标的 x 和 y 坐标,并在 MainWindow 的 2 个 textBrowsers 中连续设置它们。
QWidget 的文档是here。
QMouseEvent 的文档是here。
这里是代码
from PyQt4 import QtGui
from PyQt4.QtGui import QApplication
import sys
class Ui_MainWindow(object):
def setupUI(self, MainWindow):
self.textBrowser_1 = QtGui.QTextBrowser(self.tab)
self.textBrowser_2 = QtGui.QTextBrowser(self.tab)
self.widget_1 = QtGui.QWidget(self.tab)
self.widget_1.setMouseTracking(True)
class MyMainScreen(QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow() # This is from a python export from QtDesigner
# There is a QWidget inside that is self.ui.widget_1
# and 2 textBrowsers, textBrowser_1 and textBrowser_2
# I want to populate these 2 textBrowsers with the current x,y
# coordinates.
if __name__ == "__main__":
app = QApplication(sys.argv)
mainscreen = MyMainScreen()
mainscreen.show()
app.exec_()
【问题讨论】:
-
你似乎对Qt不太了解。 MouseMoveEvent 是一个事件回调,只要鼠标在小部件内移动,就会从框架调用。这可能是您想要的,但与其调用它(没有意义),您可能想要覆盖它并在那里实现您自己的操作。
标签: python pyqt pyqt4 qwidget qmouseevent