# SinglePictureQt.java (c) Kari Laitinen # http://www.naturalprogramming.com/ # 2009-11-09 File created. # 2009-11-09 Last modification. # This program demonstrates how a PyQt program can show # and scale pictures. The picture file must be in the same # folder where this program is located. import sys from PyQt4 import QtGui, QtCore class SinglePictureWindow( QtGui.QWidget ) : def __init__( self, parent=None ) : QtGui.QWidget.__init__( self, parent ) self.setGeometry( 100, 100, 800, 480 ) self.setWindowTitle( "SINGLE PICTURE IN SEVERAL SIZES" ) self.picture_to_show = QtGui.QImage( "governor_arnold_schwarzenegger.jpg" ) def paintEvent( self, event ) : painter = QtGui.QPainter() painter.begin( self ) picture_width = self.picture_to_show.width() picture_height = self.picture_to_show.height() picture_position_x = 15 picture_position_y = 15 painter.drawImage( picture_position_x, picture_position_y, self.picture_to_show ) picture_position_x = picture_position_x + picture_width + 10 # Next we'll show a smaller version of the picture. # The first parameter for the drawImage() method specifies # the rectangle into which a smaller picture is drawn. # The third parameter specifies that we use the whole picture # which we'll squeeze into the smaller rectangle. painter.drawImage( QtCore.QRect( picture_position_x, picture_position_y, picture_width / 2, picture_height / 2 ), self.picture_to_show, QtCore.QRect( 0, 0, picture_width, picture_height ) ) picture_position_x = picture_position_x + picture_width / 2 + 10 # Finally, we'll show the picture so that its width is enlarged. # Here we use class QRectF in place of QRect. The letter F in # the class name means that we can use floating-point values. painter.drawImage( QtCore.QRectF( picture_position_x, picture_position_y, picture_width * 1.5, picture_height ), self.picture_to_show, QtCore.QRectF( 0, 0, picture_width, picture_height ) ) painter.end() # Here is the main program: this_application = QtGui.QApplication( sys.argv ) application_window = SinglePictureWindow() application_window.show() this_application.exec_()