如何获取 QLabel 的当前高度?
How can I get the current height of a QLabel?
在这里,标签被切割,imageLabel
猫的图片的值为x = 0,y =标签的高度。
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:
我在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
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)
顶部标签的自动换行必须正确设置,并且两个标签都必须添加到布局中。还必须在容器小部件上设置布局。不需要设置标签的几何形状,因为布局会自动完成。
更新:
有一个problem with layouts that contain labels with word-wrapping。似乎有时高度计算会出错,这意味着小部件可能会重叠。
下面是修复这些问题的演示:
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_()
在这里,标签被切割,imageLabel
猫的图片的值为x = 0,y =标签的高度。
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:
我在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
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)
顶部标签的自动换行必须正确设置,并且两个标签都必须添加到布局中。还必须在容器小部件上设置布局。不需要设置标签的几何形状,因为布局会自动完成。
更新:
有一个problem with layouts that contain labels with word-wrapping。似乎有时高度计算会出错,这意味着小部件可能会重叠。
下面是修复这些问题的演示:
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_()