[PyQt] creating a QgraphicsView in MainWindow

Phil Thompson phil at riverbankcomputing.com
Thu Feb 26 10:25:25 GMT 2009


On Thu, 26 Feb 2009 02:16:41 -0800 (PST), Mads Kofod Hansen
<kofoden at yahoo.com> wrote:
> Hi all
> 
> My question is if it is possible to create a QGraphicsView in a
MainWindow?
> 
> I have a QMainWindow with a menu bar and I want to create a QGraphicsView
> below the menu bar.
> 
> I have made a simple example:
> 
> import sys
> from PyQt4 import QtGui, QtCore
> 
> class MainWindow(QtGui.QMainWindow):
>     def __init__(self):
>         QtGui.QMainWindow.__init__(self)
> 
>         self.resize(250, 150)
>         self.setWindowTitle('menubar')
> 
>         exit = QtGui.QAction(QtGui.QIcon('icons/Exit.png'), 'Exit', self)
>         exit.setShortcut('Ctrl+Q')
>         exit.setStatusTip('Exit application')
>         self.connect(exit, QtCore.SIGNAL('triggered()'),
>         QtCore.SLOT('close()'))
> 
>         self.statusBar()
> 
>         menubar = self.menuBar()
>         file = menubar.addMenu('&File')
>         file.addAction(exit)
>         
>         grview = QtGui.QGraphicsView()
>         scene = QtGui.QGraphicsScene()
>         scene.addPixmap(QtGui.QPixmap('icons/Exit.png'))
>         grview.setScene(scene)
> 
> app = QtGui.QApplication(sys.argv)
> main = MainWindow()
> main.show()
> sys.exit(app.exec_())
> 
> But this only displays the MainWindow with a menubar. How do I create the
> GraphicsView in the MainWindow?
> 
> thanks for any help

You aren't keeping references to your view or scene so they are being
garbage collected.

Phil


More information about the PyQt mailing list