There is a fixed-width QLabel with predefined styles.

self.name.setStyleSheet("QLabel#name{background:#fff;border:0px;padding: 0 10px 0 10px}") 

In another place I need for some manipulations to find out the dimensions of padding, which was specified through styles. Is there any method for this?

  1. Yes, as a β€œdeveloper” I know the size and I can manually register where necessary, but I would like not to produce magic numbers, yes, you can add a comment, but:

  2. If in styles padding sizes change and FORGET to change the same value in another place, everything will start to appear crookedly and you will have to look for a place where the size of padding is registered manually, and not taken from the method.

I hope this method exists, if so, kindly suggest it.

The same question about the border specified with the styles.

UPD: There is a method that does the "same thing":

 self.name.setContentsMargins(10, 0, 10, 0) 

BUT:

  1. With the border is still unclear.
  2. It has nothing to do with Padding in styles, i.e. a crutch can be replaced with these padding in styles
  3. It works exactly padding - it adds padding INSIDE the widget without changing its size, and not like MARGIN, which should add padding "on top" of the widget (judging by CSS). Qt bug or I did not understand the description of the method correctly (the English is rather weak)?

    1 answer 1

    Try spinning SpinBoxs in my example:

     import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class MyWidget(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self._border = 0 self._paddingTop = 0 self._paddingRight = 0 self._paddingBottom = 0 self._paddingLeft = 0 self.label_1 = QLabel("Π­Ρ‚ΠΎ тСкст") self.label_2 = QLabel("QLabel с фиксированной ΡˆΠΈΡ€ΠΈΠ½ΠΎΠΉ, с Π·Π°Π΄Π°Π½Π½Ρ‹ΠΌΠΈ стилями", objectName='name') self.label_2.setFixedWidth(320) self.label_2.setStyleSheet(""" QLabel#name{ background: #fff; border: %dpx solid #32414B; color: blue; padding: %dpx %dpx %dpx %dpx;} """ % (self._border, self._paddingTop, self._paddingRight, self._paddingBottom, self._paddingLeft)) self.gridLayout = QGridLayout() self.gridLayout.addWidget(self.label_1, 0, 0) self.gridLayout.addWidget(self.label_2, 1, 0) self.gridLayout.setRowStretch(2, 1) self.formLayout = QFormLayout() for item in ("border", "paddingTop", "paddingRight", "paddingBottom", "paddingLeft"): spinBox = QSpinBox() spinBox.setMinimum(0) spinBox.setMaximum(50) spinBox.setValue(0) spinBox.valueChanged.connect(lambda i, p=item: self.valueChangedSpin(i, p)) self.formLayout.addRow(QLabel(item), spinBox) self.gridLayout.addLayout(self.formLayout, 3, 0) self.setLayout(self.gridLayout) def valueChangedSpin(self, i, p): if p=="paddingTop": self._paddingTop = i elif p=="paddingRight": self._paddingRight = i elif p=="paddingBottom": self._paddingBottom = i elif p=="paddingLeft": self._paddingLeft = i elif p=="border": self._border = i self.label_2.setStyleSheet(""" QLabel#name{ background: #fff; border: %dpx solid #32414B; color: blue; padding: %dpx %dpx %dpx %dpx;} """ % (self._border, self._paddingTop, self._paddingRight, self._paddingBottom, self._paddingLeft)) if __name__=="__main__": app = QApplication(sys.argv) widget = MyWidget() widget.show() sys.exit(app.exec_()) 

    enter image description here


    Option two:

     import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class MyWidget(QWidget): def __init__(self, dP, parent=None): QWidget.__init__(self, parent) self._border = dP["border"] self._paddingTop = dP["paddingTop"] self._paddingRight = dP["paddingRight"] self._paddingBottom = dP["paddingBottom"] self._paddingLeft = dP["paddingLeft"] self.label_1 = QLabel("QLabel с фиксированной ΡˆΠΈΡ€ΠΈΠ½ΠΎΠΉ, с Π·Π°Π΄Π°Π½Π½Ρ‹ΠΌΠΈ стилями") self.label_2 = QLabel("QLabel с фиксированной ΡˆΠΈΡ€ΠΈΠ½ΠΎΠΉ, с Π·Π°Π΄Π°Π½Π½Ρ‹ΠΌΠΈ стилями", objectName='name') # self.label_2.setFixedWidth(320) self.label_2.resize(320, 50) self.label_3 = QLabel() self.label_3.setText(""" "label.width" ={}, "width" ={}, "border" ={}, "paddingTop" ={}, "paddingRight" ={}, "paddingBottom"={}, "paddingLeft" ={} """.format(self.label_2.width(), self.label_2.width() - self._paddingRight - self._paddingLeft, self._border, self._paddingTop, self._paddingRight, self._paddingBottom, self._paddingLeft )) self.gridLayout = QGridLayout() self.gridLayout.addWidget(self.label_1, 0, 0) self.gridLayout.addWidget(self.label_2, 1, 0, alignment=Qt.AlignLeft) self.gridLayout.addWidget(self.label_3, 2, 0) self.gridLayout.setRowStretch(4, 1) self.formLayout = QFormLayout() for item in ("border", "paddingTop", "paddingRight", "paddingBottom", "paddingLeft"): spinBox = QSpinBox() spinBox.setFixedWidth(80) spinBox.setMinimum(0) spinBox.setMaximum(50) spinBox.setValue(0) spinBox.valueChanged.connect(lambda i, p=item: self.valueChangedSpin(i, p)) self.formLayout.addRow(QLabel(item), spinBox) self.gridLayout.addLayout(self.formLayout, 3, 0) #, alignment=Qt.AlignLeft) self.setLayout(self.gridLayout) def valueChangedSpin(self, i, p): if p=="paddingTop": self._paddingTop = i elif p=="paddingRight": self._paddingRight = i elif p=="paddingBottom": self._paddingBottom = i elif p=="paddingLeft": self._paddingLeft = i elif p=="border": self._border = i print(self.label_1.width(), self.label_2.width(), self.label_2.width() - self._paddingRight - self._paddingLeft) self.label_2.setStyleSheet(""" QLabel#name{ background: #fff; border: %dpx solid #32414B; color: blue; padding: %dpx %dpx %dpx %dpx;} """ % (self._border, self._paddingTop, self._paddingRight, self._paddingBottom, self._paddingLeft)) self.label_3.setText(""" "label.width" ={}, "width" ={}, "border" ={}, "paddingTop" ={}, "paddingRight" ={}, "paddingBottom"={}, "paddingLeft" ={} """.format(self.label_2.width(), self.label_2.width() - self._paddingRight - self._paddingLeft, self._border, self._paddingTop, self._paddingRight, self._paddingBottom, self._paddingLeft )) # ---------------------------------------------------------------------- dP = {"border": 0, "paddingTop": 0, "paddingRight": 0, # dP - dictPadding "paddingBottom": 0, "paddingLeft": 0} CSS = """ QLabel#name{ background: #fff; border: %dpx solid #32414B; color: blue; padding: %dpx %dpx %dpx %dpx; } """ % (dP["border"], dP["paddingTop"], dP["paddingRight"], dP["paddingBottom"], dP["paddingLeft"]) if __name__=="__main__": app = QApplication(sys.argv) app.setStyleSheet(CSS) widget = MyWidget(dP) widget.show() sys.exit(app.exec_()) 
    • You do not quite understand me. I need another. I have a method that cuts long text (not fit in QLabel), for this you need to know the β€œworking” width QLabel width = label.width() - padding_width * 2 (2 - because padding is left and right), and here is my The question is how to recognize this padding_width which was β€œonce in a while” padding_width for a label ? - froxxendsg 10:21 pm
    • I don't need to re-change the QLabel styles. - froxxendsg
    • I do not propose to you to re-change the styles QLabel. I showed you the option that you can control the size through the values ​​of variables. Namely: padding_width = 10 . . . self.name.setStyleSheet("QLabel#name{background:#fff;border:0px;padding: 0 %dpx 0 %dpx}" % (padding_width, padding_width)) . . . Here is your width: width = label.width() - padding_width * 2 - S. Nick
    • I know this, thank you, I forgot to write about this option in the question: I want to put all the styles in a separate QSS file and connect it to the program (I don’t know how to put it more correctly), and in this case the trick with the variable will not work, alas. As I understand it, all the same, you cannot pull out the values ​​of the properties of styles? Alas, it seems to have to make an exception and for this part of the program to prescribe styles on the spot, and not in a separate file. - froxxendsg
    • All styles put in a separate QSS file - this is correct. With variables, too, everything is in order. See the second answer. Watch what happens. Experiment. - S. Nick