Qt Process Qprocess

Source: Internet
Author: User
definition

The Qprocess class is used-to-start external programs and to communicate with them.

The Qprocess class is used to start an external program and communicate with it. Instructions for use 3 ways to open an external thread

To start a process, pass the name and command line arguments of "the program" want to run as arguments to start (). Arguments is supplied as individual strings in a qstringlist.

Alternatively, you can set the program to run with Setprogram () and setarguments (), and then call Start () or open ().

To start a process, you can pass the name of the program you want to start and the command-line arguments passed as parameters to the start () function. Parameters as each individual string in the qstringlist.

Alternatively, you can set the program through Setprogram () and setarguments (), and then call Start () or open () to start the program. The parameter is attached when the program name is specified at startup

Qprocess process;
Process.Start ("cmd/c ping 127.0.0.1");
specify function name and parameter list separately at startup
Qprocess process;
Process.Start ("cmd", qstringlist () << "/C" << "ping 127.0.0.1", qiodevice::readwrite);
specify function names and parameters, respectively, and then start
Qprocess process;
Process.setprogram ("cmd");
Process.setarguments (Qstringlist () << "/C" << "ping 127.0.0.1");
Process.Start (Qiodevice::readwrite);
read-once process output
void Readprocessallonce (bool processautoexit) {qdebug () << "readprocessallonce, processautoexit =" <<
    Processautoexit;
    Qdebug () << "----------------------";
    BOOL ret;
    Qprocess process;
    QString command;
    if (processautoexit) {command = "cmd/c ping 127.0.0.1";
    } else {command = "cmd/k ping 127.0.0.1";
    } process.start (command);
    /* * BOOL qprocess::waitforstarted (int msecs = 30000) */ret = process.waitforstarted ();
    Qdebug () << "waitforstarted" <<ret;
    Qdebug () <<qdatetime::currentdatetime (); /* * If you open a process that is not automatically closed, you may have to wait up to 30 seconds * BOOL Qprocess::waitforfinished (int msecs = 30000) */ret = Process
    . waitforfinished ();
    Qdebug () <<qdatetime::currentdatetime ();
    Qdebug () << "waitforfinished" <<ret;
    Qbytearray ByteArray = Process.readallstandardoutput ();
    QString str = qstring::fromlocal8bit (ByteArray);
    Qdebug () <<str; Process.closE ();
    Qdebug () << "";
Qdebug () << "======================"; }

read process output by row

void Readprocessbyline () {qdebug () << "Readprocessbyline";
    Qdebug () << "----------------------";
    BOOL ret;
    Qprocess process (0); /* * CMD/C commands are closed when the command is finished * cmd/k the command does not close the window after execution * * AA && BB is executed AA, after success then execute BB * aa | | BB executes the AA first, if the execution succeeds the BB is no longer executed, and if the failure executes BB * A & B means execute A and B, regardless of whether a succeeds */Process.Start ("cmd", qstringlist () &LT;&L t; "
/k "<<" ping 127.0.0.1 "<<" & "<<" Exit ", Qiodevice::readwrite);
Process.setprogram ("cmd");
Process.setarguments (Qstringlist () << "/k" << "ping 127.0.0.1" << "&" << "exit");
    Process.Start (Qiodevice::readwrite);
    ret = process.waitforstarted ();
    Qdebug () << "waitforstarted" <<ret;
    Qint64 maxSize = 512;
    Char Buffer[maxsize];
    Qint64 Len;
        while (true) {/* * a waitforreadyread signal may output multiple lines */ret = Process.waitforreadyread ();
    Qdebug () << "Waitforreadyread" <<ret;    if (!ret) {break;
            } while (true) {len = process.readline (buffer, maxSize);
            Qdebug () << "Buffer Len" <<len;
                /* Because each line has at least a carriage return newline, it reads 0, indicating that the Waitforreadyread timeout returns false */if (len <= 0) {
            Break
            } QString str = qstring::fromlocal8bit (buffer);
            Qdebug () << "Qstring len" <<str.length ();
            Qdebug () <<str;
        Qdebug () << "";
    }} process.write ("exit\r\n");
    ret = process.waitforfinished ();
    Qdebug () << "waitforfinished" <<ret;
    Process.close ();
    Qdebug () << "";
Qdebug () << "======================"; }

interacting with the process

void Openprocessthenwrite () {qdebug () << "Openprocessthenwrite";
    Qdebug () << "----------------------";
    BOOL ret;
    Qprocess *process = new Qprocess (Qthread::currentthread ()); /* * If there is no qcoreapplication, the following error will be reported * Qobject::starttimer:timers can only is used with threads started with Qthr ead.
     A timer can only be created and started in the same thread.
    */Process->start ("cmd", qiodevice::readwrite);
    ret = process->waitforstarted ();
    Qdebug () << "Waitforreadyread" <<ret;
    Qbytearray ByteArray = Process->readallstandardoutput ();
    QString str = qstring::fromlocal8bit (ByteArray);
    Qdebug () <<str;
    The command cannot be sent out without a change of line Qint64 len = Process->write ("ping 127.0.0.1\r\nexit\r\n");
    Qdebug () << "Write Len" <<len; while (true) {/* * If the process has been closed, Waitforreadyread will return false */ret = Process->waitforready
        Read ();
        Qdebug () << "Waitforreadyread" <<ret; if (!ret) {break;
        } ByteArray = Process->readallstandardoutput ();
        str = qstring::fromlocal8bit (ByteArray);
        Qdebug () <<str;
    Qdebug () << "";
    }/* * If there is no command to close cmd, the process will never finished and will time out (30 seconds) to return false */ret = process->waitforfinished ();
    Qdebug () << "waitforfinished" <<ret;
    Process->close ();
    Delete process;
    Qdebug () << "";
Qdebug () << "======================"; }

test function

#include <QCoreApplication>
#include <QDebug>
#include <QProcess>
#include < qthread>
#include <QDateTime>

void readprocessallonce (bool processautoexit) {
    ...
}

void Readprocessbyline () {
    ...
}

void Openprocessthenwrite () {
    ...
}

int main (int argc, char *argv[])
{
    qcoreapplication A (argc, argv);
    Qdebug () << "======================";
    Readprocessallonce (false);
    Readprocessallonce (true);
    Readprocessbyline ();
    Openprocessthenwrite ();

    Qdebug () << "that's all";
    return a.exec ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.