Use qprocess to enable external programs and close external programs in QT

Source: Internet
Author: User
Tags emit

Because the current program needs to provide a file contrast function, and there is already a special text contrast software, so I intend to directly call the external text contrast program.
By looking at the help documentation for QT, you found that the Qprocess class could provide this requirement.
I found the following two ways to start an external program:
1, Start ()
void Qprocess::start (const & program, const & arguments, mode = ReadWrite)

Starts the program is in a new process, passing the command line arguments in arguments.  The is set to mode. Would immediately enter the starting state. If The process starts successfully, would emit (); Otherwise, () would be emitted.

Note this arguments that contain spaces are not passed to the process as separate arguments.

Windows:arguments that contain spaces are wrapped in quotes.

Note:processes are started asynchronously, which means the () and () signals may is delayed. Call () to make sure the process has started (or has failed to start) and those signals have.

Also (), (), and ().
2, use Qprocess::execute (), but when using this method, the program will be most blocked until the end of the program executed by this method to return, this time can be used in combination with qprocess and qthread these two classes to use the method to handle, to prevent calls in the main thread caused by blocking
First inherit a class from Qthread and implement the Run () function again:

Quote:
Class Mythread:public Qthread
{
Public
void run ();
};

void Mythread::run ()
{
Qprocess::execute ("notepad.exe");
}

In this way, you can define a member variable of type mythread when you use it, and call its start () method when you use it:

Quote:
Class .........
{...........
Mythread thread;
............
};

.....................
Thread.Start ();

Two specific differences I am not very clear, but I tried in the program, found that the second will block the main program, but use the start () does not. Here is the code that I use Qprocess to start Winmerge.
#include
Qprocess *process = new Qprocess;
Qstringlist str;
STR << "";
Process->start ("./winmerge/winmergeu.exe", str);
If the program does not input parameters, it will directly to the software directly run the interface.
The interface that pops up after the argument is the first one in the first column of the toolbar that needs to be entered (this is my guess, I'm not sure, but it does pop).
Here is a screenshot:
The buttons in the GUI:

After clicking the switch configuration comparison:


Well, then you can start the external program.

^-^ added: When the main program exits, the external program that is started will not exit with the main program exiting, we certainly do not want this situation.
Continue to review the QT Help document and discover the function close, and look at its description:
void Qprocess::close () [virtual]

Closes all communication with the process and kills it. After the calling this function, would no longer emit (), and data can no longer be read or written.

Reimplemented from.

As you can see, calling this will close all the process-initiated external programs. So, you can add a judgment before the main program is launched.

if (process)

Process->close ();

Delete process;

Process = 0;



From:http://blog.chinaunix.net/uid-26808060-id-3347391.html

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.