Relax ZeroC
Relax ZeroC

Reputation: 683

How can I get widget size which inside layout

I have a QLabel in QWidget's layout , I try to use sizeHint or geometry but it doesn't work. how can I get QLabel actural width and height ?

from PySide2 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(689, 439)
        self.horizontalLayout = QtWidgets.QHBoxLayout(Form)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label = QtWidgets.QLabel(Form)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
        self.label.setSizePolicy(sizePolicy)
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtWidgets.QApplication.translate("Form", "Form", None, -1))
        self.label.setText(QtWidgets.QApplication.translate("Form", "TextLabel", None, -1))

import sys,os,asyncio
from PySide2 import QtWidgets
from PySide2.QtWidgets import (QApplication, QMainWindow, QMessageBox)
from qasync import QEventLoop, asyncSlot, asyncClose
from TestLB2 import Ui_Form

class LB(QtWidgets.QWidget, Ui_Form):


    def __init__(self, *args, **kwargs):
        super(LB, self).__init__(*args, **kwargs)
        self.setupUi(self)

        print(self.frameGeometry().width())
        print(self.label.geometry().width())
        print(self.label.sizeHint().width())


if __name__ == '__main__':

    # mp.freeze_support()
    app = QApplication(sys.argv)
    loop = QEventLoop(app)
    asyncio.set_event_loop(loop)
    w = LB()
    w.show()
    sys.exit(app.exec_())
    with loop:
        loop.run_forever()

Upvotes: 1

Views: 312

Answers (1)

eyllanesc
eyllanesc

Reputation: 244162

For efficiency reasons, only the geometric properties are updated when necessary, in the case of layouts and widgets they are only updated when they are displayed, therefore you do not get the desired values. The solution is to obtain that information an instant after displaying it using a QTimer.

import asyncio
import os
import sys

from PySide2 import QtCore, QtWidgets

from qasync import QEventLoop, asyncSlot, asyncClose

from TestLB2 import Ui_Form


class LB(QtWidgets.QWidget, Ui_Form):
    def __init__(self, *args, **kwargs):
        super(LB, self).__init__(*args, **kwargs)
        self.setupUi(self)
        #
        self.label.setStyleSheet("QLabel{background-color: red}")
        QtCore.QTimer.singleShot(0, lambda: print(self.label.width()))


if __name__ == "__main__":

    app = QtWidgets.QApplication(sys.argv)
    loop = QEventLoop(app)
    asyncio.set_event_loop(loop)
    w = LB()
    w.show()
    with loop:
        loop.run_forever()

Upvotes: 2

Related Questions