我试图更改我的Qpainter的大小,但我无法弄清楚有人可以在这里帮助我查看我的代码,因为我需要的代码已嵌入其他代码中,不需要谢谢您的帮助。
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_())
最佳答案
drawPixmap函数需要将要绘制矩形的矩形作为第一个参数,即(0、0,宽度,高度)
def paintEvent(self, event):
painter = QPainter(self)
pixmap = QPixmap("Images\Image.png")
painter.drawPixmap(QRect(0, 0, pixmap.width(), pixmap.height()), pixmap)
完整的代码:
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_())