Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to make a QFrame smaller?
Forum Update on Monday, May 27th 2025

How to make a QFrame smaller?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 335 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fuchsiaff
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved