带有QtDesigner的pyQt信号/插槽 [英] pyQt signals/slots with QtDesigner

查看:122
本文介绍了带有QtDesigner的pyQt信号/插槽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个将与QGraphicsView交互的程序.我想收集QGraphicsView中发生的鼠标和键盘事件.例如,如果用户单击QGraphicsView小部件,我将获得鼠标位置,类似这样.我可以很容易地对其进行硬编码,但是我想使用QtDesigner,因为UI会经常更改.

I'm trying to write a program that will interact with QGraphicsView. I want to gather mouse and keyboard events when the happen in the QGraphicsView. For example, if the user clicks on the QGraphicsView widget I will get the mouse position, something like that. I can hard code it rather easily, but I want to use QtDesigner because the UI will be changing frequently.

这是我为gui.py提供的代码.一个带有QGraphicsView的简单小部件.

This is the code that I have for the gui.py. A simple widget with a QGraphicsView in it.

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内部,我到处都可以看到鼠标的位置.

When I run this code, it gives me the opposite of what I want. I get the mouse position everywhere except for inside the QGraphicsView.

我确定我的QObject.connect存在问题.但是每次我回去阅读有关信号和插槽的信息时,这都是有道理的,但我却听不懂.

I'm sure it's a problem with my QObject.connect. But every time I go back and read about signals and slots it makes sense but I can't get it.

请帮助,过去几天来我一直在head头.很抱歉,是否如以前所要求的那样,但是我已经遍历了有关此主题的所有主题,因此无处可寻.

Please help, I've been banging my head for the past few days now. I'm sorry if this as been asked before but I've been through all the threads on this topic and I can't get anywhere.

谢谢

推荐答案

信号必须来自ui中定义的QGraphicsView对象.

The signal must come from the QGraphicsView object that was defined in the ui.

您可以像这样从QGraphicsView派生一个类

You can create a class derived from QGraphicsView like this

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"),
  • Header file 字段中输入该类的文件名,但不要使用 .py 扩展名
  • 点击添加按钮,然后点击升级按钮.
  • right-click on the QGraphicsView then Promote to
  • write the class name in the Promoted Class Name field (e.g. "MyView"),
  • write the file name where that class is in the Header file field but without the .py extension,
  • click on the Add button and then on the Promote button.

您可以使用pyuic4重新生成文件 gui.py .

And you can regenerate your file gui.py with pyuic4.

这篇关于带有QtDesigner的pyQt信号/插槽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆