2016-11-29 79 views
-1

我正在使用PyQt4.QMainWindow作爲我的應用程序接口,並且我想要在QWidget中獲取鼠標的x和y座標,並將它們連續設置在2個textBrowsers中主窗口。在QWidget中使用PyQt4.QtGui.QMouseEvent

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_() 
+0

試試我的解決方案 – eyllanesc

+0

你似乎對Qt不太瞭解。 MouseMoveEvent是一個事件回調,只要鼠標在窗口小部件中移動,該回調就從框架調用。這可能是你想要的,但不是調用它(沒有意義),你可能想重寫它並在那裏實現你自己的操作。 – Trilarion

+0

[使用PyQt4,如何設置mouseMoveEvent只能在QMainWindow中的QWidget中工作,但不在MainWindow中](http://stackoverflow.com/questions/40878157/using-pyqt4-how-do -you-set-a-mousemoveevent-to-only-work-inside-of-a-qwidget-in) –

回答

1

當你申請setMouseTracking它僅適用於小部件,而不是你的孩子,所以你必須手動,在未來的解決方案:

def setMouseTracking(self, flag): 
    def recursive_set(parent): 
     for child in parent.findChildren(QtCore.QObject): 
      try: 
       child.setMouseTracking(flag) 
      except: 
       pass 
      recursive_set(child) 
    QtGui.QWidget.setMouseTracking(self, flag) 
    recursive_set(self) 

完整代碼:

from PyQt4 import QtCore 

from PyQt4 import QtGui 
from PyQt4.QtGui import QApplication, QMainWindow 
import sys 


class Ui_MainWindow(object): 
    def setupUi(self, MainWindow): 
     MainWindow.resize(800, 132) 
     self.centralwidget = QtGui.QWidget(MainWindow) 
     self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget) 
     self.textBrowser_1 = QtGui.QTextBrowser(self.centralwidget) 
     self.horizontalLayout.addWidget(self.textBrowser_1) 
     self.textBrowser_2 = QtGui.QTextBrowser(self.centralwidget) 
     self.horizontalLayout.addWidget(self.textBrowser_2) 
     MainWindow.setCentralWidget(self.centralwidget) 
     self.menubar = QtGui.QMenuBar(MainWindow) 
     self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22)) 
     MainWindow.setMenuBar(self.menubar) 
     self.statusbar = QtGui.QStatusBar(MainWindow) 
     MainWindow.setStatusBar(self.statusbar) 
     QtCore.QMetaObject.connectSlotsByName(MainWindow) 


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 
     self.ui.setupUi(self) 
     self.setMouseTracking(True) 
     self.ui.textBrowser_1.setMouseTracking(True) 
     self.ui.textBrowser_2.setMouseTracking(True) 
     self.ui.menubar.setMouseTracking(True) 
     self.ui.statusbar.setMouseTracking(True) 

    def setMouseTracking(self, flag): 
     def recursive_set(parent): 
      for child in parent.findChildren(QtCore.QObject): 
       try: 
        child.setMouseTracking(flag) 
       except: 
        pass 
       recursive_set(child) 
     QtGui.QWidget.setMouseTracking(self, flag) 
     recursive_set(self) 

    def mouseMoveEvent(self, event): 
     pos = event.pos() 
     self.ui.textBrowser_1.append(str(pos.x())) 
     self.ui.textBrowser_2.append(str(pos.y())) 
     QtGui.QMainWindow.mouseMoveEvent(self, event) 

if __name__ == "__main__": 
    app = QApplication(sys.argv) 
    mainscreen = MyMainScreen() 
    mainscreen.show() 
    app.exec_() 

這是我的輸出:

enter image description here

+0

註釋不適用於擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/129372/discussion-on-answer-by-eyllanesc-using-pyqt4-qtgui-qmouseevent-in-a-qwidget)。 –