简体   繁体   English

如何获取 QLabel 的当前高度?

[英]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.在这里,label 被切割,猫的imageLabel图片的 x = 0 和 y = label 的高度。

标签是剪切

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我已经通过在setWordWrap之后做一些数学来解决这个问题就像这样

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将恒定宽度设置为 761,因为它是布局的默认宽度,并将高度设置为此

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)

The word-wrapping must be set correctly on the top label, and both labels must be added to the layout.顶部 label 必须正确设置自动换行,并且必须将两个标签都添加到布局中。 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 .包含带有 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:以下是修复这些问题的演示:

在此处输入图像描述

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_()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM