我想在
QGraphicsItem
被双击时发射一个信号,以便更改主窗口中的小部件。图形场景/-项不提供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()
。 - ekhumoroQGraphicsScene
子类的实例并将其设置在视图上。 - ekhumoro