[PyQt] how to show interactive Picture

Mads Ipsen mpi at comxnet.dk
Mon Feb 9 22:37:55 GMT 2009


--------- Original Besked --------
Fra: Markus Feldmann <feldmann_markus at gmx.de>
Til: pyqt at riverbankcomputing.com <pyqt at riverbankcomputing.com>
Emne: [PyQt] how to show interactive Picture
Dato: 09/02/09 23:18

> 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(&quot;Reisehelfer&quot;))
>          self.scrollarea = QScrollArea()
>          self.setCentralWidget(self.scrollarea)
> 
>          self.pen = QPen()
>          self.brush = QBrush()
>          self.pixmap = QPixmap()
>          self.pixmap.load(&quot;:/media/images/aventurien.jpg&quot;)
> 
>          self.readSettings()
> 
>          self.update()
> 
> 
>      def createActions(self):
>          self.openMapAct = QAction(self.tr(&quot;&amp;Open Map&quot;),
self)
>          self.openMapAct.setShortcut(self.tr(&quot;Ctrl+O&quot;))
>          self.connect(self.openMapAct,
QtCore.SIGNAL(&quot;triggered()&quot;), 
> self.loadMap)
> 
>          self.exitAct = QAction(self.tr(&quot;E&amp;xit&quot;), self)
>          self.exitAct.setShortcut(self.tr(&quot;Ctrl+Q&quot;))
>          self.connect(self.exitAct,
QtCore.SIGNAL(&quot;triggered()&quot;), self, 
> QtCore.SLOT(&quot;close()&quot;))
> 
> 
>      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()
> 
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
> 

Could is be that you try to paint before the window is shown? You need to
call show() before you can start painting. The following snippet generates
an error similar to yours:

import sys

from PyQt4 import QtCore, QtGui

class MyWidget(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)

    def paintEvent(self, event):
        painter = QtGui.QPainter()
        painter.begin(self)
        painter.end()
        
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    widget = MyWidget()

    # Try to call an immature paintEvent
    region = QtGui.QRegion(0,0,64,64)
    event = QtGui.QPaintEvent(region)
    widget.paintEvent(event)

    widget.show()
    sys.exit(app.exec_())


More information about the PyQt mailing list