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 386 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.
  • R Offline
    R Offline
    Renio
    wrote on 11 May 2023, 16:11 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?
    J 1 Reply Last reply 11 May 2023, 16:45
    0
    • R Renio
      11 May 2023, 16:11

      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?
      J Offline
      J Offline
      JonB
      wrote on 11 May 2023, 16:45 last edited by JonB 5 Nov 2023, 16:45
      #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.

      R 1 Reply Last reply 12 May 2023, 08:02
      2
      • J JonB
        11 May 2023, 16:45

        @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.

        R Offline
        R Offline
        Renio
        wrote on 12 May 2023, 08:02 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?

        J 1 Reply Last reply 12 May 2023, 08:20
        0
        • R Renio
          12 May 2023, 08:02

          @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?

          J Offline
          J Offline
          JonB
          wrote on 12 May 2023, 08:20 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
          • R Renio has marked this topic as solved on 12 May 2023, 09:12

          2/4

          11 May 2023, 16:45

          • Login

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