How to get the height of QVBoxLayout ?
-
How to get the height of QVBoxLayout ?
titles = QVBoxLayout() titles.addLayout(QLabel('Title')) titles.addWidget(QLabel('SubTitle')) print(titles.contentsRect()) # => PySide2.QtCore.QRect(0, 0, 0, 0)Pyside2 5.13.1 on Manjaro
-
How to get the height of QVBoxLayout ?
titles = QVBoxLayout() titles.addLayout(QLabel('Title')) titles.addWidget(QLabel('SubTitle')) print(titles.contentsRect()) # => PySide2.QtCore.QRect(0, 0, 0, 0)Pyside2 5.13.1 on Manjaro
@NonNT
Apart from what @jsulm has just said.I would assume the reason for
QRect(0, 0, 0, 0)is that if the code is as you show theQVBoxLayout, and its widgets, are in limbo-land size-/layout-wise until you actually put them onto a window or actually show it? At minimum try putting the layout somewhere, and possibly have to wait till it's actually shown, not sure about that? -
How to get the height of QVBoxLayout ?
titles = QVBoxLayout() titles.addLayout(QLabel('Title')) titles.addWidget(QLabel('SubTitle')) print(titles.contentsRect()) # => PySide2.QtCore.QRect(0, 0, 0, 0)Pyside2 5.13.1 on Manjaro
-
@J-Hilk
Good spot, I didn't actually notice that! You can add aQLayoutto aQLayout, but you can't pass aQWidgettoaddlayout()!I don't know how the python bindings handle this
I tried in PyQt5, and get run-time error:
TypeError: addLayout(self, QLayout, stretch: int = 0): argument 1 has unexpected type 'QLabel'
PySide2 run-time error:
TypeError: 'PySide2.QtWidgets.QBoxLayout.addLayout' called with wrong argument types:
PySide2.QtWidgets.QBoxLayout.addLayout(QLabel)
Supported signatures:
PySide2.QtWidgets.QBoxLayout.addLayout(PySide2.QtWidgets.QLayout, int = 0)Don't you just love it when Python has to run-time error where a decent language would pick it up at compile time :(
-
@J-Hilk
Good spot, I didn't actually notice that! You can add aQLayoutto aQLayout, but you can't pass aQWidgettoaddlayout()!I don't know how the python bindings handle this
I tried in PyQt5, and get run-time error:
TypeError: addLayout(self, QLayout, stretch: int = 0): argument 1 has unexpected type 'QLabel'
PySide2 run-time error:
TypeError: 'PySide2.QtWidgets.QBoxLayout.addLayout' called with wrong argument types:
PySide2.QtWidgets.QBoxLayout.addLayout(QLabel)
Supported signatures:
PySide2.QtWidgets.QBoxLayout.addLayout(PySide2.QtWidgets.QLayout, int = 0)Don't you just love it when Python has to run-time error where a decent language would pick it up at compile time :(
-
@J-Hilk
Yes, they both gave me a stacktrace/code lines. Didn't paste, but I didn't mean to imply they did not produce those. Just that you only discover when/if you hit the code at run-time, where code above would not compile in C++. Since I have to use Python for my Qt work, I like to moan about it a lot ;) -
@J-Hilk Sorry, yes, it should be
addWidgetlogo = QSvgWidget() logo.load('logo.svg') titles = QVBoxLayout() titles.addWidget(QLabel('Title')) titles.addWidget(QLabel('SubTitle')) titleBox = QHBoxLayout() titleBox.addWidget(logo) titleBox.addLayout(titles)logoshould have the same width as height, depending on the height oftitles
but now the width oflogois dynamic, but should be fixed.
forlogo.setFixedSize()I need the height oftitlesthanks
-
@J-Hilk Sorry, yes, it should be
addWidgetlogo = QSvgWidget() logo.load('logo.svg') titles = QVBoxLayout() titles.addWidget(QLabel('Title')) titles.addWidget(QLabel('SubTitle')) titleBox = QHBoxLayout() titleBox.addWidget(logo) titleBox.addLayout(titles)logoshould have the same width as height, depending on the height oftitles
but now the width oflogois dynamic, but should be fixed.
forlogo.setFixedSize()I need the height oftitlesthanks
@NonNT
for such cases, QWidgets have aint QWidget::heightForWidth(int w) constbut you have to override that function for it to work as indented. I'm not sure how to do this with python ..
Here's an example on how to do such a thing in c++, still working in Qt5.12 +
https://stackoverflow.com/a/18923122 -
@J-Hilk Sorry, yes, it should be
addWidgetlogo = QSvgWidget() logo.load('logo.svg') titles = QVBoxLayout() titles.addWidget(QLabel('Title')) titles.addWidget(QLabel('SubTitle')) titleBox = QHBoxLayout() titleBox.addWidget(logo) titleBox.addLayout(titles)logoshould have the same width as height, depending on the height oftitles
but now the width oflogois dynamic, but should be fixed.
forlogo.setFixedSize()I need the height oftitlesthanks
@NonNT said in How to get the height of QVBoxLayout ?:
for logo.setFixedSize() I need the height of titles
As @J-Hilk has said,
QWidget::heightForWidth()looks like the best way to go. (I don't know if https://doc.qt.io/qt-5/qsizepolicy.html#setHeightForWidth does it for you without override.) But if you are still asking to get the size of the layout, I don't see you have acted on what we said earlier? -
Don't know how to override
QWidget::heightForWidth(int w)But I can sum up the heights of the widgets and the spacing between the widgets.
title = QLabel(f'<h3>{self.title}</h3>') subTitle = QLabel(self.subTitle) widgetTmp = QWidget() vboxlayoutTmp = QVBoxLayout(widgetTmp) vboxlayoutHeight = title.sizeHint().height() + vboxlayoutTmp.layout().spacing() + subTitle.sizeHint().height() logo = QSvgWidget() logo.load('logo.svg') logo.setFixedSize(vboxlayoutHeight, vboxlayoutHeight) labels = QVBoxLayout() labels.addWidget(title) labels.addWidget(subTitle) titleBox = QHBoxLayout() titleBox.addWidget(logo) titleBox.addLayout(labels)