A simple use of QT programming 8:qprocess (calling external applications)

Source: Internet
Author: User


External programs take Timer.exe as an example






Qprocess There are three ways to start an external program:



    void Start (const QString &program, const qstringlist &arguments, OpenMode mode = ReadWrite);
    void Start (const QString &program, OpenMode mode = ReadWrite);
    

    static int Execute (const QString &program, const qstringlist &arguments);
    static int Execute (const QString &program);
    

    static bool Startdetached (const QString &program, const qstringlist &arguments, const QString & WorkingDirectory,
                              Qint64 *pid = 0);
    static bool Startdetached (const QString &program, const qstringlist &arguments);
    static bool Startdetached (const QString &program);


The start () and startdetached () functions are non-blocking functions.






1, for example, using start to call the previous write Timer.exe



Pro = Newqprocess (this);



    Pro->start ("Timer.exe");
    Qdebug () << TR ("Main program continues to run");
    Qdebug () << tr ("-----------------------");


The results show that the Timer.exe program runs and the Qdebug () output.










The two functions typically used in conjunction with the Start () function (which are blocking functions):



Waitforstarted (): Waits for the program to start, the time-out is 30 seconds (you can set the wait time yourself). If the program starts successfully and returns TRUE, the startup time-out or startup failure returns false.



Waitforfinished (): Waits for the program to end with a time-out of 30 seconds (you can set the wait time yourself). Returns true if the wait time Timer.exe program ends, and the timeout returns false.



    Qdebug () << tr ("------------------------Main program running");
    Pro->start ("Timer.exe");
    if (pro->waitforstarted)
    {
        qdebug () << tr ("Timer.exe started successfully");

        Waits for the external program to end, if the external program is closed within a given time, returns True, the timeout returns false
        if (pro->waitforfinished (10000))
            Qdebug () << tr (" Timer.exe program is closed ");
        else
            qdebug () << TR ("Timer.exe program is not closed within the specified time");
    }
    Qdebug () << TR ("------------Main program continues to run-----------");






2, use Pro->execute ("Timer.exe"), then only run Timer.exe application, the main program is blocked here, know Timer.exe end.



Not a long explanation



3, normally, the use of the start () function to call the external program, the current program is closed, the external program will be closed, so the introduction of function startdetached ().



Execution succeeds returns true, otherwise false is returned.



   Qdebug () << tr ("------------------------Main program running");
    if (pro->startdetached ("Timer.exe")
    {
        qdebug () << TR ("External program Timer.exe Run successfully");
        Close ();                Close main program
    }
   qdebug () << TR ("------------Main program continues to run-----------");    will not be executed


The result is: Timer.exe starts normally and the main program shuts down.



4, when the external program exists space or Chinese characters, the above three functions can not use a parameter to execute an external program, you need to pass in the second parameter (external program if no parameters).



QString program  = TR ("Time process/timer.exe");
"Time Process" folder
Pro->start (Program,qstringlist ());

Pro->execute (Program,qstringlist ());

Pro->startdetached (Program,qstringlist ())




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.