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_())
Author by
finefoot
Updated on June 05, 2022Comments
-
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:
Screenshot of resulting window for MCVE snippet:
What I want the window to look like:
-
eyllanesc almost 7 yearsYou could explain better or show an image of what you want.
-
eyllanesc almost 7 yearsI do not notice the difference, you could point with an arrow or something similar please.
-
G.M. almost 7 years
-
-
ekhumoro almost 7 yearsThis 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. 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 :-)