代码之家  ›  专栏  ›  技术社区  ›  pediatrictactic

在python中没有所有锅炉板的情况下从QUiLoader装载是否安全?

  •  1
  • pediatrictactic  · 技术社区  · 7 年前

    所有教程都以如下方式加载ui(在构造函数中):

    from PySide2.QtUiTools import QUiLoader
    ...
    # in the constructor
        loader = QUiLoader()
        file = QFile(self.__resource('the_ui_file.ui'))
        file.open(QFile.ReadOnly)
        self.ui = loader.load(file, None)
        file.close()
    

    但是,这同样有效:

    self.ui = QUiLoader().load('the_ui_file.ui')
    

    考虑到python的垃圾收集,较短的版本是否可以安全使用?这种方法有什么缺点吗?

    非常感谢。

    1 回复  |  直到 7 年前
        1
  •  0
  •   eyllanesc    7 年前

    实际上没有问题,因为GC将在加载小部件后完成其作用域后删除它。

    唯一的缺点是您将只加载一个小部件,但是如果您有一个QUiLoader实例,您可以从其他.ui加载多个小部件。也许你经常看到它,因为它在里面 the docs of PySide2 docs of Qt 它没有类似于短版本的版本。