python qtdesigner 槽_python-使用QtDesigner的pyQt信号/插槽

本文介绍了如何使用Python的PyQt和QtDesigner来处理QGraphicsView的鼠标和键盘事件。作者遇到的问题是,尽管尝试通过信号槽连接来获取QGraphicsView内的鼠标位置,但结果却在窗口的任何地方都能获取到鼠标位置。问题可能出在信号连接上,作者寻求帮助以理解正确的信号槽机制。
摘要由CSDN通过智能技术生成

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

这是我为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的问题.但是每次我回过头来阅读有关信号和插槽的信息时,这都是有道理的,但我却听不懂.

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

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值