[PyQt] Newbe - QGraphicsScreen and QGraphicsView interaction
problem
stan
modifiedbessel at free.fr
Wed Dec 17 15:15:39 GMT 2008
Oh me, of course! Can't see the forest for the trees. Works like a
charm; thanks very much for that valuable insight and help.
On Wed, 2008-12-17 at 06:29 -0800, Brian Kelley wrote:
> You are creating a new text() object and not adding it to the
> scene so it is never drawn. You may want to create a single
> text item and pass it to the marker class as follows:
>
> 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, text):
> QGraphicsItem.__init__(self)
> self.setFlag(QGraphicsItem.ItemIsFocusable)
> self.setFlag(QGraphicsItem.ItemIsMovable)
> self.text = text
>
> 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 = self.text
> dbl.setVisible(True)
> dbl.setPlainText(str(mouseEvent1.pos().y()))
> dbl.setFlag(QGraphicsItem.ItemIsFocusable)
> dbl.update()
> self.scene().update()
>
> class MainScene(QGraphicsScene):
> def __init__(self):
> QGraphicsScene.__init__(self)
> self.setSceneRect(0.0,0.0,480,270)
> t = text()
> item = marker(t)
> self.addItem(t)
> self.addItem(item)
>
> 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_())
More information about the PyQt
mailing list