如何与PySide2一起使用Felgo

2024-09-30 01:19:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在编写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. 由于Felgo没有随PySide2一起提供,我将从QT文件夹中复制安装的所有Felgo相关组件,并将它们粘贴到“…\venv\Lib\site packages\PySide2\qml”中,以加入其余的标准qml组件。这些是我抓到的文件夹和文件
  • C:\Qt\Qt\5.12.8\mingw73\u 64\qml\Felgo
  • C:\Qt\Qt\5.12.8\mingw73\u 64\qml\VPlay
  • C:\Qt\Qt\5.12.8\mingw73\u 64\qml\VPlayApps
  • C:\Qt\Qt\5.12.8\mingw73\u 64\qml\VPlayPlugins
  • C:\Qt\Qt\5.12.8\mingw73\u 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:

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

现在,该应用程序将在技术上运行和“工作”,但没有主题出现。简单的按钮或导航栏与显示的实时预览完全不同python vs Felgo Live Preview

查看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();
}

Tags: andthetoimportapp应用程序mainqt

热门问题