1

我想在编写 python 应用程序时使用 Felgo 工具包来扩展 Qt 的 qml 类型,但一直遇到问题并且没有太多运气让它启动和运行。我很好奇其他人在 python 应用程序中编写 qml 时是否能够成功使用 Felgo 库。如果是这样,你是怎么做到的?

这是我到目前为止所做的:

  1. 下载 Felgo 并让它在 QtCreator 中运行(这里没有使用 python)

  2. 创建了一个venv,pip将Pyside2安装到venv中。

  3. 创建了一个非常简单的 qml 文件,它使用 Felgo 组件和一个 python 文件来运行它。

//main.qml

import Felgo 3.0
import QtQuick 2.0

App {
    id: app
    AppText {
        text: "Hello World"
        anchors.centerIn: parent
    }
}
#main.py

import sys
import os

from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine


if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    engine.load(os.path.join(os.path.dirname(__file__), "main.qml"))

    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())
  1. 由于PySide2 未附带Felgo,我复制了从QT 文件夹安装的所有Felgo 相关组件并将它们粘贴到'...\venv\Lib\site-packages\PySide2\qml' 以加入标准的其余部分qml 组件。这些是我抓取的文件夹和文件
  • C:\Qt\Qt\5.12.8\mingw73_64\qml\Felgo
  • C:\Qt\Qt\5.12.8\mingw73_64\qml\VPlay
  • C:\Qt\Qt\5.12.8\mingw73_64\qml\VPlayApps
  • C:\Qt\Qt\5.12.8\mingw73_64\qml\VPlayPlugins
  • C:\Qt\Qt\5.12.8\mingw73_64\qml\builtins.qmltypes
  1. 当我尝试运行它时,我收到此错误
QQmlApplicationEngine failed to load component
file:///C:/Users/jblos/PycharmProjects/Felgo/TestProject/main.qml:3:1: module "Felgo" is not installed

我是不是忘记了什么?

编辑1:

在将 Qt 的 Felgo 版本(对我来说是 5.13.2)与 pyside2 版本(pip install pyside2==5.13.2)匹配后,这解决了我看到的几个问题。我不再看到“Felgo”没有安装。它还解决了一些存在循环 qml 依赖问题的问题。

现在该应用程序将在技术上运行并“工作”,但不存在任何主题。简单的按钮或导航栏看起来不像显示的实时预览。python vs Felgo 实时预览

查看 Felgo main.cpp 示例应用程序,他们正在使用可能不会发生的更多设置或初始化的 FelgoApplication 类?

// main.cpp from sample application

#include <QApplication>
#include <FelgoApplication>

#include <QQmlApplicationEngine>

// uncomment this line to add the Live Client Module and use live reloading with your custom C++ code
//#include <FelgoLiveClient>

int main(int argc, char *argv[])
{

    QApplication app(argc, argv);

    FelgoApplication felgo;

    // Use platform-specific fonts instead of Felgo's default font
    felgo.setPreservePlatformFonts(true);

    QQmlApplicationEngine engine;
    felgo.initialize(&engine);

    // Set an optional license key from project file
    // This does not work if using Felgo Live, only for Felgo Cloud Builds and local builds
    felgo.setLicenseKey(PRODUCT_LICENSE_KEY);

    // use this during development
    // for PUBLISHING, use the entry point below
    felgo.setMainQmlFileName(QStringLiteral("qml/Main.qml"));

    // use this instead of the above call to avoid deployment of the qml files and compile them into the binary with qt's resource system qrc
    // this is the preferred deployment option for publishing games to the app stores, because then your qml files and js files are protected
    // to avoid deployment of your qml files and images, also comment the DEPLOYMENTFOLDERS command in the .pro file
    // also see the .pro file for more details
    //felgo.setMainQmlFileName(QStringLiteral("qrc:/qml/Main.qml"));

    engine.load(QUrl(felgo.mainQmlFileName()));

    // to start your project as Live Client, comment (remove) the lines "felgo.setMainQmlFileName ..." & "engine.load ...",
    // and uncomment the line below
    //FelgoLiveClient client (&engine);

    return app.exec();
}
4

1 回答 1

0

我找到了这个链接,也许可以帮助你。QML 应用教程,带有 python https://doc.qt.io/qtforpython/tutorials/qmlapp/qmlapplication.html

我对您的解决方案感兴趣。

于 2020-12-06T13:20:13.607 回答