cyanidem
cyanidem

Reputation: 103

Why does Qt.AlignCenter onQLabel differ for image and text in PyQt5?

I am new to python and currently doing some basic excerises. I'm trying to rewrite some application in which I used tkinter to do the same with PyQt5. Everything works apart from one problem - I have a QLabel containing image and I'm trying to align the image in the center of label but it doesn't want to, image stays aligned to the left. This was answered by @eyllanesc, who suggested that the QLabel is not centered with respect to the window and I should center the widget by changing to:

layout.addWidget(label_img, alignment=Qt.AlignCenter)

And that worked perfectly, however there are two more widgets (label_top, label_bottom) in the same layout, labels with text.And despite not beign aligned to center of the window, the text displayed centered. Why does label with text behave different to label with image?

Code below:

from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout, QPushButton, QFileDialog
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon, QPixmap

app=QApplication([])
window=QWidget()
window.setFixedSize(500,500)

layout=QVBoxLayout()

label_top=QLabel('PLEASE WAIT')
label_top.setAlignment(Qt.AlignCenter)
label_top.setStyleSheet("font: 20pt Bahnschrift; background-color: #ffd167; color: black")
layout.addWidget(label_top)

label_img=QLabel()
label_img.setFixedSize(300, 300)
label_img.setAlignment(Qt.AlignCenter)
image = QFileDialog.getOpenFileName(None,'Select file','D:\_Download', "Image files(*.png *.jpg *.jpeg *.gif)")
imagePath = image[0]
pixmap = QPixmap(imagePath)
pixmap.scaledToHeight(label_img.height(), Qt.SmoothTransformation)
label_img.setPixmap(pixmap)
#label_img.resize(pixmap.width(),pixmap.height())
layout.addWidget(label_img, alignment=Qt.AlignCenter)

label_bottom=QLabel('PLEASE WAIT')
label_bottom.setAlignment(Qt.AlignCenter)
label_bottom.setStyleSheet("font: 20pt Bahnschrift; background-color: #ffd167; color: black")
layout.addWidget(label_bottom)

window.setLayout(layout)
window.show()
app.setStyle('Fusion')
app.exec_()

Upvotes: 1

Views: 1447

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

First, it must be understood that it means label.setAlignment(Qt.AlignCenter), that code indicates that the element that is displayed (QPixmap or text) in the QLabel will be in the center of the QLabel, not in the center of the window.

In the example the difference is given by the size of the labels since the first and third labels do not have fixed sizes unlike the second one that has a size of 300x500, so the behavior of a QLabel without fixed size is to occupy the entire space Possible as the following code shows:

from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout

if __name__ == '__main__':
    app=QApplication([])
    window=QWidget()
    window.setFixedSize(500,500)


    label_top=QLabel()
    label_top.setStyleSheet("background-color: red;")

    label_img=QLabel()
    label_img.setFixedSize(300, 300)
    label_img.setStyleSheet("background-color: green;")

    label_bottom=QLabel()
    label_bottom.setStyleSheet("background-color: blue;")

    layout=QVBoxLayout(window)
    layout.addWidget(label_top)
    layout.addWidget(label_img) 
    layout.addWidget(label_bottom)

    window.show()

    app.exec_()

enter image description here

So in QLabel that does not have a fixed size like the first and third, the alignment with respect to the window is irrelevant unlike the second because if you want the content of the QLabel (text or image) to be centered with respect to the window this It will have to be centered as well.


For example, other solutions are:

  • Do not set a fixed size to the width but only to the height.
  • establish that the width of the middle QLabel is 500.

Upvotes: 2

Related Questions