Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to make a QFrame smaller?



  • So I've made a class that has a QHBoxLayout and in that layout are 2 widgets, a label and a pushbutton

    I want to make the frame only as big as it needs to be and the label and pushbutton have to be next to each other as close as possible, NOTE: The text can be as big as the user tells it to.

    This my code:

    class TextLabel(QFrame):
    
        def __init__(self, text):
            super().__init__()
    
            self.layout = QHBoxLayout()
            self.text = text
            self.label = QLabel(str(self.text))
            self.removeButton = QPushButton("❌")
    
            self.removeButton.setFixedWidth(50)
            self.removeButton.setFixedHeight(50)
    
            self.layout.addWidget(self.label)
            self.layout.addWidget(self.removeButton)
    
            self.setMinimumHeight(300)
    
            self.setMinimumWidth(300)
    
            self.setStyleSheet("""
            
            QFrame {
            background-color: #f7797d;
            }
            
            QPushButton {
            background-color: transparent;
            }
            
            """)
    
            self.setLayout(self.layout)
    

    And the Frame it produces looks like this:

    0_1556473664143_cb09015e-def9-454e-a065-a4d1635dfcfe-Screenshot 2019-04-28 at 20.47.19 1.png Screenshot 2019-04-28 at 20.47.19 1

    As you can see, it's unnecessarily big. NOTE: The widget is placed inside a QScrollArea that has a QVBoxLayout in it


  • Lifetime Qt Champion

    Hi
    Well you can set a MaximumSize on it so it does not use all space available.

    To make Hello and X closer to each other, you can use a Spacer.
    https://doc.qt.io/qt-5/qspaceritem.html

    alt text


Log in to reply