如何使用 .ui 文件创建小部件? [英] How to create a Widget using .ui file?

查看:60
本文介绍了如何使用 .ui 文件创建小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 MainWindow 中创建一个小部件,该小部件从使用 QtDesigner 创建的文件中获取其设计.

I'd like to create a widget inside a MainWindow that gets its design from a file created with QtDesigner.

  • 我创建的 .ui 文件是 test.ui
  • 我有一个继承自 QMainWindow 的 MainWindow 实例,它将创建一个 堆叠的小部件,其中包含许多小部件.在本例中,为简单起见,它将仅创建一个小部件作为中央小部件.
  • MainWindow 中的 My Widget 实例是必须从 .ui 文件中获取其设计的实例
  • the .ui file I created is test.ui
  • I have a MainWindow instance inheriting from QMainWindow that will create a stacked widget with many widgets inside it. For simplicity in this example, it will create just a widget as central widget.
  • The My Widget instance inside the MainWindow is the one that has to grab its design from the .ui file

实际上,应用程序显示一个空白窗口

As it is, the application shows a blank window

代码如下:

from PySide2.QtWidgets import QMainWindow, QApplication, QDesktopWidget, QWidget
from PySide2.QtCore import QCoreApplication, Qt

from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import QFile

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

    ui_file = QFile("test.ui")
    ui_file.open(QFile.ReadOnly)

    loader = QUiLoader()
    window = loader.load(ui_file)
    ui_file.close()


class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui_dim_percentage = 70/100
        self.initUI()

    def initUI(self):
        self.center()

        self.home_widget = MyWidget(self)
        self.setCentralWidget(self.home_widget)

        self.show()

    def center(self): # resizes the UI to a percentage of the screen and centers the widget

        screen_size = QDesktopWidget().screenGeometry()
        self.resize(screen_size.width()*self.ui_dim_percentage, screen_size.height()*self.ui_dim_percentage)

        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


def main():

    app = QApplication(sys.argv)
    ex = MainWindow()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

我应该如何更正代码?

推荐答案

问题是你混淆了概念,当你使用 QUiLoader 时你正在创建窗口,在这种情况下是 window 变量那是一个局部变量,也就是.ui不填MyWidget.

The problem is that you are confusing concepts, when you use QUiLoader you are creating the window that in this case is the window variable that is a local variable, that is, the .ui does not fill in MyWidget.

解决办法不是创建MyWidget类,直接使用window变量即可.

The solution is not to create the MyWidget class, just use the window variable.

from PySide2.QtWidgets import QMainWindow, QApplication, QDesktopWidget
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader


def create_widget():
    ui_file = QFile("test.ui")
    if ui_file.open(QFile.ReadOnly):
        loader = QUiLoader()
        window = loader.load(ui_file)
        ui_file.close()
        return window


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui_dim_percentage = 70 / 100
        self.initUI()

    def initUI(self):
        self.center()

        self.home_widget = create_widget()
        self.setCentralWidget(self.home_widget)

        self.show()

    # resizes the UI to a percentage of the screen and centers the widget
    def center(self):

        screen_size = QDesktopWidget().screenGeometry()
        self.resize(
            screen_size.width() * self.ui_dim_percentage,
            screen_size.height() * self.ui_dim_percentage,
        )

        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


def main():
    import sys

    app = QApplication(sys.argv)
    ex = MainWindow()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

这篇关于如何使用 .ui 文件创建小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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