How to make border of QLabel look like border of other widgets like QTreeWidget?

12,023

You may have found the answer to this already but just in case...

The following should provide you with what you want...

label = QLabel(self)
label.setFrameShape(QFrame.Panel)
label.setFrameShadow(QFrame.Sunken)
label.setLineWidth(3)

Where I've just hardwired the line width. Note that the "border: 1px" specifier must be removed from the style sheet otherwise it will be used in preference to the specified line width. The complete code would be...

import sys
from PyQt5.QtWidgets import *

class widget(QWidget):
    def __init__(self):
        super().__init__()
        treewidget = QTreeWidget(self)
        label = QLabel(self)
        label.setStyleSheet("background-color: white; inset grey; min-height: 200px;")
        label.setFrameShape(QFrame.Panel)
        label.setFrameShadow(QFrame.Sunken)
        label.setLineWidth(3)
        grid = QGridLayout()
        grid.setSpacing(10)
        grid.addWidget(treewidget, 1, 0)
        grid.addWidget(label, 2, 0)
        self.setLayout(grid)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    f = widget()
    sys.exit(app.exec_())
Share:
12,023
finefoot
Author by

finefoot

Updated on June 05, 2022

Comments

  • finefoot
    finefoot almost 2 years

    I noticed different border styles for QTreeWidget and QLabel - even if I try to adjust the stylesheet. Of course, I could change the stylesheet for both, but ideally I'd like to keep the QTreeWidget's border style. How can I make the border of QLabel look like the border of QTreeWidget?


    MCVE snippet:

    import sys
    from PyQt5.QtWidgets import *
    
    class widget(QWidget):
        def __init__(self):
            super().__init__()
            treewidget = QTreeWidget(self)
            label = QLabel(self)
            label.setStyleSheet("background-color: white; border: 1px inset grey; min-height: 200px;")
            grid = QGridLayout()
            grid.setSpacing(10)
            grid.addWidget(treewidget, 1, 0)
            grid.addWidget(label, 2, 0)
            self.setLayout(grid)
            self.show()
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        f = widget()
        sys.exit(app.exec_())
    

    Without stylesheet:

    window default


    Screenshot of resulting window for MCVE snippet:

    window as is


    What I want the window to look like:

    window as desired

    • eyllanesc
      eyllanesc almost 7 years
      You could explain better or show an image of what you want.
    • eyllanesc
      eyllanesc almost 7 years
      I do not notice the difference, you could point with an arrow or something similar please.
    • G.M.
      G.M. almost 7 years
      I think the QFrame properties are what you're looking for. Specifically frameShape and frameShadow.
  • ekhumoro
    ekhumoro almost 7 years
    This won't work for all styles. The borders of the tree-widget and label may not look the same, even if exactly the same frame settings are applied to both. Some styles just draw the borders differently for certain widgets. This seems to be the case for the Plastique style and the Windows style (which the OP appears to be using). By contrast, the default Fusion style draws them the same, as do the Cleanlooks and GTK2 styles. (I tested this using the preview mode in Qt Designer).
  • G.M.
    G.M. almost 7 years
    @ekhumoro What you say is true but... it's also in the nature of styles as implemented in Qt -- some features/changes will work with certain styles but not with others. Personally, I generally leave the style stuff alone -- the user should choose a style and it's not up to me to interfere with it :-)