pyside显示/隐藏布局 [英] pyside show / hide layouts

查看:698
本文介绍了pyside显示/隐藏布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据是否选中复选框来显示两种布局之一.

I'm trying to display one of two layouts depending on whether a checkbox is checked or not.

仅使用窗口小部件,我可以执行以下工作: (此示例中的每个小部件都是QLineEdit)

Only using widgets I can do the following which works fine: (each widget in this example is a QLineEdit)

myCheckbox.stateChanged.connect(switchControls)

def switchControls (self, state):
    if state == 2:
        self.widget1.show()
        self.widget2.hide()
    else:
        self.widget1.hide()
        self.widget2.show()

但是,由于我想为每个QLineEdit添加一个描述性标签,因此我需要在某种布局或容器中结合使用QLineEdit + QLabel. 我一直在尝试使用addlayout/removeLayout/removeItem在布局而不是小部件上执行上述操作,但是无法使其正常工作.该程序在我最后一次尝试时崩溃了.

However, since I want to add a descriptive label to each QLineEdit, I need to combine a QLineEdit+QLabel in a layout or container of some kind. I have been trying the addlayout / removeLayout / removeItem to do the above on layouts instead of widgets, but can't get it to work. The program crashed on my last try.

在两种布局之间切换的最简单方法是什么?它不必使用复选框,但我更喜欢.

What is the easiest way to switch between two layouts? It doesn't have to use a checkbox but I'd prefer that.

推荐答案

将布局放入单独的小部件中.现在,您仅使用小部件".

Put the layouts into separate widgets. Now you're "only using widgets".

这是一个例子:

from PySide.QtCore import *
from PySide.QtGui import *

class MainWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        self.layout=QVBoxLayout()
        self.setLayout(self.layout)


        self.checkbox=QCheckBox("Layouts")
        self.layout.addWidget(self.checkbox)


        self.widget1=QWidget()
        self.layout.addWidget(self.widget1)

        self.layout1=QVBoxLayout()
        self.widget1.setLayout(self.layout1)

        self.layout1.addWidget(QLabel("First layout"))

        self.layout1.addWidget(QTextEdit())


        self.widget2=QWidget()
        self.layout.addWidget(self.widget2)

        self.layout2=QHBoxLayout()
        self.widget2.setLayout(self.layout2)

        self.layout2.addWidget(QTextEdit("Second layout"))

        self.layout2.addWidget(QTextEdit())


        self.checkbox.toggled.connect(self.checkbox_toggled)
        self.checkbox.toggle()

        self.show()

    def checkbox_toggled(self, state):
        self.widget1.setVisible(state)
        self.widget2.setVisible(not state)

app=QApplication([])
mw=MainWindow()
app.exec_()

运行它以查看其工作原理.

Run it to see how it works.

这篇关于pyside显示/隐藏布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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