[PyQt] Best way to display additional window

Phil Thompson phil at riverbankcomputing.com
Fri Dec 5 09:07:27 GMT 2008

On Fri, 5 Dec 2008 00:10:00 +0100, "Filip Gruszczyński"
<gruszczy at gmail.com> wrote:
> I would like to be able to display a very simple widget: widget with
> an image drawn inside (it is supposed to display an image, that was
> previously visible only on a thumbnail). This should be opened in a
> new window.
> First thing I tried was QDialog. But it seems, that just painting on
> it using paintEvent is not enough and it disappears (though I used
> exec_ on it). So I went back to a widget, checked the docs for making
> it a standalone window and tried something like this:
> class ImageViewer(QWidget):
> 	def __init__(self, resource, parent):
> 		QWidget.__init__(self, parent, Qt.Popup)
> 		self.__resource = resource
> 		self.repaint()
> 	def paintEvent(self, event):
> 		painter = QPainter(self)
> 		image = QImage(self.__resource.file())
> 		self.resize(image.size())
> 		painter.drawImage(QRect(QPoint(0, 0), image.size()), image)
> This should be modal, top-level window. I call show on it, but it
> appears only for a moment to disappear after that. I do:
> 			column = event.pos().x() /  self.RectangleFullWidth
> 			row = event.pos().y() /  self.RectangleFullHeight
> 			image = ImageViewer(self.__drawn[(column, row)], self)
> 			image.show()
> (I am picking thumbnail from the displayed and trying to display it in
> full).
> I'd really appreciate some advice on how can I achieve a stand alone
> window for displaying some stuff, e.g. an Image. As long as there are
> some widgets inside it's easy, because dialog is enough. But this
> doesn't.

Convert the image to a QPixmap and use a QLabel.


More information about the PyQt mailing list