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. QLabel background-color expanding too much
Forum Updated to NodeBB v4.3 + New Features

QLabel background-color expanding too much

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.7k Views 2 Watching
  • 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.
  • P Offline
    P Offline
    Pival81
    wrote on last edited by
    #1

    Hi, I've recently started using Qt5 with python, and I'm liking it a lot.
    But today I found myself stuck at this problem:
    When I set the background color for a QLabel inside a QVBoxLayout, the QLabel takes the whole width of the layout.
    I've tried setting the padding and the margin to 0, without luck.

    Here is my full code:

    #!/bin/env python
    
    import sys
    from PyQt5.QtWidgets import *
    
    class App(QMainWindow):
    	
    	def __init__(self):
    		super().__init__()
    		
    		self.initUI()
    		
    		
    	def initUI(self):
    
    		self.setGeometry(300,300,350,100)
    		self.setFixedSize(350, 70)
    		self.setWindowTitle("Paladins_Test")
    
    		self.hbox = QHBoxLayout()
    		self.vbox = QVBoxLayout()
    		self.vbox.addLayout(self.hbox)
    
    		window = QWidget()
    		window.setObjectName("mainwidget")
    		window.setLayout(self.vbox)
    		self.setCentralWidget(window)
    
    		self.textbox = QLineEdit(self)
    		self.button = QPushButton("Submit", self)
    
    		self.button.clicked.connect(self.showTabs)
    		self.textbox.returnPressed.connect(self.button.click)
    
    		self.hbox.addWidget(self.textbox)
    		self.hbox.addWidget(self.button)
    
    	def showTabs(self):
    		self.setFixedSize(350, 500)
    		player = self.textbox.text()
    		tabwidget = QTabWidget(self)
    
    		self.vbox.addWidget(tabwidget)
    
    		tab1 = QWidget(self)
    		tab1.layout = QVBoxLayout()
    
    		tab1.setLayout(tab1.layout)
    		tabwidget.addTab(tab1, "Player Info")
    
    		label1 = QLabel(self)
    		label1.setText(player)
    		label1.setObjectName("playername")
    
    		label2 = QLabel(self)
    		label2.setText("Level:")
    
    		label3 = QLabel(self)
    		label3.setText("Wins:" )
    
    		label4 = QLabel(self)
    		label4.setText("Losses:" )
    
    		label5 = QLabel(self)
    		label5.setText("Region:")
    
    		tab1.layout.addWidget(label1)
    		tab1.layout.addWidget(label2)
    		tab1.layout.addWidget(label3)
    		tab1.layout.addWidget(label4)
    		tab1.layout.addWidget(label5)
    		tab1.layout.addStretch(1)
    
    
    if __name__ == '__main__':
        
    	app = QApplication(sys.argv)
    	gui = App()
    
    	with open('style.qss', 'r') as myfile:
    		qss = myfile.read()
    
    	app.setStyleSheet(qss)
    	gui.show()
    	sys.exit(app.exec_())
    

    And here's the qss stylesheet:

    QLabel {
        font-size: 20px;
        
    }
    
    #mainwidget {
        background-color: lightblue;
    }
    
    #playername {
        font-size: 30px;
        margin-top: 15px;
        background-color: red;
        border: 1px solid darkred;
        border-radius: 4px;
        qproperty-alignment: AlignHCenter;
    }
    

    Here's a pic:
    alt text

    As you can see, the QLabel's background is too widened.
    How can I make it fit the text?

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

      Hi and welcome to devnet,

      From the top of my head, put the QLabel in a QHBoxLayout surrounded by QSpacerItems.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • P Offline
        P Offline
        Pival81
        wrote on last edited by
        #3

        I tried it, and it works, but isn't there any better way to do it? I mean, I don't think it should have done that in the first place.

        mrjjM 1 Reply Last reply
        0
        • P Pival81

          I tried it, and it works, but isn't there any better way to do it? I mean, I don't think it should have done that in the first place.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Pival81

          Hi
          All your labels are that size. You just dont see it as they have transparent backgrounds.
          Did you try setting MaximumWidth on the pival81 label ?

          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