pyqt5 addStretch在小部件之间? [英] Pyqt5 addStretch in between widgets?

查看:336
本文介绍了pyqt5 addStretch在小部件之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用QVBox布局,并且该布局中有两个小部件和一个动态布局"layout2".窗口小部件1固定在顶部窗口小部件3固定在底部,窗口小部件2是动态窗口小部件.每次删除和添加layout2.这里的问题是,由于layout2布局被删除,我无法将widget3定位在底部,而Widget3则移至顶部.下面是示例代码.

I am using a QVBox layout and there are two widgets and a dynamic layout 'layout2' in the layout. Widget1 is fixed on top Widget3 is fixed at the bottom and widget2 is dynamic widget. layout2 is deleted and added each time. The problem here is I am not able to position the widget3 at the bottom as layout2 layout is deleted Widget3 moves to the top. Below is the sample code.

class Screen(QWidget):
    def __init__(self):            
        super(Screen, self).__init__()

        self.main_layout = QVBoxLayout()
        widget1 = QPushButton("Text1")
        #self.widget2 = QWidget()
        widget3 = QLabel("Text3")
        self.widget2_layout = QHBoxLayout()
        widget2_label = QLabel("text2")
        self.widget2_layout.addWidget(widget2_label)
        #self.widget2.setLayout(self.widget2_layout)
        self.main_layout.addWidget(widget1,Qt.AlignTop)
        self.main_layout.addLayout(self.widget2_layout)
        self.main_layout.addWidget(widget3,Qt.AlignBottom)
        widget1.clicked.connect(self.change_widget2)    
        self.setLayout(self.main_layout)    
        self.show()

    def clearLayout(self,layout):
        item = layout.takeAt(0)
        while item:
            w = item.widget()
            if w:
                w.deleteLater()
            lay = item.layout()
            if lay:
                self.clearLayout(item.layout())
            item = layout.takeAt(0)
    def change_widget2(self):
        self.clearLayout(self.widget2_layout)
        self.widget2_layout = QHBoxLayout()
        widget2_label = QLabel("text changed")
        self.widget2_layout.addWidget(widget2_label)
        self.main_layout.addLayout(self.widget2_layout)

app = QApplication(sys.argv)
Gui = Screen()
sys.exit(app.exec_())

我尝试了addstretch,虚拟的附加布局,但没有任何效果.

I have tried addstretch, dummy additional layout and nothing worked.

推荐答案

如果您只想更改位于第二位置的小部件,则无需删除创建新布局,只需重用它,在以下示例中,我们看到了小部件的变化方式:

If you only want to change the widget that is in the second position it is not necessary to delete create a new layout, it is only necessary to reuse it, in the following example we see how the widget is changing:

class Screen(QWidget):
    def __init__(self):
        super(Screen, self).__init__()
        self.setLayout(QVBoxLayout())

        widget1 = QPushButton("Text1", self)
        widget3 = QLabel("Text3", self)

        self.widget2_layout = QHBoxLayout()
        self.change_widget2()

        self.layout().addWidget(widget1)
        self.layout().addLayout(self.widget2_layout)
        self.layout().addWidget(widget3)

        widget1.clicked.connect(self.change_widget2)

    def clearLayout(self, layout):
        item = layout.takeAt(0)
        while item:
            w = item.widget()
            if w:
                w.deleteLater()
            lay = item.layout()
            if lay:
                self.clearLayout(item.layout())
            item = layout.takeAt(0)

    def change_widget2(self):
        self.clearLayout(self.widget2_layout)

        # change the widget.
        import random
        widgets = [QLabel, QLineEdit, QPushButton]
        widget2 = widgets[random.randint(0, len(widgets)-1)]("widget2", self)

        self.widget2_layout.addWidget(widget2)

这篇关于pyqt5 addStretch在小部件之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆