[PyQt] how to show interactive Picture

Markus Feldmann feldmann_markus at gmx.de
Mon Feb 9 22:08:56 GMT 2009


Hi All,

i am programming a MDI Application where a Window in the
workspace shall work as a Map Program. Therefore i want
to show a Picture which is parted in many small hexagons.
This hexagons can be selected with the mouse.

Ok, i started first to set up the picture, but got
trouble. Here are some Messages:
StdErr: QPainter::begin: Widget painting can only begin as a result of a 
paintEvent
QPainter::begin: Painter already active
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::save: Painter not active
QPainter::restore: Unbalanced save/restore
QPainter::end: Painter not active, aborted


So i think i do something wrong to show my picture.
How can i show the picture correctly ?

Regards Markus

Here it comes a part of my Programm:
#!/usr/bin/env python
#
# reisehelfer.py
#

import sys
from PyQt4.QtGui import QMainWindow,  QDockWidget,  QPainter,  QAction, 
  QScrollArea
from PyQt4.QtGui import QPaintEvent,  QPixmap,  QPen,  QBrush
from PyQt4 import QtCore


class ReiseHelfer(QMainWindow):
     def __init__(self, parent=None):
         QMainWindow.__init__(self, parent)

         self.setAttribute(QtCore.Qt.WA_DeleteOnClose)

         self.createActions()
         self.createMenus()
         self.createStatusBar()

         self.setWindowTitle(self.tr("Reisehelfer"))
         self.scrollarea = QScrollArea()
         self.setCentralWidget(self.scrollarea)

         self.pen = QPen()
         self.brush = QBrush()
         self.pixmap = QPixmap()
         self.pixmap.load(":/media/images/aventurien.jpg")

         self.readSettings()

         self.update()


     def createActions(self):
         self.openMapAct = QAction(self.tr("&Open Map"), self)
         self.openMapAct.setShortcut(self.tr("Ctrl+O"))
         self.connect(self.openMapAct, QtCore.SIGNAL("triggered()"), 
self.loadMap)

         self.exitAct = QAction(self.tr("E&xit"), self)
         self.exitAct.setShortcut(self.tr("Ctrl+Q"))
         self.connect(self.exitAct, QtCore.SIGNAL("triggered()"), self, 
QtCore.SLOT("close()"))


     def createMenus(self):
...


     def createStatusBar(self):
...


     def loadMap(self):
         pass

     def paintEvent(self,  event):
         painter =  QPainter(self.scrollarea)
         painter.begin(self)
         painter.setPen(self.pen)
         painter.setBrush(self.brush)
         painter.setRenderHint(QPainter.Antialiasing,  True)

         painter.save()
         painter.drawPixmap(10,  10,  self.pixmap)
         painter.restore()
         painter.end()



     def readSettings(self):
...

     def writeSettings(self):
...


     def closeEvent(self, event):
         self.writeSettings()
         event.accept()



More information about the PyQt mailing list