在布局中重新排序小部件

时间:2011-09-07 16:23:23

标签: layout widget pyqt

我有一个senario,我有QComboBoxes(黑色矩形)和QPushButtons(红色立方体)。我需要两个按钮始终保持在最正确的组合的两侧。

Valid XHTML

代码示例:

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 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

如果您有任何想法,提示或解决方案,请与我们联系。

1 个答案:

答案 0 :(得分:1)

我认为嵌套布局会对您有所帮助。不要仅使用其他按钮将组合框添加到主布局,而是将组合框添加到自己的布局中,并将该布局添加到主布局中。从内部布局添加和删除组合框应该更容易,而将其他所有内容留在原处。