【问题标题】:Inserting layout into QDialog when signal is emitted from combobox从组合框发出信号时将布局插入QDialog
【发布时间】:2016-07-11 22:23:24
【问题描述】:

我在 qtdesigner 中创建了一个如下所示的 ui:

并且我想在标题为“方程式”的组框和包含标题为“下标”和“连接元素”的两个组框的布局之间插入另一个包含几个小部件的布局。

我不确定如何插入此附加布局的原因是,当我查看 qtdesigner 中的对象检查器时,我看到了:

它不会告诉我包含对话框窗口中所有其他小部件和布局的垂直布局的名称。

我正在加载用户界面如下:

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4 import uic

class EquationEditor(QDialog):

    def __init__(self, parent=None):
        super().__init__(parent)

        uic.loadUi('equation_editor.ui', self)

        # Insert a layout containing a couple of widgets on index change of
        # the combobox
        self.typeBox.currentIndexChanged.connect(self.enableInitialValueEntry)

    def enableInitialValueEntry(self):
        vartype = self.typeBox.currentText()

        if vartype == "Stock":
            hbox = QHBoxLayout()
            hbox.addStretch(1)

            hbox.addWidget(QLabel("Initial Value"))
            hbox.addWidget(QLineEdit())

            #How can I insert the layout `hbox`?

【问题讨论】:

    标签: python qt pyqt qdialog qlayout


    【解决方案1】:

    包含所有其他布局的布局将是对话框的布局,因此您可以尝试以下操作:

        def enableInitialValueEntry(self):
            ...
            if vartype == "Stock":
                ...
                main_layout = self.layout()
                main_layout.insertLayout(2, hbox)
    

    【讨论】:

    • 哦,好吧,太好了!我在 QDialog 文档中搜索了布局,但没有找到任何东西。稍后会试一试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    相关资源
    最近更新 更多