Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Reading python script and store as string at compile time?



  • I'm using C++ version of Qt, I want to read some python script and store them as strings at compile time. I tried copy and pasting them into the cpp file and read them as raw string literal. It works but since python script is space sensitive, they looks so ugly when pasted in a cpp file

                QString pythonScript = R"(import abc as apple
    print("1")
    def = "hello")";
    

    Is there better ways to do this?


  • Lifetime Qt Champion

    Hi
    You could use a resource file and store a .py file there and load it.



  • @mrjj

    Thanks I tried the resource system and it works.

    QFile file(":/python_scripts/script1.py");        
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QString pythonScript = file.readAll();
    

Log in to reply