--------- Original Besked -------- Fra: Markus Feldmann <feldmann_mar...@gmx.de> Til: pyqt@riverbankcomputing.com <pyqt@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("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() > > _______________________________________________ > PyQt mailing list PyQt@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_()) _______________________________________________ PyQt mailing list PyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt