Problems aligning buttons in widget in pyqt5 - python

I've been trying to create a widget with image background and two buttons (cancel and ok) with image and push down/up effect, using pyqt5 GUI of python language, but I have two problem:
1 – I can't align the buttons in widget center
2 – The buttons events occur twice
Code:
import sys
from PyQt5 import QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class PicButton(QAbstractButton):
def __init__(self, pixmap, pixmap_pressed, id_button, parent=None):
super(PicButton, self).__init__(parent)
self.pixmap = pixmap
self.pixmap_pressed = pixmap_pressed
self.id_buton = id_button
self.pressed.connect(self.update)
self.released.connect(self.update)
def paintEvent(self, event):
if self.isDown():
pix = self.pixmap_pressed
print("botao pressionado: ", self.id_buton)
else:
pix = self.pixmap
painter = QPainter(self)
painter.drawPixmap(event.rect(), pix)
def enterEvent(self, event):
self.update()
def leaveEvent(self, event):
self.update()
def sizeHint(self):
return QSize(131, 82)
app = QApplication(sys.argv)
window = QWidget()
window.setGeometry(800, 450, 800, 450)
pixmap = QPixmap("background.png")
brush = QBrush(pixmap)
palette = QPalette()
palette.setBrush(QPalette.Background, brush)
window.setPalette(palette)
button1 = PicButton(QtGui.QPixmap("cancel_up.png"), QtGui.QPixmap("cancel_down.png"), "cancel")
button2 = PicButton(QtGui.QPixmap("ok_up.png"), QtGui.QPixmap("ok_down.png"), "ok")
layout = QHBoxLayout()
layout.addStretch(1)
layout.addWidget(button1)
layout.addWidget(button2)
layout.setAlignment(Qt.AlignCenter)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
Get images in link: https://www.filedropper.com/imagens
Can somebody please tell me what I'm doing wrong?

I can't align the buttons in widget center: If you have applied addStretch() before button1 so that it is symmetrical you must set another addStretch() after button2
The buttons events occur twice: QAbstractButton already calls the update method when you press the button so it is not necessary to connect pressed or released signals to the update() method or call it in the enterEvent() and leaveEvent() methods.
Considering the above the solution is:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class PicButton(QtWidgets.QAbstractButton):
def __init__(self, pixmap, pixmap_pressed, id_button, parent=None):
super(PicButton, self).__init__(parent)
self.pixmap = pixmap
self.pixmap_pressed = pixmap_pressed
self.id_buton = id_button
def paintEvent(self, event):
if self.isDown():
pix = self.pixmap_pressed
print("botao pressionado: ", self.id_buton)
else:
pix = self.pixmap
painter = QtGui.QPainter(self)
painter.drawPixmap(event.rect(), pix)
def sizeHint(self):
return QtCore.QSize(131, 82)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window =QtWidgets. QWidget()
window.setGeometry(800, 450, 800, 450)
pixmap = QtGui.QPixmap("background.png")
brush = QtGui.QBrush(pixmap)
palette = QtGui.QPalette()
palette.setBrush(QtGui.QPalette.Background, brush)
window.setPalette(palette)
button1 = PicButton(QtGui.QPixmap("cancel_up.png"), QtGui.QPixmap("cancel_down.png"), "cancel")
button2 = PicButton(QtGui.QPixmap("ok_up.png"), QtGui.QPixmap("ok_down.png"), "ok")
layout = QtWidgets.QHBoxLayout(window)
layout.addStretch()
layout.addWidget(button1)
layout.addWidget(button2)
layout.addStretch()
window.show()
sys.exit(app.exec_())

Related

points are drawn on main window not on the label in the GUI

i'm loading an image on a label supposed after that
a mouse click event that draws dots on the label but dots are drawn on the main window ( behind the label )
GUI Image
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super(ApplicationWindow, self).__init__()
uic.loadUi('MainWindow.ui', self)
self.setFixedSize(self.size())
self.show()
self.points = QtGui.QPolygon()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def mousePressEvent(self, e):
self.points << e.pos()
self.update()
def paintEvent(self, ev):
qp = QtGui.QPainter(self)
qp.setRenderHint(QtGui.QPainter.Antialiasing)
pen = QtGui.QPen(QtCore.Qt.red, 5)
brush = QtGui.QBrush(QtCore.Qt.red)
qp.setPen(pen)
qp.setBrush(brush)
for i in range(self.points.count()):
# qp.drawEllipse(self.points.point(i), 5, 5)
# or
qp.drawPoints(self.points)
def main():
app = QtWidgets.QApplication(sys.argv)
application = ApplicationWindow()
application.show()
sys.exit(app.exec_())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = ApplicationWindow()
sys.exit(app.exec_())
main()
You are calling QPainter(self) so the paint device is the MainWindow. Instead call QPainter on the QPixmap. Here is an example.
class Template(QWidget):
def __init__(self):
super().__init__()
self.lbl = QLabel()
self.pix = QPixmap('photo.jpeg')
grid = QGridLayout(self)
grid.addWidget(self.lbl, 0, 0)
self.points = QPolygon()
def mousePressEvent(self, event):
self.points << QPoint(event.x() - self.lbl.x(), event.y() - self.lbl.y())
def paintEvent(self, event):
qp = QPainter(self.pix)
qp.setRenderHint(QPainter.Antialiasing)
pen = QPen(Qt.red, 5)
brush = QBrush(Qt.red)
qp.setPen(pen)
qp.setBrush(brush)
qp.drawPoints(self.points)
self.lbl.setPixmap(self.pix)
However a better way is to use QImage in a custom widget that will act as a canvas.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class Canvas(QWidget):
def __init__(self, photo, *args, **kwargs):
super().__init__(*args, **kwargs)
self.image = QImage(photo)
self.setFixedSize(self.image.width(), self.image.height())
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
qp = QPainter(self.image)
qp.setRenderHint(QPainter.Antialiasing)
qp.setPen(QPen(Qt.red, 5))
qp.setBrush(Qt.red)
qp.drawPoint(event.pos())
self.update()
def paintEvent(self, event):
qp = QPainter(self)
rect = event.rect()
qp.drawImage(rect, self.image, rect)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
w = QWidget()
self.setCentralWidget(w)
grid = QGridLayout(w)
grid.addWidget(Canvas('photo.jpeg'))
if __name__ == '__main__':
app = QApplication(sys.argv)
gui = MainWindow()
gui.show()
sys.exit(app.exec_())
Output:

setting background image for a widget in another class

I've been recently learning pyqt5 as my first gui framework. So far I have been experimenting with QtStackedLayout. I currently have two window screens, one created inside the UI class and another in another separate class. I have two concerns:
Everything was working until I started experimenting on adding a background image for Window 1. There is no image displayed but the code runs ok.
There is this small fraction of time in the beginning where window one will get displayed first until it gets loaded to the mainwindow, I tried passing self during instantiation of the object to sert as some kind of parent to prevent this but I think I'm not doing it right.
See below code (I have bad import statements, I will sort it out)
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Ui(QWidget):
def setupUi(self, Main, width, height):
self.stack = QStackedLayout()
self.window_1 = WindowOne(width, height)
self.window_2 = QWidget(self)
self.window_2_UI()
self.stack.addWidget(self.window_1)
self.stack.addWidget(self.window_2)
# Only one button
self.btn = QPushButton("Change window", self)
# Create the central widget of your Main Window
self.main_widget = QWidget(self)
layout = QVBoxLayout(self.main_widget)
layout.addLayout(self.stack)
layout.addWidget(self.btn)
self.setCentralWidget(self.main_widget)
self.btn.clicked.connect(self.change_window)
def change_window(self):
if self.stack.currentIndex() == 0:
self.stack.setCurrentIndex(1)
else:
self.stack.setCurrentIndex(0)
def window_2_UI(self):
label = QLabel("In Window 2", self.window_2)
class WindowOne(QWidget):
def __init__(self, width, height):
super().__init__()
self.set_bg(width, height)
self.set_label()
# self.setStyleSheet("background-image: url(:resource/images/blue_bg.jpg)")
def set_label(self):
label = QLabel("In Window 1", self)
def set_bg(self, w, h):
oImage = QImage("resource/images/blue_bg.jpg")
sImage = oImage.scaled(QSize(w, h))
palette = QPalette()
palette.setBrush(10, QBrush(sImage))
self.setPalette(palette)
class Main(QMainWindow, Ui):
def __init__(self):
super().__init__()
self.w_width = 480
self.w_height = 720
self.resize(self.w_width, self.w_height)
self.init_ui()
self.setupUi(self, self.w_width, self.w_height)
def init_ui(self):
self.center()
self.setWindowTitle('Main Window')
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
if __name__ == "__main__":
app = QApplication(sys.argv)
M = Main()
M.show()
sys.exit(app.exec())
By default only the window (which is different to a widget) will use the background color of QPalette, if you want a widget to use the background color of QPalette you must enable the autoFillBackground property.
# ...
self.set_label(width, height)
self.setAutoFillBackground(True)
# ...
Although your code is a little messy, for example you establish that certain methods receive certain parameters but you never use them. Finally I think you want the background of the image to be re-scale using the size of the window so I have override the resizeEvent() method so that scaling takes the size of the window.
Considering all the above, I have improved your code:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.m_stacked_layout = QtWidgets.QStackedLayout()
self.widget_1 = WidgetOne()
self.widget_2 = QtWidgets.QWidget()
self.widget_2_UI()
self.m_stacked_layout.addWidget(self.widget_1)
self.m_stacked_layout.addWidget(self.widget_2)
button = QtWidgets.QPushButton(
"Change window", clicked=self.change_window
)
lay = QtWidgets.QVBoxLayout(self)
lay.addLayout(self.m_stacked_layout)
lay.addWidget(button)
#QtCore.pyqtSlot()
def change_window(self):
ix = self.m_stacked_layout.currentIndex()
self.m_stacked_layout.setCurrentIndex(1 if ix == 0 else 0)
def widget_2_UI(self):
label = QtWidgets.QLabel("In Window 2", self.widget_2)
class WidgetOne(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setAutoFillBackground(True)
self.set_label()
self.m_image = QtGui.QImage("resource/images/blue_bg.jpg")
def set_label(self):
label = QtWidgets.QLabel("In Window 1", self)
def resizeEvent(self, event):
palette = self.palette()
sImage = self.m_image.scaled(event.size())
palette.setBrush(10, QtGui.QBrush(sImage))
self.setPalette(palette)
super(WidgetOne, self).resizeEvent(event)
class Main(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
widget = Widget()
self.setCentralWidget(widget)
self.resize(480, 720)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = Main()
w.show()
sys.exit(app.exec())

Insert Image into QGridLayout and Draw on top of image in PyQt5

I'm pretty new to PyQt and am trying to make an application with a QPixmap on the left, which can be drawn on, and a QTextEdit on the right (for a simple OCR GUI).
I looked at:
PyQt5 Image and QGridlayout
but I couldn't connect it with the code below (I'm losing my hair with all the head scratching!!)
When I try adapting the following code, what I get is a QMainWindow with the QPixmap as the background which can be drawn on with the mouse and a second occurance of the QPixmap in it's correct position, which can not be drawn on. Can someone tell me what I'm doing wrong?
Thank you very much!
# https://stackoverflow.com/questions/51475306/
import sys
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtWidgets import QMainWindow, QApplication,QGridLayout, QLabel, QWidget, QTextEdit
from PyQt5.QtGui import QPixmap, QPainter, QPen
class Menu(QMainWindow):
def __init__(self):
super().__init__()
self.drawing = False
self.lastPoint = QPoint()
self.image = QPixmap("S3.png")
self.setGeometry(100, 100, 500, 300)
self.resize(self.image.width(), self.image.height())
layout = QGridLayout()
# Add a QTextEdit box
self.edit = QTextEdit()
layout.addWidget(self.edit, 0, 0, 10, 20)
# This:
# https://stackoverflow.com/questions/52616553
# indicates that a QPixmap must be put into a label to insert into a QGridLayout
self.label = QLabel()
self.label.setPixmap(self.image)
layout.addWidget(self.label, 10, 20, 10, 20)
# https://stackoverflow.com/questions/37304684/
self.widget = QWidget()
self.widget.setLayout(layout)
self.setCentralWidget(self.widget)
self.show()
def paintEvent(self, event):
painter = QPainter(self)
painter.drawPixmap(self.rect(), self.image)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drawing = True
self.lastPoint = event.pos()
print(self.lastPoint)
def mouseMoveEvent(self, event):
if event.buttons() and Qt.LeftButton and self.drawing:
painter = QPainter(self.image)
painter.setPen(QPen(Qt.red, 3, Qt.SolidLine))
painter.drawLine(self.lastPoint, event.pos())
print(self.lastPoint,event.pos())
self.lastPoint = event.pos()
self.update()
def mouseReleaseEvent(self, event):
if event.button == Qt.LeftButton:
self.drawing = False
if __name__ == '__main__':
app = QApplication(sys.argv)
mainMenu = Menu()
sys.exit(app.exec_())
Each widget must fulfill a specific task, so I have created a widget that only has the painted function, the main widget works as a container for the painting widget and the QTextEdit.
from PyQt5 import QtCore, QtGui, QtWidgets
class Label(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Label, self).__init__(parent)
self.image = QtGui.QPixmap("S3.png")
self.drawing = False
self.lastPoint = QtCore.QPoint()
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.drawPixmap(QtCore.QPoint(), self.image)
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
self.drawing = True
self.lastPoint = event.pos()
def mouseMoveEvent(self, event):
if event.buttons() and QtCore.Qt.LeftButton and self.drawing:
painter = QtGui.QPainter(self.image)
painter.setPen(QtGui.QPen(QtCore.Qt.red, 3, QtCore.Qt.SolidLine))
painter.drawLine(self.lastPoint, event.pos())
self.lastPoint = event.pos()
self.update()
def mouseReleaseEvent(self, event):
if event.button == QtCore.Qt.LeftButton:
self.drawing = False
def sizeHint(self):
return self.image.size()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.label = Label()
self.textedit = QtWidgets.QTextEdit()
widget = QtWidgets.QWidget()
self.setCentralWidget(widget)
lay = QtWidgets.QHBoxLayout(widget)
lay.addWidget(self.label, alignment=QtCore.Qt.AlignCenter)
lay.addWidget(self.textedit)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

How to switch screens dynamically with splash effect in pyqt5

I'm making a python application for switch screens in touchscreen display. I need to make switch screens dynamically with splash effect (not window effect), but I don't know how switch screens.
I have two screen class. The screen class have one button to switch the screens.
How do I switch screens?
Code:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class PicButton(QtWidgets.QPushButton):
def __init__(self, pixmap, pixmap_pressed, width, height, id_button, parent=None):
super(PicButton, self).__init__(parent)
self.pixmap = pixmap
self.pixmap_pressed = pixmap_pressed
self.id_buton = id_button
self.width = width
self.height = height
def paintEvent(self, event):
if self.isDown():
pix = self.pixmap_pressed
print("Pressed button: ", self.id_buton)
else:
pix = self.pixmap
painter = QtGui.QPainter(self)
painter.drawPixmap(event.rect(), pix)
def sizeHint(self):
return QtCore.QSize(self.width, self.height)
class ScreenNext(QtWidgets.QWidget):
def __init__(self):
super().__init__(flags=QtCore.Qt.SplashScreen)
self.setGeometry(800, 450, 800, 450)
pixmap = QtGui.QPixmap("background.png")
brush = QtGui.QBrush(pixmap)
palette = QtGui.QPalette()
palette.setBrush(QtGui.QPalette.Window, brush)
self.setPalette(palette)
self.next_button = PicButton(QtGui.QPixmap("next_screen_up.png"),
QtGui.QPixmap("next_screen_down.png"), 111, 61, "next")
hlayout = QtWidgets.QHBoxLayout(self)
hlayout.addStretch()
hlayout.addWidget(self.next_button)
hlayout.addStretch()
self.next_button.clicked.connect(self.switch_screen)
self.show()
def switch_screen(self):
print("ScreenNext")
class ScreenReturn(QtWidgets.QWidget):
def __init__(self):
super().__init__(flags=QtCore.Qt.SplashScreen)
self.setGeometry(800, 450, 800, 450)
pixmap = QtGui.QPixmap("background.png")
brush = QtGui.QBrush(pixmap)
palette = QtGui.QPalette()
palette.setBrush(QtGui.QPalette.Window, brush)
self.setPalette(palette)
self.return_button = PicButton(QtGui.QPixmap("return_screen_up.png"),
QtGui.QPixmap("return_screen_down.png"), 111, 61, "return")
hlayout = QtWidgets.QHBoxLayout(self)
hlayout.addStretch()
hlayout.addWidget(self.return_button)
hlayout.addStretch()
self.return_button.clicked.connect(self.switch_screen)
self.show()
def switch_screen(self):
print("ScreenReturn")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
screen = ScreenReturn()
sys.exit(app.exec_())
Get images in link: http://www.filedropper.com/images_1
Get gif demonstration in link: http://www.filedropper.com/display
According to the .gif that you share, you want the widget to occupy the entire screen, so you must use showFullScreen, on the other hand it is better to create a class that handles the transition, in this case ScreenManager will take care of making the transition when the signal associated is emitted.
import sys
from functools import partial
from PyQt5 import QtCore, QtGui, QtWidgets
class PicButton(QtWidgets.QPushButton):
def __init__(self, pixmap, pixmap_pressed, width, height, id_button, parent=None):
super(PicButton, self).__init__(parent)
self.pixmap = pixmap
self.pixmap_pressed = pixmap_pressed
self.id_buton = id_button
self.setFixedSize(width, height)
def paintEvent(self, event):
if self.isDown():
pix = self.pixmap_pressed
print("Pressed button: ", self.id_buton)
else:
pix = self.pixmap
painter = QtGui.QPainter(self)
painter.drawPixmap(event.rect(), pix)
class ScreenNext(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setGeometry(800, 450, 800, 450)
pixmap = QtGui.QPixmap("background.png")
brush = QtGui.QBrush(pixmap)
palette = QtGui.QPalette()
palette.setBrush(QtGui.QPalette.Window, brush)
self.setPalette(palette)
self.next_button = PicButton(QtGui.QPixmap("next_screen_up.png"),
QtGui.QPixmap("next_screen_down.png"), 111, 61, "next")
hlayout = QtWidgets.QHBoxLayout(self)
hlayout.addStretch()
hlayout.addWidget(self.next_button)
hlayout.addStretch()
class ScreenReturn(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setGeometry(800, 450, 800, 450)
pixmap = QtGui.QPixmap("background.png")
brush = QtGui.QBrush(pixmap)
palette = QtGui.QPalette()
palette.setBrush(QtGui.QPalette.Window, brush)
self.setPalette(palette)
self.return_button = PicButton(QtGui.QPixmap("return_screen_up.png"),
QtGui.QPixmap("return_screen_down.png"), 111, 61, "return")
hlayout = QtWidgets.QHBoxLayout(self)
hlayout.addStretch()
hlayout.addWidget(self.return_button)
hlayout.addStretch()
class ScreenManager(QtCore.QObject):
def __init__(self, parent=None):
super(ScreenManager, self).__init__(parent)
self._current_window = None
def add_transition(self, signal, screen):
conn = signal.connect(partial(self.open_window, screen))
def open_window(self, window, *args):
if self._current_window is not None:
self._current_window.hide()
window.showFullScreen()
self._current_window = window
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
screen1 = ScreenReturn()
screen2 = ScreenNext()
manager = ScreenManager()
manager.add_transition(screen1.return_button.clicked, screen2)
manager.add_transition(screen2.next_button.clicked, screen1)
screen1.showFullScreen()
sys.exit(app.exec_())

PyQt5 - resize label to fill the whole window

from PyQt5 import QtGui, QtCore, QtWidgets
class Window(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.label = QtWidgets.QLabel(self)
self.label.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Ignored)
self.label.resize(800, 600)
self.label.setContentsMargins(0, 0, 0, 0);
self.pixmap = QtGui.QPixmap("image.jpg")
self.label.setPixmap(self.pixmap)
self.label.setMinimumSize(1, 1)
self.label.installEventFilter(self)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.label)
def eventFilter(self, source, event):
if (source is self.label and event.type() == QtCore.QEvent.Resize):
self.label.setPixmap(self.pixmap.scaled(
self.label.size(), QtCore.Qt.KeepAspectRatio))
return super(Window, self).eventFilter(source, event)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
window.resize(800, 600)
sys.exit(app.exec_())
This is my application, my goal is simple - have an image that fills the whole window and resizes after the window resize.
This code works okay in resizing the image, but the label doesn't cover the whole window, I have those "borders". How can I remove them/resize the label to window size?
I am working on Windows if this changes things.
That is the effect I get now.
I solved it in PyQt4, so I'm not 100% sure if it will work for PyQt5, but I guess it should (some minor modifications might be needed, e.g. import PyQt5 instead of PyQt4).
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.label = QtGui.QLabel(self)
self.label.resize(800, 600)
pixmap1 = QtGui.QPixmap("image.png")
self.pixmap = pixmap1.scaled(self.width(), self.height())
self.label.setPixmap(self.pixmap)
self.label.setMinimumSize(1, 1)
def resizeEvent(self, event):
pixmap1 = QtGui.QPixmap("image.png")
self.pixmap = pixmap1.scaled(self.width(), self.height())
self.label.setPixmap(self.pixmap)
self.label.resize(self.width(), self.height())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
window.resize(800, 600)
sys.exit(app.exec_())
The most important for you is definition of resizeEvent. You could use already defined self.pixmap and just resize it, but the quality of the image would degrade the more resizing you use. Therefore, it's better always create new pixmap scaled to current width and height of the Window.
No need to create a QLabel inside the separate QWidget. You can simply inherit QLabel instead of QWidget. It will make your code more simple and cleaner:
class MyLabelPixmap(QtWidgets.QLabel):
def __init__(self):
QtWidgets.QLabel.__init__(self)
self.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Ignored)
self.resize(800, 600)
self.pixmap = QtGui.QPixmap("image.jpg")
self.setPixmap(self.pixmap)
self.installEventFilter(self)
def eventFilter(self, source, event):
if (source is self and event.type() == QtCore.QEvent.Resize):
self.setPixmap(self.pixmap.scaled(self.size()))
return super(Window, self).eventFilter(source, event)
In case you would like to embed your MyLabelPixmap widget into the QMainWindow just add in your QMainWindow.__init__
self.myLabelPixmap = MyLabelPixmap()
self.setCentralWidget(self.myLabelPixmap)

Categories