Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Execute a python script stored in qrc file

Execute a python script stored in qrc file

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 378 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • RenioR Offline
    RenioR Offline
    Renio
    wrote on last edited by
    #1

    Good evening,
    in my qt application I would like to run a python script through this code:

    void SandboxDialog::onExecPythonClicked(void)
    {
    
        QStringList params;
        params << ":/file/PythonSandboxClass.py";
        moP.start("python3", params);
        moP.waitForFinished();
    
        ui->moTextEdit->append("calling  PythonSandboxClass.py");
    }
    

    the python file is placed in a python .qrc file

    <RCC>
        <qresource prefix="/file">
            <file>PythonSandboxClass.py</file>
        </qresource>
    </RCC>
    

    but when I run the application, I get the following error:

    /usr/bin/python3: can't open file '/home/Qt/DiagSendbox/bin/:/PythonSandboxClass.py': [Errno 2] No such file or directory

    My questions are:

    • in the qrc file can I load files with *.py extension
    • if yes, where am I wrong?
    JonBJ 1 Reply Last reply
    0
    • RenioR Renio

      Good evening,
      in my qt application I would like to run a python script through this code:

      void SandboxDialog::onExecPythonClicked(void)
      {
      
          QStringList params;
          params << ":/file/PythonSandboxClass.py";
          moP.start("python3", params);
          moP.waitForFinished();
      
          ui->moTextEdit->append("calling  PythonSandboxClass.py");
      }
      

      the python file is placed in a python .qrc file

      <RCC>
          <qresource prefix="/file">
              <file>PythonSandboxClass.py</file>
          </qresource>
      </RCC>
      

      but when I run the application, I get the following error:

      /usr/bin/python3: can't open file '/home/Qt/DiagSendbox/bin/:/PythonSandboxClass.py': [Errno 2] No such file or directory

      My questions are:

      • in the qrc file can I load files with *.py extension
      • if yes, where am I wrong?
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Renio
      Resources are placed physically within the executable file. Qt code can access them via :/... syntax, but nothing outside can, they are not files in the operating system.

      To do what you want you would need to extract that file to an external .py file, get python3 to execute that, and finally delete the file.

      RenioR 1 Reply Last reply
      2
      • JonBJ JonB

        @Renio
        Resources are placed physically within the executable file. Qt code can access them via :/... syntax, but nothing outside can, they are not files in the operating system.

        To do what you want you would need to extract that file to an external .py file, get python3 to execute that, and finally delete the file.

        RenioR Offline
        RenioR Offline
        Renio
        wrote on last edited by
        #3

        @JonB Thank you for your reply.
        I do this because I don't want insert the path of python file into my code, or avoid that someone replace my script.
        this is my pro file

        QT       += core gui
        
        greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
        
        CONFIG += c++11
        
        # You can make your code fail to compile if it uses deprecated APIs.
        # In order to do so, uncomment the following line.
        #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
        
        SOURCES += \
            main.cpp \
            SandboxDialog.cpp
        
        HEADERS += \
            SandboxDialog.h
        
        FORMS += \
            SandboxDialog.ui
        
        TRANSLATIONS += \
            DiagSendbox_it_IT.ts
        CONFIG += lrelease
        
        # Default rules for deployment.
        qnx: target.path = /tmp/$${TARGET}/bin
        else: unix:!android: target.path = /opt/$${TARGET}/bin
        !isEmpty(target.path): INSTALLS += target
        
        DISTFILES += \
            PythonSandboxClass.py
        
        RESOURCES += \
            python.qrc
        

        I undestrud that i can't to do this, correct?

        JonBJ 1 Reply Last reply
        0
        • RenioR Renio

          @JonB Thank you for your reply.
          I do this because I don't want insert the path of python file into my code, or avoid that someone replace my script.
          this is my pro file

          QT       += core gui
          
          greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
          
          CONFIG += c++11
          
          # You can make your code fail to compile if it uses deprecated APIs.
          # In order to do so, uncomment the following line.
          #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
          
          SOURCES += \
              main.cpp \
              SandboxDialog.cpp
          
          HEADERS += \
              SandboxDialog.h
          
          FORMS += \
              SandboxDialog.ui
          
          TRANSLATIONS += \
              DiagSendbox_it_IT.ts
          CONFIG += lrelease
          
          # Default rules for deployment.
          qnx: target.path = /tmp/$${TARGET}/bin
          else: unix:!android: target.path = /opt/$${TARGET}/bin
          !isEmpty(target.path): INSTALLS += target
          
          DISTFILES += \
              PythonSandboxClass.py
          
          RESOURCES += \
              python.qrc
          

          I undestrud that i can't to do this, correct?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Renio
          Sorry, I don't understand if there is a further question here. This is fine, I wrote in my previous why you get the error trying to execute it and what you need to do at runtime to make it work.

          1 Reply Last reply
          1
          • RenioR Renio has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved