Bala
Bala

Reputation: 686

How to obtain the Minimum Size/Fixed Size of the frame based on the Contents in PyQt5?

I want to set the frame size to Minimum/Fixed, based on the contents of the frame. For example, in my code, I set a frame Size value by manual(line 47), instead of this, how to calculate/obtain the Size of the Frame by code itself?

import sys, os
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


def stylesheet_country_63():
    return """
           QLabel{
           font-family: Trebuchet MS; font-style: normal; font-size:10pt; font-weight:400;
           color:Black; padding:6px;
           qproperty-alignment: 'AlignVCenter | AlignRight';
           min-width:170px;
           }
           """

class AssistScriptClassCountry63(QWidget):

    def __init__(self):
        super().__init__()
        self.setWindowTitle("Country")

        self.widgets_country_63()
        self.layout_country_63()

    def widgets_country_63(self):
        self.lbl_country63_name = QLabel("Country Name :")
        self.lbl_country63_isdcode = QLabel("Isd Code :")
        self.lbl_country63_currency = QLabel("Currency :")
        self.lbl_country63_curascii = QLabel("Currency Symbol :\n (Ascii Value)  ")
        self.lbl_country63_curicon = QLabel(" Currency Symbol :")

        self.lbl_country63_name.setStyleSheet(stylesheet_country_63())
        self.lbl_country63_isdcode.setStyleSheet(stylesheet_country_63())
        self.lbl_country63_currency.setStyleSheet(stylesheet_country_63())
        self.lbl_country63_curascii.setStyleSheet(stylesheet_country_63())
        self.lbl_country63_curicon.setStyleSheet(stylesheet_country_63())

        self.tbx_country63_name = QLineEdit()
        self.tbx_country63_isdcode = QLineEdit()
        self.tbx_country63_currency = QLineEdit()
        self.tbx_country63_curascii = QLineEdit()
        self.tbx_country63_curicon = QLineEdit()

    def layout_country_63(self):
        self.layout_country63_outerframe = QHBoxLayout()

        self.frame_country63_innerframe = QFrame()
        self.frame_country63_innerframe.setStyleSheet("background-color:lightgreen")
        self.layout_country63_innerframe = QFormLayout(self.frame_country63_innerframe)

        self.layout_country63_innerframe.addRow(self.lbl_country63_name, self.tbx_country63_name)
        self.layout_country63_innerframe.addRow(self.lbl_country63_isdcode, self.tbx_country63_isdcode)
        self.layout_country63_innerframe.addRow(self.lbl_country63_currency, self.tbx_country63_currency)
        self.layout_country63_innerframe.addRow(self.lbl_country63_curascii, self.tbx_country63_curascii)
        self.layout_country63_innerframe.addRow(self.lbl_country63_curicon, self.tbx_country63_curicon)

        self.frame_country63_innerframe.setFixedSize(300,180)
        self.layout_country63_outerframe.addWidget(self.frame_country63_innerframe)
        self.setLayout(self.layout_country63_outerframe)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    countrywin = AssistScriptClassCountry63()
    # qApp.setStyleSheet(stylesheet_country_63())
    countrywin.show()
    sys.exit(app.exec_())

Upvotes: 2

Views: 1047

Answers (1)

musicamante
musicamante

Reputation: 48260

Use the sizeHint() of the widget:

This property holds the recommended size for the widget [...]
The default implementation of sizeHint() returns an invalid size if there is no layout for this widget, and returns the layout's preferred size otherwise.

(emphasis mine)

The preferred size for a layout is the preferred size for all child widgets (and layouts) managed by that layout. What you'll get will be the computed size based on the various size constraints (minimum/maximum/fixed sizes), hints and policies of each child item.

    hint = self.frame_country63_innerframe.sizeHint()
    self.frame_country63_innerframe.setMinimumSize(hint)

Note: you either set a minimum size OR a fixed size, trying to do both is pointless: setting a fixed size actually results in setting both minimum and maximum size to the same value.

Upvotes: 3

Related Questions