简体   繁体   English

如何将 .qml 扩展名更改为 .ui 以在 pyqt5 应用程序中使用它

[英]How to change the .qml extension to a .ui to use it in pyqt5 application

I create the design of my app with qt designer and then transform every window to a python file using the command pyuic5 example.ui -o example.py in able to get a Ui_Form class and call it in my program.我使用qt 设计器创建我的应用程序的设计,然后使用命令 pyuic5 example.ui -o example.py 将每个窗口转换为 python 文件,以便能够获取 Ui_Form 类并在我的程序中调用它。 So every thing is working but now we have changed our design and we get a qml files.所以一切正常,但现在我们改变了我们的设计,我们得到了一个 qml 文件。 My question is how to work with this qml files without changing the concepts of the app.我的问题是如何在不改变应用程序概念的情况下使用这个 qml 文件。 Is there a method like the pyuic5 (to get the Ui_Form class) to transform the qml and use it in pyqt5.有没有像pyuic5(获取Ui_Form类)这样的方法来转换qml并在pyqt5中使用它。

This is an example of the old app:这是旧应用程序的示例:

from main_screen import Ui_Form as Ui_main_screen
class MainScreen(QWidget, Ui_main_screen):
    teachButton = False
    manageButton = False
    utilitiesButton = False
    adminButton = False
    helpButton = False
    systemButton = False
    inspectionButton = False
    modelSelected = None
    def __init__(self):
        super(MainScreen, self).__init__()
        #QWidget.__init__(self)
        self.setupUi(self)
        self.trans = QTranslator(self)

        self.toLanguage()
        self.product()
        self.Menu() .....

As you can see, I imported the Ui_Form into the MainScreen class.如您所见,我将 Ui_Form 导入 MainScreen 类。 Now i want to do the same with the qml file现在我想对 qml 文件做同样的事情

import QtQuick 2.7
Item {
    width:904
    height:678
    Image {
        id: background
        source: "images/background.png"
        x: 0
        y: 1
        opacity: 1
    }
    Image {
        id: logo
        source: "images/logo.png"
        x: 691
        y: 34
        opacity: 1
    }
    Image {
        id: teach
        source: "images/teach.png"
        x: 717
        y: 154
        opacity: 1
    }
    Image {
        id: administration
        source: "images/administration.png"
        x: 711
        y: 410
        opacity: 0.49803921568627
    }
    Image {
        id: system
        source: "images/system.png"
        x: 708
        y: 468
        opacity: 0.49803921568627
    }
    Image {
        id: utilities
        source: "images/utilities.png"
        x: 711
        y: 353
        opacity: 0.49803921568627
    }
    Image {
        id: help
        source: "images/help.png"
        x: 712
        y: 524
        opacity: 0.49803921568627
    }
    Image {
        id: teachinf_wizard
        source: "images/teachinf_wizard.png"
        x: 740
        y: 196
        opacity: 1
    }
    Image {
        id: inspection
        source: "images/inspection.png"
        x: 713
        y: 295
        opacity: 0.49803921568627
    }
    Image {
        id: manage
        source: "images/manage.png"
        x: 714
        y: 239
        opacity: 1
    }
}

So how to get something like Ui_Form class with qml file那么如何使用 qml 文件获取类似 Ui_Form 类的东西

Short Answer:简答:

No it can not be done.不,这是做不到的。

Long Answer:长答案:

The .ui are just a set of instructions on how the qwidgets should be displayed, on the other hand, qml is a programming language since they indicate how the objects interact. .ui 只是关于如何显示 qwidget 的一组指令,另一方面,qml 是一种编程语言,因为它们指示对象如何交互。

The closest thing to what you want is to be able to embed the qml into a QWidget, using for example QQuickWidget:最接近您想要的是能够将 qml 嵌入到 QWidget 中,例如使用 QQuickWidget:

import os
import sys
import os
from pathlib import Path


from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQuickWidgets import QQuickWidget


CURRENT_DIRECTORY = Path(__file__).resolve().parent


def main():

    app = QApplication(sys.argv)

    widget = QQuickWidget(resizeMode=QQuickWidget.ResizeMode.SizeRootObjectToView)
    filename = os.fspath(CURRENT_DIRECTORY / "main.qml")
    url = QUrl.fromLocalFile(filename)
    widget.setSource(url)
    widget.show()

    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM