[PyKDE] drawing on QCanvas not working

Peter Hinely phinely at hawaii.edu
Tue May 8 04:31:30 BST 2001


Hi,

Phil Thompson wrote:

> Remember that sometimes you need to keep explicit references to widgets
> to stop them from being garbage collected. (Hint: item => self.item,
> newCanvas => self.newCanvas.)

It works now.  Thank you very much.  That was my first python program.

But I'm quite confused by this behavior.  Is it some artifact brought on
by the interface between Python and C++ objects?  Surely pure Python would
not behave this way, right?

Calling
  setCentralWidget(self.canvasView)
should have prevented the QCanvasView from being destructed because the
main window maintains a reference to the canvasView.

  QCanvasView(self.newCanvas, self)
should have prevented the canvas object from being destructed because the
QCanvasView maintains a reference to the canvas.

and
  QCanvasEllipse(20, 20, self.newCanvas)
should have prevented the ellipse from being destructed.

It's not clear to me why I need to maintain an explicit reference to these
objects in my app.

regards,
peter


>
> Phil
>
> Peter Hinely wrote:
> >
> > Hello all,
> >
> > I'm trying to accomplish the simple task of drawing an elipse on a QCanvas
> > viewed through a QCanvasView.  I can draw the ellipse fine if I don't have
> > the QCanvasView inside a QMainWindow.  But when I embed it in a
> > QMainWindow, the ellipse does not appear.  Below is the code needed to
> > duplicated the problem.  Can anything tell me whether I'm doing something
> > wrong or whether I've encountered a bug?
> >
> > #!/usr/bin/env python
> >
> > import sys, string
> > from qt import *
> >
> > class TopologyCanvasView(QCanvasView):
> >     def __init__(self, canvas, parent):
> >         QCanvasView.__init__(self, canvas, parent)
> >
> > class ApplicationWindow(QMainWindow):
> >     def __init__(self):
> >         QMainWindow.__init__(self,None,'main window',Qt.WDestructiveClose)
> >
> >         self.file = QPopupMenu(self)
> >         self.menuBar().insertItem('&File',self.file)
> >
> > self.file.insertItem('E&xit',qApp,SLOT('closeAllWindows()'),Qt.CTRL +
> > Qt.Key_E)
> >
> >         newCanvas = QCanvas(200,200)
> >         canvasView = QCanvasView(newCanvas, self)
> >
> >         self.setCentralWidget(canvasView)
> >         item = QCanvasEllipse(20, 20, newCanvas)
> >         item.setX(50)
> >         item.setY(50)
> >         item.setPen(QPen(Qt.red))
> >         item.setBrush(QBrush(Qt.blue))
> >         item.show()
> >
> >         canvasView.show()
> >
> >         self.statusBar().message('Ready',2000)
> >         self.resize(350,400)
> >
> > a = QApplication(sys.argv)
> > mw = ApplicationWindow()
> > mw.show()
> > a.connect(a, SIGNAL('lastWindowClosed()'), a, SLOT('quit()'))
> > a.exec_loop()
> >
> > #===========================================================
> > # This code without the QMainWindow works fine
> > # The code was written by someone on this list I think
> > #===========================================================
> >
> > #!/usr/bin/env python
> >
> > import sys
> > from qt import *
> >
> > qapp=QApplication(sys.argv)
> >
> > canvas=QCanvas(200, 400)
> > canvasView=QCanvasView(canvas)
> >
> > item = QCanvasEllipse(20, 20, canvas)
> > item.setX(50)
> > item.setY(50)
> > item.setPen(QPen(Qt.red))
> > item.setBrush(QBrush(Qt.blue))
> > item.show()
> >
> > canvas.resize(200, 200)
> > canvasView.show()
> >
> > qapp.connect(qapp, SIGNAL('lastWindowClosed()'), qapp, SLOT('quit()'))
> > qapp.exec_loop()
> >
> > _______________________________________________
> > PyKDE mailing list    PyKDE at mats.gmd.de
> > http://mats.gmd.de/mailman/listinfo/pykde
>







More information about the PyQt mailing list