python - 如何在 PyQt 中从另一个 QWidget 调用一个 QWidget

标签 python pyqt qt-designer

我正在使用 PyQt 开发一个基于 QWidget 的应用程序。我将整个应用程序放在一个类中(名为“示例”)。在这个应用程序中,我有一个按钮,可以调用我在 QtDesigner 上设计的另一个 QWidget 窗口(名为“Report Widget”)并打开它。 问题是我不知道该怎么做。这是我使用的方法(来 self 在互联网上找到的东西)。 在我的主类“Example”上,一个方法定义了调用“Report Widget”的按钮:

ReportBtn = QtGui.QPushButton("Generate report")
ReportBtn.clicked.connect(self.ShowReportWidget)

第二行调用Example(主)类的ShowReportWidget方法:

def ShowReportWidget(self):
    self.f = QtGui.QWidget()
    self.ReportWidget = ReportWidget(self.f)
    self.ReportWidget.show()

此方法调用类 ReportWidget,它是来自示例(主)类的类:

class ReportWidget(QtGui.QWidget):
def __init__(self, parent = None):
    QtGui.QWidget.__init__(self)
    self.ui = rw.Ui_ReportWidget()
    self.ui.setupUi(parent)

最后,此类引用外部类 (Ui_ReportWidget),该外部类包含在另一个单独的 Python 文件(从 QtDesigner .ui 文件生成的文件)中,我在脚本开头将其导入为 rw。

问题是,使用这种方法,当我单击“报告小部件”按钮时,会弹出一个新窗口,但它是空的。 Ui_ReportWidget 类的内容未加载。

我希望我的问题足够清楚。

谢谢

最佳答案

SetupUi 将 Qwidget 作为输入,而 init() 函数应将调用 QObject 作为父级。

def ShowReportWidget(self):
    self.f = QtGui.QWidget()
    self.ReportWidget = ReportWidget(self)
    self.ReportWidget.setupUi(self.f)
    self.f.exec_()

class ReportWidget(QtGui.QWidget):
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self,parent)

    def setupUi(self,Widget):
        self.ui = rw.Ui_ReportWidget()
        self.ui.setupUi(Widget)

关于python - 如何在 PyQt 中从另一个 QWidget 调用一个 QWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22348864/

相关文章:

python - Pandas 与另一列的最大值聚合分组?

python - 即使我获得了所有标签, Selenium 也从下拉菜单中选择选项

python - 如何强制 pytest 运行每个测试(停止跳过未标记为跳过的测试)?

python - 自动聚焦显示删除 QLineEdit 的占位符文本

qt - 在 QTabWidget 选项卡中设置标签文本

python - 在 PyQt5 中安装 EventFilter

python - Zope AdvancedQuery ICatalog工具

python - 如何将记录器输出重定向到 PyQt 文本小部件

c++ - 使用 QtDesigner 创建的 QWidget 添加到 QToolBar 时不可见

python - QLineEdit 对象的 PyQt 集合