Re: [PyQt] Re: how to show interactive Picture
- Original Besked Fra: Markus Feldmann feldmann_mar...@gmx.de Til: pyqt@riverbankcomputing.com pyqt@riverbankcomputing.com Emne: [PyQt] Re: how to show interactive Picture Dato: 09/02/09 23:58 Mads Ipsen schrieb: gt; Could is be that you try to paint before the window is shown? You need to gt; call show() before you can start painting. The following snippet generates gt; an error similar to yours: Hi, This is not all of my Code. So there is a lt;show()gt; in my main source code. This Source Code only shows a sub-widget of a MDI Application. However this window is shown, but without the picture. How do the program know in which widget shall the Pixmap be shown ? ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt Thats something you tell the program. You reimplement the method 'paintEvent' for a widget class like in the example I posted. Then for a particular instance of the widget, the window is painted everytime a paintEvent occurs and the widget get painted. A paintEvent event occurs eg. when a window is hidden underneath another window and then shown. To a draw a pixmap onto a widget do something like this in the paintEvent method: def paintEvent(self, event): painter = QtGui.QPainter(64,64) pixmap = QtGui.QPixmap() painter.begin(self) painter.drawPixmap(0,0,pixmap) painter.end() Mads ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
[PyQt] Re: how to show interactive Picture
Mads Ipsen schrieb: - Original Besked Fra: Markus Feldmann feldmann_mar...@gmx.de Til: pyqt@riverbankcomputing.com pyqt@riverbankcomputing.com Emne: [PyQt] Re: how to show interactive Picture Dato: 09/02/09 23:58 Mads Ipsen schrieb: gt; Could is be that you try to paint before the window is shown? You need to gt; call show() before you can start painting. The following snippet generates gt; an error similar to yours: Hi, This is not all of my Code. So there is a lt;show()gt; in my main source code. This Source Code only shows a sub-widget of a MDI Application. However this window is shown, but without the picture. How do the program know in which widget shall the Pixmap be shown ? ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt Thats something you tell the program. You reimplement the method 'paintEvent' for a widget class like in the example I posted. Then for a particular instance of the widget, the window is painted everytime a paintEvent occurs and the widget get painted. A paintEvent event occurs eg. when a window is hidden underneath another window and then shown. To a draw a pixmap onto a widget do something like this in the paintEvent method: def paintEvent(self, event): painter = QtGui.QPainter(64,64) pixmap = QtGui.QPixmap() painter.begin(self) painter.drawPixmap(0,0,pixmap) painter.end() Mads I think my Problem is that i have to send an envent? As you wrote i changed some of my code. I parted my scrollarea, where i want to paint my picture, and my mainwindow, which is a MDI Sub-Widget. The scrollarea will be centered into the MDI Sub-Widget. class ScrollArea(QScrollArea): def __init__(self, parent=None): QScrollArea.__init__(self, parent) self.pen = QPen() self.brush = QBrush() self.pixmap = QPixmap() self.pixmap.load(:/../media/images/aventurien.jpg) def paintEvent(self, event): painter = QPainter() painter.begin(self) painter.setPen(self.pen) painter.setBrush(self.brush) painter.save() painter.drawPixmap(0, 0, self.pixmap) painter.restore() painter.end() 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 = ScrollArea() self.setCentralWidget(self.scrollArea) self.readSettings() ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
[PyQt] Re: how to show interactive Picture
I got the follwing Messages: QPainter::begin: Widget painting can only begin as a result of a paintEvent QPainter::save: Painter not active QPainter::restore: Unbalanced save/restore QPainter::end: Painter not active, aborted I can't find the soultion. Please help. ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
[PyQt] Re: how to show interactive Picture
David Boddie schrieb: On Tue Feb 10 15:02:13 GMT 2009, Markus Feldmann wrote: I think my Problem is that i have to send an envent? Yes, you need to make a paint event happen. You can do this by calling the update() method on the widget you want to be updated. As you wrote i changed some of my code. I parted my scrollarea, where i want to paint my picture, and my mainwindow, which is a MDI Sub-Widget. The scrollarea will be centered into the MDI Sub-Widget. class ScrollArea(QScrollArea): def __init__(self, parent=None): QScrollArea.__init__(self, parent) self.pen = QPen() self.brush = QBrush() self.pixmap = QPixmap() self.pixmap.load(:/../media/images/aventurien.jpg) def paintEvent(self, event): painter = QPainter() painter.begin(self) painter.setPen(self.pen) painter.setBrush(self.brush) painter.save() painter.drawPixmap(0, 0, self.pixmap) painter.restore() painter.end() Looks OK to me. 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 = ScrollArea() self.setCentralWidget(self.scrollArea) self.readSettings() Does this work? Does it cause the errors you reported in your other message? David Yes it works, but with no Picture. I am reading an example in /usr/share/doc/python-qt4-doc/examples/painting/basicdrawing And the example is a little bit different. It is doing something with the paintevent i think. It is useful to post my whole Code? Regards Markus ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
[PyQt] Re: how to show interactive Picture
I changed the class Qwidget -- QSrollArea and got the same Problem as i have. So i think that i can not paint in a scrollarea ??? ### import sys from PyQt4 import QtCore, QtGui import basicdrawing_rc class RenderArea(QtGui.QScrollArea): Line, Points, Polyline, Polygon, Rect, RoundRect, Ellipse, Arc, \ Chord, Pie, Path, Text, Pixmap = range(13) ###WHAT SHALL THAT ???### def __init__(self, parent = None): QtGui.QScrollArea.__init__(self, parent) ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
[PyQt] Re: how to show interactive Picture
I go it. :-) I am not sure, but maybe i have to set a Widget in the ScrollArea, and in this Widget i may paint? Here is my Code: ## mport sys from PyQt4.QtGui import QMainWindow, QDockWidget, QPainter, QAction, QScrollArea from PyQt4.QtGui import QPaintEvent, QPixmap, QPen, QBrush, QWidget, QLabel from PyQt4.QtGui import QImage, QPalette from PyQt4 import QtCore class ScrollArea(QScrollArea): def __init__(self, parent=None): QScrollArea.__init__(self, parent) self.imagelabel = QLabel() self.image = QImage(./media/images/aventurien.jpg) self.imagelabel.setPixmap(QPixmap.fromImage(self.image)) self.setWidget(self.imagelabel) 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 = ScrollArea() self.scrollArea.setBackgroundRole(QPalette.Dark) self.setCentralWidget(self.scrollArea) self.readSettings() ... Regards Markus ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Re: how to show interactive Picture
On Tue, 10 Feb 2009 22:42:24 +0100, Markus Feldmann wrote: I go it. :-) :-) I am not sure, but maybe i have to set a Widget in the ScrollArea, and in this Widget i may paint? That's the idea, yes. Here is my Code: [...] class ScrollArea(QScrollArea): def __init__(self, parent=None): QScrollArea.__init__(self, parent) self.imagelabel = QLabel() self.image = QImage(./media/images/aventurien.jpg) self.imagelabel.setPixmap(QPixmap.fromImage(self.image)) self.setWidget(self.imagelabel) Here, because you are using a QLabel to show the image, it should automatically appear correctly in the QScrollArea. (Aside: If you create your own custom widget (by subclassing QWidget), you have to reimplement the sizeHint() method as well as the paintEvent() method to make it appear correctly in a QScrollArea. By using a standard widget designed to show an image, you neatly avoid this problem.) 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 = ScrollArea() self.scrollArea.setBackgroundRole(QPalette.Dark) self.setCentralWidget(self.scrollArea) self.readSettings() ... This should work. I'll post comments on your earlier message about the Basic Drawing example in a separate message. You might also find it useful to look at the Image Viewer example (widgets/imageviewer). David ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Re: how to show interactive Picture
On Tue Feb 10 18:15:52 GMT 2009, Markus Feldmann wrote: To compare i post the example. There are some commands in the example i didn't understand. I do some notes in the Code !!! import sys from PyQt4 import QtCore, QtGui import basicdrawing_rc Note that you need to have this module to be able to show the image used by the example. It is created by running pyrcc4 in the basicdrawing directory like this: pyrcc4 -o basicdrawing_rc.py basicdrawing.qrc This causes all the resources declared in the basicdrawing.qrc file (it's just XML) to be written to a Python source file that can be imported. class RenderArea(QtGui.QWidget): Line, Points, Polyline, Polygon, Rect, RoundRect, Ellipse, Arc, \ Chord, Pie, Path, Text, Pixmap = range(13) ###WHAT SHALL THAT ???### Someone just wanted to define some constants. It's quicker to use Python's sequence unpacking feature with a range() call instead of doing this: Line = 0 Points = 1 Polyline = 2 ... and so on. def __init__(self, parent = None): QtGui.QWidget.__init__(self, parent) self.shape = RenderArea.Pixmap ###IS IT CALLING HIMSELF ???### No, it's just referencing the Pixmap constant (with a value of 12). self.pen = QtGui.QPen() self.brush = QtGui.QBrush() self.pixmap = QtGui.QPixmap() self.pixmap.load(:/images/qt-logo.png) Here's what might be causing the image not to appear. If, for some reason, the basicdrawing_rc.py file was created without the image data, this will fail, leaving you with a null pixmap. def minimumSizeHint(self): return QtCore.QSize(100, 100) def sizeHint(self): return QtCore.QSize(400, 200) Defining these two methods are important if you want your widget to have minimum and preferred sizes. If you put a widget into a QScrollArea and it doesn't have these methods, it might not appear at all. def setPen(self, pen): self.pen = pen self.update() def setBrush(self, brush): self.brush = brush self.update() def paintEvent(self, event): painter = QtGui.QPainter() painter.begin(self) painter.setPen(self.pen) painter.setBrush(self.brush) painter.save() painter.drawPixmap(10, 10, self.pixmap) painter.restore() painter.end() Here we draw the pixmap. If everything was set up correctly, this should just work. Does this make things any clearer? David ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
[PyQt] Re: how to show interactive Picture
David Boddie schrieb: Here, because you are using a QLabel to show the image, it should automatically appear correctly in the QScrollArea. (Aside: If you create your own custom widget (by subclassing QWidget), you have to reimplement the sizeHint() method as well as the paintEvent() method to make it appear correctly in a QScrollArea. By using a standard widget designed to show an image, you neatly avoid this problem.) Maybe this is intersting for me, because i want to zoom in and out into the image. But i posted this Problem in a new Thread. :-) This should work. I'll post comments on your earlier message about the Basic Drawing example in a separate message. You might also find it useful to look at the Image Viewer example (widgets/imageviewer). You mean the Link: file:///usr/share/doc/python-qt4-doc/html/widgets-imageviewer.html The Package under Debian is named qt4-doc-html and is round about 80Mbyte big. Regards Markus ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
[PyQt] Re: how to show interactive Picture
David Boddie schrieb: import basicdrawing_rc Note that you need to have this module to be able to show the image used by the example. It is created by running pyrcc4 in the basicdrawing directory like this: pyrcc4 -o basicdrawing_rc.py basicdrawing.qrc This causes all the resources declared in the basicdrawing.qrc file (it's just XML) to be written to a Python source file that can be imported. Here's what might be causing the image not to appear. If, for some reason, the basicdrawing_rc.py file was created without the image data, this will fail, leaving you with a null pixmap. I understand, but my Programm does not have any resource files. And it appears correctly, why? Is it because of my special solution ? So when do i need the resource file and what is the advantage of the resource file? How can i create basicdrawing_rc.py file?(qmake) Does this make things any clearer? Yes :-) ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Re: how to show interactive Picture
On Tue Feb 10 23:04:55 GMT 2009, Markus Feldmann wrote: David Boddie schrieb: Here, because you are using a QLabel to show the image, it should automatically appear correctly in the QScrollArea. (Aside: If you create your own custom widget (by subclassing QWidget), you have to reimplement the sizeHint() method as well as the paintEvent() method to make it appear correctly in a QScrollArea. By using a standard widget designed to show an image, you neatly avoid this problem.) Maybe this is intersting for me, because i want to zoom in and out into the image. But i posted this Problem in a new Thread. :-) Yes, I'll reply there. This should work. I'll post comments on your earlier message about the Basic Drawing example in a separate message. You might also find it useful to look at the Image Viewer example (widgets/imageviewer). You mean the Link: file:///usr/share/doc/python-qt4-doc/html/widgets-imageviewer.html I'm using Ubuntu 7.10 and I don't see a python-qt4-doc/html directory containing that file, but that would appear to be the example I'm referring to. The Package under Debian is named qt4-doc-html and is round about 80Mbyte big. Possibly. I didn't realise that there's a package with Python versions of the HTML example pages. The source code is part of the python-qt4-doc package: http://packages.debian.org/lenny/python-qt4-doc The C++ version of the example is documented online here: http://doc.trolltech.com/4.4/widgets-imageviewer.html David ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Re: [PyQt] Re: how to show interactive Picture
On Wed, 11 Feb 2009 00:24:47 +0100, Markus Feldmann wrote: Here's what might be causing the image not to appear. If, for some reason, the basicdrawing_rc.py file was created without the image data, this will fail, leaving you with a null pixmap. I understand, but my Programm does not have any resource files. And it appears correctly, why? Actually, I don't know. How do you load the image in your program? Is it because of my special solution ? Which is this, I guess: self.image = QImage(./media/images/aventurien.jpg) This will work, but be aware that you are using a relative path to specify the file. What happens if you try to run the program from a different directory? So when do i need the resource file and what is the advantage of the resource file? How can i create basicdrawing_rc.py file?(qmake) Just use the pyrcc4 tool at the command line: pyrcc4 -o basicdrawing_rc.py basicdrawing.qrc This creates the basicdrawing_rc.py file (the -o option tells pyrcc4 to write to this file). It's not necessary to understand how to use resources initially, but you will find them useful when you start wanting to run the program from within different directories. David ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
[PyQt] Re: how to show interactive Picture
David Boddie schrieb: It's not necessary to understand how to use resources initially, but you will find them useful when you start wanting to run the program from within different directories. This is a really important thing, but maybe i can bypass this with some python commands. :-) As i understood, the advantage of the resource system is safety?! I wil think about, whether i need safety to find every time my pictures. :-) ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt
[PyQt] Re: how to show interactive Picture
Mads Ipsen schrieb: 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: Hi, This is not all of my Code. So there is a show() in my main source code. This Source Code only shows a sub-widget of a MDI Application. However this window is shown, but without the picture. How do the program know in which widget shall the Pixmap be shown ? ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt