Fill QGridLayout by placing widgets in exact positions without dynamic padding (Python)

100 Views Asked by At

I have QGridLayout inside a scrollArea. I want to dynamically fill it with widgets one by one triggered by an action in GUI.

I want to place widgets in 4 columns starting from left top corner.

So when the first widget is added - it is placed in the top left corner as intended. But when the second/third widget are added, they are placed in the same row with first widget but with odd spacing

enter image description here

I want to fill the grid one by one so that every next widget would be placed in the intended position inside the grid next to the previous widget without this dynamic padding.

My function for adding widgets is the follwing:

def createNewModelFrame(self, row_number, column_number):

    self.frame = QFrame(self.ui.scrollAreaWidgetContents)
    self.frame.setObjectName(u"frame")
    self.frame.setMaximumSize(QSize(200, 120))
    self.frame.setMinimumSize(QSize(200, 120))
    self.frame.setFrameShape(QFrame.StyledPanel)
    self.frame.setFrameShadow(QFrame.Raised)
    
    self.ui.gridLayout.addWidget(self.frame, row_number, column_number, 1, 1, Qt.AlignLeft|Qt.AlignTop)

This is the my grid with intended placeholders for the future widgets:





...

I need to be able to place widgets one after another without braking this grid:

 X X _ _ 
 _ _ _ _ 
 _ _ _ _ 


 X X X _ 
 _ _ _ _ 
 _ _ _ _ 


 X X X X 
 X _ _ _ 
 _ _ _ _ 

etc.

0

There are 0 best solutions below