How to set margin of content of QScrollArea
-
Hi,
I am struggling to set the margin of the widgets inside a QScrollArea.
This is what I tried:class NodeWidget(QWidget): def __init__(self): super(NodeWidget, self).__init__() # UI main_layout = QGridLayout() name_label = QLabel('label 1') name_label.setFont(QFont('Poppins', 12)) package_name_label = QLabel('label 1') package_name_label.setFont(QFont('Poppins', 12)) main_layout.addWidget(name_label, 0, 0) main_layout.addWidget(package_name_label, 1, 0) self.setLayout(main_layout) main_layout.setVerticalSpacing(0) main_layout.setSpacing(0) self.setContentsMargins(-6, -6, -6, -6) main_layout.setContentsMargins(-6, -6, -6, -6) self.setStyleSheet('margin: 0px;') # doen't work class MyWidget(QScrollArea): def __init__(self): super(MyWidget, self).__init__() self.setWidgetResizable(True) self.list_scroll_area_widget = QWidget() self.setWidget(self.list_scroll_area_widget) self.list_layout = QVBoxLayout() self.list_layout.setAlignment(Qt.AlignTop) self.list_scroll_area_widget.setLayout(self.list_layout) nw1 = NodeWidget() self.list_layout.addWidget(nw1) nw2 = NodeWidget() self.list_layout.addWidget(nw2) self.setStyleSheet('background-color: #5555aa; border: 1px solid grey;') self.setContentsMargins(0, 0, 0, 0) # doesn't work either
Result:
I actually want the space between the label boxes and the border of the scroll area to be smaller.
How can i do this?
Thanks for answers! -
@Niagarer said in How to set margin of content of QScrollArea:
self.setContentsMargins(0, 0, 0, 0) # doesn't work either
Hi
It has to be on the inner most layout that holds the widgets. -
@mrjj Hi,
thank you, that was an improvement.
I now callself.list_layout.setContentsMargins(0, 0, 0, 0)
too.
But still there is some space I don't get rid of.
Setting spacing of the list_layout to 0 also has no effect on the space between the two boxes... -
@Niagarer
hi
I think we still have some ContentsMargins on one of the layouts
It does look like the default size. on left / right side.The space between the two boxes might be normal as it just how the layout divide the space
even if spacing is set to zero , it will do like this unless you insert a spacer to press them upwards. -
-
Thank you, you are right, I made a mistake:
I applied negative content margins on a layout which seems to result in positive minus border:
left:main_layout.setContentsMargins(6, 6, 6, 6)
right:main_layout.setContentsMargins(-6, -6, -6, -6)
I actually could have seen this in @mrjj's post... my badSo, all I needed to do is to call
layout.setSpacing(0) layout.setContentMargins(0, 0, 0, 0)
on every layout in the extreme case.
I still haven't found a way to do all this through a stylesheet for the scrollarea though but at least this is one way of doing it.