Search code examples
pythonpyqtpyqt4qpainter

python resizing a Qpainter


i have tried to change the size of my Qpainter and i can't figure out how could someone help here is my code i have looked online and i cant figure it out since the code i need is embedded in a shit tone of other code that is not needed thanks for your help.

import sys
import os
from PyQt4.QtCore import QSize, QTimer
from PyQt4.QtGui import QApplication,QGraphicsRectItem , QMainWindow, QPushButton, QWidget, QIcon, QLabel, QPainter, QPixmap, QMessageBox, QAction, QKeySequence, QFont, QFontMetrics, QMovie
from PyQt4 import QtGui


class UIWindow(QWidget):
    def __init__(self, parent=None):
        super(UIWindow, self).__init__(parent)
        self.resize(QSize(400, 450))

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawPixmap(self.rect(), QPixmap("Images\Image.png"))
        painter.move(0,0)
        painter.resize(950,270)




class MainWindow(QMainWindow,):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setGeometry(490, 200, 950, 620)
        self.setFixedSize(950, 620)
        self.startUIWindow()
        self.setWindowIcon(QtGui.QIcon('Images\Logo.png'))

    def startUIWindow(self):
        self.Window = UIWindow(self)
        self.setWindowTitle("pythonw")
        self.setCentralWidget(self.Window)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    sys.exit(app.exec_())

Solution

  • The drawPixmap function requires as the first parameter the rectangle where it is to be drawn, ie (0, 0, width, height)

    def paintEvent(self, event):
        painter = QPainter(self)
        pixmap = QPixmap("Images\Image.png")
        painter.drawPixmap(QRect(0, 0, pixmap.width(), pixmap.height()), pixmap)
    

    Complete Code:

    import sys
    import os
    from PyQt4.QtCore import QSize, QTimer, QRect
    from PyQt4.QtGui import QApplication,QGraphicsRectItem , QMainWindow, QPushButton, QWidget, QIcon, QLabel, QPainter, QPixmap, QMessageBox, QAction, QKeySequence, QFont, QFontMetrics, QMovie
    from PyQt4 import QtGui
    
    
    class UIWindow(QWidget):
        def __init__(self, parent=None):
            super(UIWindow, self).__init__(parent)
            self.resize(QSize(400, 450))
    
        def paintEvent(self, event):
            painter = QPainter(self)
            pixmap = QPixmap("Images\Image.png")
            painter.drawPixmap(QRect(0, 0, pixmap.width(), pixmap.height()), pixmap)
    
    
    class MainWindow(QMainWindow,):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
            self.setGeometry(490, 200, 950, 620)
            self.setFixedSize(950, 620)
            self.startUIWindow()
    
        def startUIWindow(self):
            self.Window = UIWindow(self)
            self.setWindowTitle("pythonw")
            self.setCentralWidget(self.Window)
            self.show()
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        w = MainWindow()
        sys.exit(app.exec_())