[PyQt] Newbe - QGraphicsScreen and QGraphicsView interaction problem
stan
modifiedbessel at free.fr
Wed Dec 17 12:45:07 GMT 2008
I am creating an embedded application, and the versions I have to use
presently for Qt and PyQt do not support QGraphicsScene.addWidget(). I
want various scene/view items to be able to interact with others
scene/view items. I have tried emitting signals from one item to
another, but have not had any success. After having searched the
literature, posts, and google, I appreciate that this MIGHT not be
practicabl, so I would like to confirm this! Can you advise me if there
is anyway to get the " intention" of the following code to work? I
would like, for example, to effect an instance of a QGraphicstextItem
with its setPlainText() method from another GraphicsItem event such as
from a reimplementation of its .mouseReleaseEvent method:
from PyQt4 import *
from PyQt4.QtGui import*
from PyQt4.QtCore import *
import sys
class text(QGraphicsTextItem):
def __init__(self):
QGraphicsTextItem.__init__(self)
self.setFlag(QGraphicsItem.ItemIsFocusable)
self.setFlag(QGraphicsItem.ItemIsSelectable)
self.setTextInteractionFlags(Qt.TextEditable)
self.setPlainText("0000")
self.setZValue(3.0)
class marker(QGraphicsItem):
def __init__(self):
QGraphicsItem.__init__(self)
self.setFlag(QGraphicsItem.ItemIsFocusable)
self.setFlag(QGraphicsItem.ItemIsMovable)
def boundingRect(self):
return QRectF(25.0,0.0,25.0,270.0)
def paint(self, painter, option, widget):
ss = painter.drawRect(25.0,230.0,25.0,25.0)
tt = painter.drawLine(37.5,0.0,37.5,270.0)
def mouseMoveEvent(self, mouseEvent):
pt = self.mapToScene(mouseEvent.pos()).x()
self.setPos(pt-37.5,0)
self.update()
def mouseReleaseEvent(self,mouseEvent1):
dbl = text()
dbl.setVisible(True)
dbl.setPlainText("3.333")
dbl.setFlag(QGraphicsItem.ItemIsFocusable)
dbl.moveBy(50.0,50.0)
dbl.update()
class MainScene(QGraphicsScene):
def __init__(self):
QGraphicsScene.__init__(self)
self.setSceneRect(0.0,0.0,480,270)
item,item1 = text(),marker()
self.addItem(item), self.addItem(item1)
class AllItemsView(QGraphicsView):
def __init__(self):
QGraphicsView.__init__(self)
self.setWindowTitle("Text")
self.setInteractive(True)
self.setEnabled(True)
self.scene = MainScene()
self.mainScene = MainScene()
self.setScene(self.mainScene)
if __name__=="__main__":
app = QApplication(sys.argv)
view = AllItemsView()
view.show()
sys.exit(app.exec_())
The two items appear and work fine, but it is impossible to change the
contents of the text instance from the marker item. Thanks for any
comments on this problem!
More information about the PyQt
mailing list