2011-09-23 99 views
3

我想写一个程序,将与QGraphicsView交互。我想在QGraphicsView中收集鼠标和键盘事件。例如,如果用户点击QGraphicsView小部件,我将得到鼠标位置,类似的东西。我可以很容易地对它进行硬编码,但我想使用QtDesigner,因为UI将会频繁更改。pyQt信号/插槽与QtDesigner

这是我对gui.py的代码。一个简单的小部件,里面有一个QGraphicsView。

from PyQt4 import QtCore, QtGui 

try: 
    _fromUtf8 = QtCore.QString.fromUtf8 
except AttributeError: 
    _fromUtf8 = lambda s: s 

class Ui_graphicsViewWidget(object): 
    def setupUi(self, graphicsViewWidget): 
     graphicsViewWidget.setObjectName(_fromUtf8("graphicsViewWidget")) 
     graphicsViewWidget.resize(400, 300) 
     graphicsViewWidget.setMouseTracking(True) 
     self.graphicsView = QtGui.QGraphicsView(graphicsViewWidget) 
     self.graphicsView.setGeometry(QtCore.QRect(70, 40, 256, 192)) 
     self.graphicsView.setObjectName(_fromUtf8("graphicsView")) 

     self.retranslateUi(graphicsViewWidget) 
     QtCore.QMetaObject.connectSlotsByName(graphicsViewWidget) 

    def retranslateUi(self, graphicsViewWidget): 
     graphicsViewWidget.setWindowTitle(QtGui.QApplication.translate("graphicsViewWidget", "Form", None, QtGui.QApplication.UnicodeUTF8)) 

为程序代码:

#!/usr/bin/python -d 

import sys 
from PyQt4 import QtCore, QtGui 
from gui import Ui_graphicsViewWidget 

class MyForm(QtGui.QMainWindow): 

    def __init__(self, parent=None): 
     QtGui.QWidget.__init__(self, parent) 
     self.ui = Ui_graphicsViewWidget() 
     self.ui.setupUi(self) 
     QtCore.QObject.connect(self.ui.graphicsView, QtCore.SIGNAL("moved"), self.test) 

    def mouseMoveEvent(self, event): 
     print "Mouse Pointer is currently hovering at: ", event.pos() 
     self.emit(QtCore.SIGNAL("moved"), event) 

    def test(self, event): 
     print('in test') 

if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv) 
    myapp = MyForm() 
    myapp.show() 
    sys.exit(app.exec_()) 

当我运行这段代码,它给我的,我想的正好相反。除了QGraphicsView以外,我在任何地方都能看到鼠标位置。

我确定这是我的QObject.connect问题。但每次我回头阅读有关信号和插槽的信息时,它都是有道理的,但我无法得到它。

请大家帮忙,我过去几天一直在b my我的脑袋。我很抱歉,如果这是以前问过,但我已经通过了这个话题的所有主题,我不能得到任何地方。

由于

+0

在你的gui.py文件中,'self.graphicsView = MyForm(graphicsViewWidget)'这一行会抛出一个异常,因为'MyForm'没有被定义。我不确定你在那里的意思。你能修好那条线吗? –

+0

对不起,MyForm应该是QtGui.QGraphicsView。我将它固定在线程中,然后在我的计算机上运行,​​现在应该可以正常工作。 – Jeff

+0

正如一个建议,如果你使用'从PyQt4.QtGui import *'和'从PyQt4.QtCore import *'导入,你可以删除'QtCore.'和'QtGui.'的所有实例,使代码更具可读性。 – Blender

回答

3

信号必须来自在UI中定义的QGraphicsView对象。

您可以从QGraphicsView得出这样

from PyQt4.QtCore import * 
from PyQt4.QtGui import * 

class MyView(QGraphicsView): 
    moved = pyqtSignal(QMouseEvent) 

    def __init__(self, parent = None): 
     super(MyView, self).__init__(parent) 

    def mouseMoveEvent(self, event): 
     # call the base method to be sure the events are forwarded to the scene 
     super(MyView, self).mouseMoveEvent(event) 

     print "Mouse Pointer is currently hovering at: ", event.pos() 
     self.moved.emit(event) 

然后一类,在设计师:

  • 右键单击QGraphicsView然后提升到
  • 写的类名称推荐类名称字段(例如“MyView”),
  • 写的文件名如该班是在头文件场,但没有的.py扩展,
  • 点击添加按钮,然后在促进按钮。

而且你可以用pyuic4重新生成你的文件gui.py

+0

正是我所期待的,谢谢你的一步一步。 – Jeff

+0

非常好的解释,谢谢:) 我只是有一个小问题,是否可以让QtDesigner知道您在文件中创建了哪些插槽/信号(或其他方式)。要定义它们两次似乎没有意义。 – Wolph

+0

@WoLpH我想你可以创建一个插件([像那样](http://diotavelli.net/PyQtWiki/Using_Python_Custom_Widgets_in_Qt_Designer),但是如果你只在一个项目中使用这个类,看起来很复杂 – alexisdm