开发者

reorder widgets in layout

I have a senario where I have QComboBoxes (the black rectangles) and QPushButtons (the red cubes). I need the two buttons to always stay on either side of the most right combo.

reorder widgets in layout

.

Code example:

self.button1 QPushButton()
self.button2 = QPushButton()


def addCombo():
    # remove buttons from next to previous right most combo
    layout.removeWidget( self.button1 )
    layout.removeWidget( self.button2 )

    # add button left of new right most combo
    layout.addWidget( self.button1 )
    # add new right most combo
    layout.addWidget( QComboBox() )
    # add button right of 开发者_StackOverflow中文版new right most combo
    layout.addWidget( self.button2 )


def removeCombo():
    # remove buttons from next to previous right most combo
    layout.removeWidget( self.button1 )
    layout.removeWidget( self.button2 )

    # delete right most combo
    layout.takeAt( len(layout.children()) -1 )

    # add button left of new right most combo # button , index
    layout.insertWidget( self.button1 ,  len(layout.children()) - 2 )
    # add button right of new right most combo
    layout.addWidget( self.button2 )


#So for the first layout:
layout.addWidget( QComboBox() )
layout.addWidget( self.button1 ) 
layout.addWidget( QComboBox() )
layout.addWidget( self.button2 )


#second layout:
addCombo()


#third layout:
addCombo()


# fourth layout

Please let me know if you have any ideas, tips or solutions.


I think nested layouts will help you here. Instead of just adding your combo boxes to the main layout with the other buttons, add the combo boxes to their own layout and add that layout to the main layout. It should be easier to add and remove combo boxes from the inner layout leaving everything else where it is.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜