Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to run a php script in Qt?



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


  • Moderators

    @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



  • 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();
    }
    

  • Lifetime Qt Champion

    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



  • @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();
    }
    


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



  • This post is deleted!


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

    ParserApi *qqq = new ParserApi();
    

  • Lifetime Qt Champion

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



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


Log in to reply