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. How to run a php script in Qt?

How to run a php script in Qt?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 1.5k 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.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on last edited by
    #1

    Hi!
    I have a couple of scripts in php that can be run from the command line and they output text to the command line .
    Can I issue a command line in qt, run scripts , and get text?

    J.HilkJ 1 Reply Last reply
    0
    • M Mikeeeeee

      Hi!
      I have a couple of scripts in php that can be run from the command line and they output text to the command line .
      Can I issue a command line in qt, run scripts , and get text?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #2

      @Mikeeeeee
      yes you can


      Edit: I can't be that much of a prick, here ya go:
      https://doc.qt.io/qt-5/qprocess.html


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      5
      • M Offline
        M Offline
        Mikeeeeee
        wrote on last edited by
        #3

        If I do this, I get error:

        no matching function for call to 'QOverload ::of( )'
        connect(process, QOverload ::of(&QProcess::finished), this, QOverload ::of(&ParserApi::replyCheckFines));
        no matching function for call to 'QOverload ::of(void (ParserApi::*)())'
        connect(process, QOverload ::of(&QProcess::finished), this, QOverload ::of(&ParserApi::replyCheckFines));

        How do I launch the command line correctly?

        #include "parserapi.h"
        
        ParserApi::ParserApi()
        {
            process = new QProcess(this);
            //connect(process, &QProcess::readyReadStandardOutput, this, &ParserApi::replyCheckFines);
            //connect(process, &QProcess::finished, this, &ParserApi::replyCheckFines);
            //connect(process, QOverload<int, QProcess>::of(&QProcess::finished), this, QOverload<int, QProcess>::of(&ParserApi::replyCheckFines));
            connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
                [=](int exitCode, QProcess::ExitStatus exitStatus){ replyCheckFines(); });
        
        }
        
        void ParserApi::checkFines(QString numberCar, QString ctatePhoto)
        {
        //    process->start(folderWithScript);
        //    process->start("cmd /C php gopack.php check_fines У891ХС 750 9915647588 0");
            process->start("cmd /C dir");
        }
        
        void ParserApi::replyCheckFines()
        {
            qDebug()<<process->readAllStandardOutput();
        }
        
        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          HI
          You did not follow the docs and have your parameters in a QStringList.
          Nor can i see you call checkFines to try to start it. So that could be some of the issues.

          This sample runs dir and shows in a text browser widget when pressing a pushbutton.

          void MainWindow::on_pushButton_2_pressed()
          {
              QProcess *process = new QProcess(this);
              connect(process, &QProcess::started, []() {
                  qDebug() << "Started!";
              });
              connect(process, &QProcess::readyRead, [this, process]() {
                  QTextStream outputStream(process->readAllStandardOutput());
                  ui->textBrowser->append(outputStream.readAll());
              });
              connect(process, &QProcess::readyReadStandardOutput, [this, process]() {
                  QTextStream outputStream(process->readAllStandardOutput());
                  ui->textBrowser->append(outputStream.readAll());
              });
              connect(process, &QProcess::readyReadStandardError, [this, process]() {
                  QTextStream errorStream(process->readAllStandardError());
                  ui->textBrowser->append(errorStream.readAll());
              });
          
              connect(process, qOverload<int, QProcess::ExitStatus>(
                          &QProcess::finished), [this,process](int exitCode,
              QProcess::ExitStatus exitStatus) {
                  ui->textBrowser->append(QString::number(exitCode) + " " + QString::number(exitStatus));
                  process->deleteLater();
              });
          
              process->start("cmd.exe", QStringList()  << "/C" << "dir");
          }
          

          alt text

          1 Reply Last reply
          3
          • M Offline
            M Offline
            Mikeeeeee
            wrote on last edited by Mikeeeeee
            #5

            @mrjj said in How to run a php script in Qt?:

            HI
            You did not follow the docs and have your parameters in a QStringList.
            Nor can i see you call checkFines to try to start it. So that could be some of the issues.
            This sample runs dir and shows in a text browser widget when pressing a pushbutton.

            If I do this, I get error:
            QProcess: Destroyed while process ("cmd.exe") is still running.
            ""

            #include "parserapi.h"
            
            ParserApi::ParserApi()
            {
                process = new QProcess(this);
                connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), [=](int exitCode, QProcess::ExitStatus exitStatus){ replyCheckFines(); });
            
            }
            
            void ParserApi::checkFines(QString numberCar, QString ctatePhoto)
            {
                process->start("cmd.exe", QStringList()  << "/C" << "dir");
            }
            
            void ParserApi::replyCheckFines()
            {
                qDebug()<<process->readAllStandardOutput();
            }
            
            JonBJ 1 Reply Last reply
            0
            • M Mikeeeeee

              @mrjj said in How to run a php script in Qt?:

              HI
              You did not follow the docs and have your parameters in a QStringList.
              Nor can i see you call checkFines to try to start it. So that could be some of the issues.
              This sample runs dir and shows in a text browser widget when pressing a pushbutton.

              If I do this, I get error:
              QProcess: Destroyed while process ("cmd.exe") is still running.
              ""

              #include "parserapi.h"
              
              ParserApi::ParserApi()
              {
                  process = new QProcess(this);
                  connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), [=](int exitCode, QProcess::ExitStatus exitStatus){ replyCheckFines(); });
              
              }
              
              void ParserApi::checkFines(QString numberCar, QString ctatePhoto)
              {
                  process->start("cmd.exe", QStringList()  << "/C" << "dir");
              }
              
              void ParserApi::replyCheckFines()
              {
                  qDebug()<<process->readAllStandardOutput();
              }
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Mikeeeeee
              I don't think you're supposed to call your replyCheckFines() in finished slot, I think by then the sub-process has gone? Try it @mrjj's way, do your readAllStandardOutput() in your readyRead... slots? I'm not sure, but worth a try?

              P.S.

              QProcess: Destroyed while process ("cmd.exe") is still running.

              You're not letting your ParserApi instance get deleted/go out of scope, and taking process member variable with it, while the command has been started but not yet finished, are you?

              1 Reply Last reply
              1
              • M Offline
                M Offline
                Mikeeeeee
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mikeeeeee
                  wrote on last edited by
                  #8

                  It didn 't work because the class object is destroyed after closing the constructor.
                  When I did so, everything worked

                  ParserApi *qqq = new ParserApi();
                  
                  mrjjM JonBJ 2 Replies Last reply
                  0
                  • M Mikeeeeee

                    It didn 't work because the class object is destroyed after closing the constructor.
                    When I did so, everything worked

                    ParserApi *qqq = new ParserApi();
                    
                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Mikeeeeee
                    Hi
                    well it sound like you had a out of scope issue but its hard to
                    say when you dont see the complete code.

                    So now you are ready to run php.exe instead.
                    Make sure you also connect QProcess::ExitStatus signal and such so you
                    know when something goes wrong.

                    1 Reply Last reply
                    2
                    • M Mikeeeeee

                      It didn 't work because the class object is destroyed after closing the constructor.
                      When I did so, everything worked

                      ParserApi *qqq = new ParserApi();
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @Mikeeeeee

                      It didn 't work because the class object is destroyed after closing the constructor.

                      Ah, I did wonder if that message indicated you were in that situation. If you allow a QProcess object to get destructed while it is still running a command for you we can imagine why we get that message!

                      I think you need to wait till the finished (or errorred, of course) signal has arrived before destruction. People often call QProcess::deleteLater() from that slot as the safe place to destroy the object.

                      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