Qt World Summit: Submit your Presentation

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
    def = "hello")";

    Is there better ways to do this?

  • Lifetime Qt Champion

    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