简体   繁体   中英

How to use Felgo with PySide2

I am wanting to use the Felgo tool kit to extend Qt's qml types when writing a python application but keep running into issues and haven't had much luck getting it up and running. I am curious if others have been able to successfully use the Felgo library when writing qml in a python app. If so, how did you do it?

Here is what I have done thus far:

  1. Downloaded Felgo and have it working inside QtCreator (no python used here)

  2. Created a venv, pip installed Pyside2 into the venv.

  3. Created a very simple qml file that uses Felgo components and a python file to run it.

//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. Since Felgo is not shipped with PySide2, I copy all the Felgo related components that were installed from the QT folder and paste them into '...\venv\Lib\site-packages\PySide2\qml' to join the rest of the standard qml components. These are the folders and file I grabbed
  • 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. When I try to run it, I get this error
QQmlApplicationEngine failed to load component
file:///C:/Users/jblos/PycharmProjects/Felgo/TestProject/main.qml:3:1: module "Felgo" is not installed

Am I forgetting something?

Edit 1:

After matching the Qt's version Felgo was built against (for me it was 5.13.2) with the pyside2 version (pip install pyside2==5.13.2), this solved a couple problems that I was seeing. I was no longer was seeing "Felgo" is not installed. And it also resolved some issues with there being cyclic qml dependency issues present.

Now the app will technically run and "work" but none of the theming is present. Simple buttons or navigation bars do not look anything like the live preview shown. python vs Felgo Live Preview

Looking at the Felgo main.cpp sample applications, they are using a FelgoApplication class that might be doing some more setup or initialization that might not be happening?

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

I found this link, maybe can help you. QML Application Tutorial, with python https://doc.qt.io/qtforpython/tutorials/qmlapp/qmlapplication.html

I am interested in your solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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