尝试在运行时从单独的文件动态添加 PyQt 中的小部件实例

问题描述 投票:0回答:0

我正在尝试使用 PyQt 制作一个程序,当按下按钮时,通过从单独的小部件文件添加它来动态添加小部件的新实例。为此,我有一个 QVBoxLayout 并尝试在其上使用 insertWidget(-1, [Widget From File]) 并希望将具有该小部件配置的 .ui 文件加载到新实例中以将其添加到列表中。这就是我现在尝试做的方式:

class myWidget(QtWidgets.QWidget):
    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        uic.loadUi('Widget1.ui', self)

我设法运行代码没有错误,但是当我单击新按钮将其添加到其中时,它创建了一个看起来是空的小部件,其中没有任何内容。我可能没有用 python 正确创建新实例(是的,我是个笨蛋)但我不知道为什么它至少没有给我更多关于它为什么不起作用的信息。这是添加按钮调用的函数:

def newWidget(parent):
        parent.[QVBoxLayout].insertWidget(-1, [myWidget]())
python qt pyqt5 qt-designer
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.