Aoof
Aoof

Reputation: 332

How can I get the current height of a QLabel?

In here, the label is cut and the imageLabel picture of the cat has the value of x = 0 and y = the height of the label.

The label is Cut

layout = QVBoxLayout()

widget = QWidget()

label = QLabel("Lourim Ipsum ...", parent=widget) # LONG TEXT
label.setWordWard(True)

image = QPixmap("cat.png")
imageLabel = QLabel(parent=widget)
imageLabel.setPixmap(image)
imageLabel.setGeometry(0, label.height(), image.width(), image.height())

layout.addWidget(widget)

UPDATE:

I have fixed the problem by doing some maths after setWordWrap Just like this

layout = QVBoxLayout()

widget = QWidget()

label = QLabel("Lourim Ipsum ...", parent=widget) # LONG TEXT
label.setWordWard(True)
labe.adjustSize() # THE MOST IMPORTANT LINE

image = QPixmap("cat.png")
imageLabel = QLabel(parent=widget)
imageLabel.setPixmap(image)

Set a constant width as 761 since its the layout's default width and set the height to this

dec = image.width()/761
wid = round(image.width()/dec) # Which will be 761.0 rounded to 761
hei = round(image.height()/dec)
imageLabel.setGeometry(0, label.height(), wid, hei)
imageLabel.adjustSize()

layout.addWidget(widget)

Upvotes: 1

Views: 5082

Answers (1)

ekhumoro
ekhumoro

Reputation: 120688

The word-wrapping must be set correctly on the top label, and both labels must be added to the layout. The layout must also be set on the container widget. It is not necessary to set the geometry of the labels, because the layout will do that automatically.

UPDATE:

There is a problem with layouts that contain labels with word-wrapping. It seems the height calculation can be wrong sometimes, which means the widgets may overlap.

Below is a demo that fixes these issues:

enter image description here

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

app = QtWidgets.QApplication(sys.argv)

TITLE = 'Cat for sale: Mint condition, still in original packaging'

class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        layout = QtWidgets.QVBoxLayout(self)
        layout.setContentsMargins(10, 10, 10, 10)
        self.label = QtWidgets.QLabel(TITLE)
        self.label.setWordWrap(True)
        image = QtGui.QPixmap('cat.png')
        self.imageLabel = QtWidgets.QLabel()
        self.imageLabel.setPixmap(image)
        self.imageLabel.setFixedSize(image.size() + QtCore.QSize(0, 10))
        layout.addWidget(self.label)
        layout.addWidget(self.imageLabel)
        layout.addStretch()

    def resizeEvent(self, event):
        super().resizeEvent(event)
        height = self.label.height() + self.imageLabel.height()
        height += self.layout().spacing()
        margins = self.layout().contentsMargins()
        height += margins.top() + margins.bottom()
        if self.height() < height:
            self.setMinimumHeight(height)
        elif height < self.minimumHeight():
            self.setMinimumHeight(1)

widget= Widget()
widget.setStyleSheet('''
    background-color: purple;
    color: white;
    font-size: 26pt;
    ''')
widget.setWindowTitle('Test')
widget.setGeometry(100, 100, 500, 500)
widget.show()

app.exec_()

Upvotes: 2

Related Questions