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. Cannot open file - QProcess

Cannot open file - QProcess

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 832 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.
  • C Offline
    C Offline
    Chaki
    wrote on last edited by Chaki
    #1

    Hello, I am trying to start a python script with QProcess on my Raspberry. I have tried the docu, but the following code doesn't start my python file:
    The python-file does only create a .txt-file and works if I run it.

    communication.cpp:

    #include "communication.h"
    #include <QProcess>
    
    communication::communication()
    {
    }
    communication::~communication()
    {
    }
    
    void communication::conPy()
    {
        QProcess *process = new QProcess(this);
        QString program = "test.py";
        QString folder = "/home/myFolder/test.py";
        process->start(program, QStringList() << folder);```
    
    

    communication.h:

    
    #ifndef COMMUNICATION_H
    #define COMMUNICATION_H
    
    #include <QObject>
    
    class communication : public QObject
    {
        Q_OBJECT
    public:
        communication();
        ~communication();
        void conPy();
    };
    
    #endif // COMMUNICATION_H
    

    main.cpp:

    #include <QCoreApplication>
    #include <communication.h>
    
    int main(int argc, char *argv[])
    {
         QCoreApplication a(argc, argv);
         communication comm;
         comm.comPy();
         return a.exe();
    }
    

    test.py:

    fp = open('test.txt', 'x')
    fp.close()
    
    jsulmJ 1 Reply Last reply
    0
    • C Chaki

      Hello, I am trying to start a python script with QProcess on my Raspberry. I have tried the docu, but the following code doesn't start my python file:
      The python-file does only create a .txt-file and works if I run it.

      communication.cpp:

      #include "communication.h"
      #include <QProcess>
      
      communication::communication()
      {
      }
      communication::~communication()
      {
      }
      
      void communication::conPy()
      {
          QProcess *process = new QProcess(this);
          QString program = "test.py";
          QString folder = "/home/myFolder/test.py";
          process->start(program, QStringList() << folder);```
      
      

      communication.h:

      
      #ifndef COMMUNICATION_H
      #define COMMUNICATION_H
      
      #include <QObject>
      
      class communication : public QObject
      {
          Q_OBJECT
      public:
          communication();
          ~communication();
          void conPy();
      };
      
      #endif // COMMUNICATION_H
      

      main.cpp:

      #include <QCoreApplication>
      #include <communication.h>
      
      int main(int argc, char *argv[])
      {
           QCoreApplication a(argc, argv);
           communication comm;
           comm.comPy();
           return a.exe();
      }
      

      test.py:

      fp = open('test.txt', 'x')
      fp.close()
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @Chaki said in Cannot open file - QProcess:

      QString program = "test.py";
      QString folder = "/home/myFolder/test.py";
      process->start(program, QStringList() << folder);

      Should rather be:

      QString program = "/home/myFolder/test.py";
      process->start(program, QStringList());
      

      Don't forget to delete the QProcess instance when not needed anymore, else you will have a memory leak.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chaki
        wrote on last edited by Chaki
        #3

        Thank you for your answer, I know that it works with following code:

        void communication::conPy()
        {
            QProcess *process = new QProcess(this);
            QString program = "/home/someFolder/test.py";
            QStringList arguments;
            arguments << QDir::currentPath()<< "/home/someFolder/test.py";
            process->start("python", arguments);
        
            if(!process->waitForFinished(10000))
                {
                    qDebug() << "Error: " <<process->errorString();
                }
                else
                {
                    qDebug() << "Analyzing done. ";
                }
        
        }
        
        jsulmJ JonBJ 3 Replies Last reply
        0
        • C Chaki

          Thank you for your answer, I know that it works with following code:

          void communication::conPy()
          {
              QProcess *process = new QProcess(this);
              QString program = "/home/someFolder/test.py";
              QStringList arguments;
              arguments << QDir::currentPath()<< "/home/someFolder/test.py";
              process->start("python", arguments);
          
              if(!process->waitForFinished(10000))
                  {
                      qDebug() << "Error: " <<process->errorString();
                  }
                  else
                  {
                      qDebug() << "Analyzing done. ";
                  }
          
          }
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

          @Chaki Is test.py marked as executable? Does it have the shebang line?

          #/usr/bin/env python3
          

          Can you start it directly from a terminal? Does that work?
          Also, you should really add error handling, see QProcess documentation.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • C Chaki

            Thank you for your answer, I know that it works with following code:

            void communication::conPy()
            {
                QProcess *process = new QProcess(this);
                QString program = "/home/someFolder/test.py";
                QStringList arguments;
                arguments << QDir::currentPath()<< "/home/someFolder/test.py";
                process->start("python", arguments);
            
                if(!process->waitForFinished(10000))
                    {
                        qDebug() << "Error: " <<process->errorString();
                    }
                    else
                    {
                        qDebug() << "Analyzing done. ";
                    }
            
            }
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Chaki said in Cannot open file - QProcess:

            arguments << QDir::currentPath()<< "/home/someFolder/test.py";

            Why do you pass the path to the executable as parameter?!

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • C Chaki

              Thank you for your answer, I know that it works with following code:

              void communication::conPy()
              {
                  QProcess *process = new QProcess(this);
                  QString program = "/home/someFolder/test.py";
                  QStringList arguments;
                  arguments << QDir::currentPath()<< "/home/someFolder/test.py";
                  process->start("python", arguments);
              
                  if(!process->waitForFinished(10000))
                      {
                          qDebug() << "Error: " <<process->errorString();
                      }
                      else
                      {
                          qDebug() << "Analyzing done. ";
                      }
              
              }
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Chaki
              Please first answer @jsulm's questions. If you have not set up your script file to be a properly executable Python file as he states that would be the reason for your problem.

              However, additionally:

              I know that it works with following code:

              process->start("python", arguments);
              

              I do not know about RPi, but some Linux distributions require you to use python3 as the executable. python may either run Python 2.x or does not have any such executable installed.

              arguments << QDir::currentPath()<< "/home/someFolder/test.py";
              

              You say that "it works with following code", but that should give you an error like:

              /usr/bin/python3: can't find '__main__' module in '/the/QDir/currentPath/directory'
              

              You are sure what you say works without this error, you did actually test it?

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Chaki
                wrote on last edited by Chaki
                #7

                @jsulm yes, i was able to start the python-script from a terminal. I posted the wrong code earlier - it works with the following lines, which I also tested:

                 QProcess *process = new QProcess(this);
                 process->start("python3 /home/someFolder/test.py");
                
                    if(!process->waitForFinished(10000))
                        {
                            qDebug() << "Error: " <<process->errorString();
                        }
                        else
                        {
                            qDebug() << "Analyzing done. ";
                        }
                
                JonBJ jsulmJ 2 Replies Last reply
                0
                • C Chaki

                  @jsulm yes, i was able to start the python-script from a terminal. I posted the wrong code earlier - it works with the following lines, which I also tested:

                   QProcess *process = new QProcess(this);
                   process->start("python3 /home/someFolder/test.py");
                  
                      if(!process->waitForFinished(10000))
                          {
                              qDebug() << "Error: " <<process->errorString();
                          }
                          else
                          {
                              qDebug() << "Analyzing done. ";
                          }
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @Chaki
                  That is not what @jsulm is asking you. You have already said that you can run your script, even from QProcess, with python3 .... What you have asked is why you cannot execute it with just QString program = "/home/someFolder/test.py";.

                  If you want to know the answer to that, go into a terminal and type

                  /home/someFolder/test.py
                  

                  and see what happens.

                  C 1 Reply Last reply
                  2
                  • JonBJ JonB

                    @Chaki
                    That is not what @jsulm is asking you. You have already said that you can run your script, even from QProcess, with python3 .... What you have asked is why you cannot execute it with just QString program = "/home/someFolder/test.py";.

                    If you want to know the answer to that, go into a terminal and type

                    /home/someFolder/test.py
                    

                    and see what happens.

                    C Offline
                    C Offline
                    Chaki
                    wrote on last edited by
                    #9

                    @JonB I will have a look at it - thank you very much for your answers!

                    1 Reply Last reply
                    0
                    • C Chaki

                      @jsulm yes, i was able to start the python-script from a terminal. I posted the wrong code earlier - it works with the following lines, which I also tested:

                       QProcess *process = new QProcess(this);
                       process->start("python3 /home/someFolder/test.py");
                      
                          if(!process->waitForFinished(10000))
                              {
                                  qDebug() << "Error: " <<process->errorString();
                              }
                              else
                              {
                                  qDebug() << "Analyzing done. ";
                              }
                      
                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Chaki said in Cannot open file - QProcess:

                      process->start("python3 /home/someFolder/test.py");

                      This should be

                      process->start("python3", QStringList() << "/home/someFolder/test.py");
                      

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      2

                      • Login

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