[PyQt] Newbe - QGraphicsScreen and QGraphicsView interaction problem

Brian Kelley kelley at eyesopen.com
Wed Dec 17 14:29:04 GMT 2008


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_())
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20081217/692254a8/attachment.html


More information about the PyQt mailing list