【发布时间】:2018-04-15 05:20:59
【问题描述】:
我想在双击QGraphicsItem 时发出一个信号,以便更改主窗口中的小部件。 graphics-scene/-item 不提供emit() 方法,但我只是想知道是否有另一种方法可以做到这一点。下面的代码在QGraphicsView 类中有一个函数,当双击一个项目时,它将打印到终端。我怎样才能把它变成一个槽/信号(如果QGraphicsItem 不支持信号/槽)?
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class MyFrame(QGraphicsView):
def __init__( self, parent = None ):
super(MyFrame, self).__init__(parent)
scene = QGraphicsScene()
self.setScene(scene)
self.setFixedSize(500, 500)
pen = QPen(QColor(Qt.green))
brush = QBrush(pen.color().darker(150))
item = scene.addEllipse(0, 0, 45, 45, pen, brush)
item.setPos(0,0)
def mouseDoubleClickEvent(self, event):
print("Circle Clicked!")
# this double click event prints to terminal but how to setup
# signal/slot to update the QWidget QLabel text instead?
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = QHBoxLayout(self)
top = QLabel("Double Click Green Circle (Howto change this QWidget Label with signals?)")
bottom = MyFrame()
splitter = QSplitter(Qt.Vertical)
splitter.addWidget(top)
splitter.addWidget(bottom)
hbox.addWidget(splitter)
self.setLayout(hbox)
self.setGeometry(0, 0, 500, 600)
self.show()
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
【问题讨论】:
-
在
QGraphicsScene的子类上定义一个自定义的itemDoubleClicked信号,然后从项目中执行self.scene().itemDoubleClicked.emit()。 -
你有例子吗?
-
class GS(QGraphicsScene): itemDoubleClicked = QtCore.pyqtSignal(). -
嗨,是的,不起作用。 “没有 emit() 属性”的错误。制作一个单独的 QGraphicsObject 子类并在那里调用 func 不会出错,但也不会发出。我已经看到这个关于 QGraphicsItem 的信号/插槽问题在搜索中被多次提出,但没有人能提供一个例子或证据,这实际上可以在不完全重写 QGraphicsScene 的情况下完成。这就是大多数人所做的,所以猜测它不可能从 QGraphis 向 QWidget 发出信号。耻辱,使 QGraphics 类在实际应用程序中毫无用处。我想是时候开发我自己的 QGraphics 了。
-
我发布的代码非常适合我。不知道你做错了什么。我猜你没有创建
QGraphicsScene子类的实例并将其设置在视图上。
标签: python pyqt pyside signals-slots qgraphicsitem