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. Passing arguments to Python function using QProcess
Forum Updated to NodeBB v4.3 + New Features

Passing arguments to Python function using QProcess

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 3 Posters 2.3k Views 1 Watching
  • 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #10

    Then seeing None printed is normal since that method returns None.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • JonBJ JonB

      @giusirux
      That looks good. The None is doubtless being printed somewhere or a return result, I wouldn't worry about it. [EDIT: Oh I see, it's the output from your print(...).]

      Now we must take you back to: you still have not explained how you know "This code doesn't work"?

      It is time you put in the code to show what is in stdard output/error when you run your QProcess.

      G Offline
      G Offline
      giusirux
      wrote on last edited by
      #11

      @JonB when I run my QProcess, nothing is happening.
      xml_writer_path 's goal is to write pathFile in a file which is included in my Qt project.
      In fact, when I run my QProcess, the file is not modified

      JonBJ 1 Reply Last reply
      0
      • G giusirux

        @JonB when I run my QProcess, nothing is happening.
        xml_writer_path 's goal is to write pathFile in a file which is included in my Qt project.
        In fact, when I run my QProcess, the file is not modified

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

        @giusirux
        Last time of saying:

        It is time you put in the code to show what is in stdard output/error when you run your QProcess.

        1 Reply Last reply
        1
        • G Offline
          G Offline
          giusirux
          wrote on last edited by giusirux
          #13

          @JonB
          QObject::connect(buttonSave, &QPushButton::clicked, widget, saveFile);

          in saveFile:

          void saveFile() {
              //PYTHON
              qDebug() << "save File";
              QProcess p;
              QString filename = QFileDialog::getSaveFileName(new QWidget,
                                                 "Save file", "C:",
                                                 "XML (*.xml);");
              qDebug() << filename;
              QStringList arguments { "-c", "from storage_retrieval import xml_writer_path, sys; print(xml_writer_path(str(sys.argv[1])))", filename };
              p.start("python", arguments);
              p.waitForFinished();
              QMessageBox::information(widget," ","done");
              qDebug() << "end";
          }
          

          Application output:
          ad2a3111-9e30-4a8e-af14-1b91eaa0395e-image.png
          QMessageBox appears without problems.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #14

            As already written, and quoted: read the standard output and standard error of your QProcess.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            G 1 Reply Last reply
            2
            • SGaistS SGaist

              As already written, and quoted: read the standard output and standard error of your QProcess.

              G Offline
              G Offline
              giusirux
              wrote on last edited by giusirux
              #15

              @SGaist yes, sorry.. So:

              void saveFile() {
                  //PYTHON
                  qDebug() << "save File";
                  QProcess p;
                  QString filename = QFileDialog::getSaveFileName(new QWidget,
                                                     "Save file", "C:",
                                                     "XML (*.xml);");
                  qDebug() << filename;
                  QStringList arguments { "-c", "from storage_retrieval import xml_writer_path, sys; print(xml_writer_path(str(sys.argv[1])))", filename };
                  p.start("python", arguments);
                  p.waitForFinished();
                  qDebug() << p.readAllStandardOutput();
                  qDebug() << "-----";
                  qDebug() << p.readAllStandardError();
                  QMessageBox::information(widget," ","done");
                  qDebug() << "end";
              }
              

              b7d10d55-623f-4ea8-86be-d0c4a66db459-image.png

              in xml_writer_path:

              def xml_writer_path(filepath):
              
                      try:
                              print("[+] Xml writer path in esecuzione...")
              
                              flag=0
                              with open("metadati_segmentazione.txt", "r") as input_file, open("metadati_segmentazione2.txt", "w") as output_file:
                                      
                                      for line in input_file:
                                              
                                              if line.startswith("#metadati_fase3"):
                                                      flag=1
                                                      output_file.write(line)
                                                      
                                              elif line.startswith("s3name") and flag == 1:
                                                      output_file.write(line.replace("s3name:","s3name:"+str(filepath)))
              
                                              else:
                                                      output_file.write(line)
              
                              input_file.close()                
                              output_file.close()
              
                              os.remove("metadati_segmentazione.txt")
                              os.rename("metadati_segmentazione2.txt","metadati_segmentazione.txt")
              
                              print("[+] Esecuzione xml writer path terminata.")
              
                      except:
                              print("[-] Error xml writer path.")
              
              JonBJ 1 Reply Last reply
              0
              • G giusirux

                @SGaist yes, sorry.. So:

                void saveFile() {
                    //PYTHON
                    qDebug() << "save File";
                    QProcess p;
                    QString filename = QFileDialog::getSaveFileName(new QWidget,
                                                       "Save file", "C:",
                                                       "XML (*.xml);");
                    qDebug() << filename;
                    QStringList arguments { "-c", "from storage_retrieval import xml_writer_path, sys; print(xml_writer_path(str(sys.argv[1])))", filename };
                    p.start("python", arguments);
                    p.waitForFinished();
                    qDebug() << p.readAllStandardOutput();
                    qDebug() << "-----";
                    qDebug() << p.readAllStandardError();
                    QMessageBox::information(widget," ","done");
                    qDebug() << "end";
                }
                

                b7d10d55-623f-4ea8-86be-d0c4a66db459-image.png

                in xml_writer_path:

                def xml_writer_path(filepath):
                
                        try:
                                print("[+] Xml writer path in esecuzione...")
                
                                flag=0
                                with open("metadati_segmentazione.txt", "r") as input_file, open("metadati_segmentazione2.txt", "w") as output_file:
                                        
                                        for line in input_file:
                                                
                                                if line.startswith("#metadati_fase3"):
                                                        flag=1
                                                        output_file.write(line)
                                                        
                                                elif line.startswith("s3name") and flag == 1:
                                                        output_file.write(line.replace("s3name:","s3name:"+str(filepath)))
                
                                                else:
                                                        output_file.write(line)
                
                                input_file.close()                
                                output_file.close()
                
                                os.remove("metadati_segmentazione.txt")
                                os.rename("metadati_segmentazione2.txt","metadati_segmentazione.txt")
                
                                print("[+] Esecuzione xml writer path terminata.")
                
                        except:
                                print("[-] Error xml writer path.")
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #16

                @giusirux
                This isn't what we meant, but never mind....

                Change your try to have print(filepath) as its first statement.

                Change your except to print full information about the exception. Never just put an except like you have with no detail into your code. You might even be better commenting out the try and the except lines while you debug your issue. Put further print statements into the body to see where the error is occurring. All this is what is expected of debugging a program problem.

                1 Reply Last reply
                2
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #17

                  @giusirux said in Passing arguments to Python function using QProcess:

                  metadati_segmentazione.txt

                  Where is that file exactly located ?

                  Do not use bare except, at least print the exception you got to know what is happening.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  G 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    @giusirux said in Passing arguments to Python function using QProcess:

                    metadati_segmentazione.txt

                    Where is that file exactly located ?

                    Do not use bare except, at least print the exception you got to know what is happening.

                    G Offline
                    G Offline
                    giusirux
                    wrote on last edited by
                    #18

                    @SGaist @JonB thank you very much for all the advice!
                    So, the problem was: "metadati_segmentazione.txt" was in the wrong position in my Qt project (it was in debug folder project and I don't know why).
                    I'll mark this post as solved!
                    Thank you again

                    JonBJ 1 Reply Last reply
                    0
                    • G giusirux

                      @SGaist @JonB thank you very much for all the advice!
                      So, the problem was: "metadati_segmentazione.txt" was in the wrong position in my Qt project (it was in debug folder project and I don't know why).
                      I'll mark this post as solved!
                      Thank you again

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

                      @giusirux
                      Yes, but please change that bare except with no detail, it just hides any problem which may occur!

                      1 Reply Last reply
                      0

                      • Login

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