How to add a widget to gridlayout, starting from the upper left corner

I created a user interface in Qt Designer and converted it to python:

enter image description here

In an empty area, I added scrollarea and gridlayout to align my buttons.

Each time I click the CreatePose button, I need to add an icon button to the gridlayout, starting from the upper left corner. Now he is adding it to the center of gridlayout.

self.ui.PoseBtn_GridLayout.setColumnMinimumWidth(4,4) self.button = QtGui.QPushButton('') self.button.setIcon(self._icon) self.button.setIconSize(QtCore.QSize(128, 128)) self.button.setMinimumSize(QtCore.QSize(128, 128)) self.button.setMaximumSize(QtCore.QSize(128, 128)) self.ui.PoseBtn_GridLayout.addWidget(self.button) 

How to fix this problem?

+4
source share
1 answer

See if you replace the code you posted with this:

 self.ui.PoseBtn_GridLayout.setColumnMinimumWidth(4,4) spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) self.ui.PoseBtn_GridLayout.addItem(spacerItem, 1, 1, 1, 1) spacerItem1 = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding) self.ui.PoseBtn_GridLayout.addItem(spacerItem1, 1, 0, 1, 1) self.button = QtGui.QPushButton('') self.button.setIcon(self._icon) self.button.setIconSize(QtCore.QSize(128, 128)) self.button.setMinimumSize(QtCore.QSize(128, 128)) self.button.setMaximumSize(QtCore.QSize(128, 128)) self.ui.PoseBtn_GridLayout.addWidget(self.button, 0, 0, 1, 1) 

EDIT

Here is how you should display your widgets to get the expected results, check the object inspector, basically you need a QWidget with a grid layout inside another QWidget , as well as a grid layout and 2 QSpaceItem :

Qt Designer Screenshot

0
source

All Articles