Issue
I have two widgets in QFrame class. First widget is a QTabWidget and the next one is a button. I was trying to set the top margin for the button. I have tried to update it by setting margin-top in negative value for the Qpushbutton, it didn't work. I was to trying to align the button on top, at the same height of the tab bar
class TableTreeItemChild(QFrame):
"""Component for showing child widget of tree item"""
def __init__(self, dbData, urlDBIndex):
super(TableTreeItemChild, self).__init__()
self.urlDBIndex = urlDBIndex
self.tableTreeTab = TableTreeTab(dbData)
self.setupUI()
def setupUI(self):
"""Setting up the component"""
mainLayout = QHBoxLayout()
mainLayout.setContentsMargins(0, 0, 0, 0)
mainLayout.addWidget(self.tableTreeTab)
exportButton = QPushButton("")
exportButton.setIcon(QIcon("assets/icons/export_icon_16.png"))
exportButton.setStyleSheet("QPushButton{border: 1px solid #e1e1e1; padding:5px; border-radius:4px; } ")
exportButton.setGeometry(0, -100, 10, 10)
exportButton.clicked.connect(self.onExportButtonClick)
mainLayout.addWidget(exportButton)
self.setFixedWidth(800)
self.setLayout(mainLayout)
The current output of the above code is:
Solution
In order to align a widget on top within a layout, the alignment
argument must be provided.
Remember that addWidget()
has three arguments (the mandatory widget, the stretch and the alignment), so if you do not specify the stretch the alignment has to be keyworded:
mainLayout.addWidget(exportButton, alignment=QtCore.Qt.AlignTop)
Three considerations:
- setting the geometry on a widget that is added to a layout is useless, as the layout will automatically change the widget's geometry according to the situation;
- stylesheets in Qt are not as advanced as "web" CSS: they only apply to the widgets properties, and they are not (nor they should) intended to allow control over positioning, resizing or alignment;
- while the alignment will put your widget on top, there is no way to guarantee that it will be exactly aligned with the other widgets, and that's due to the way Qt draws elements: there can be some workarounds, but they are not 100% safe in all situations; as explained above, Qt stylesheets only override the appearance of individual widgets, there's no laying out involved as with normal CSS, since layout managers (QLayout subclasses) are responsible of that and they do not support stylesheets;
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.