The painful experience of qprocess invoking command line in QT

Source: Internet
Author: User
Tags rar

In the QT program, some directories and files need to be compressed into a RAR archive. Then thought of in Qt through the Qprocess Class call command line Rar.exe to achieve the effect, but did not think Qprocess class used to be very troublesome, and can not reach the effect, tossing for 2 days still did not find the reason, using another method to solve.

How to create a compressed package

To create a compressed package on the Windows platform, you can use Rar.exe directly, which can be found in the installation directory after installing WinRAR. The program is a command-line version of WinRAR, with syntax examples as follows:

rar.exe a -k -r -s -m1 test.rar direct1/   direct2/  test.txt

Examples correspond to the following directory structure:

The above command indicates the creation of the compressed package Test.rar under the current directory, adding the Direct1 directory under the current directory with all its subdirectories and files, the Direct2 directory, its subdirectories and files, and the file Test.txt in the current directory to the Test.rar compressed package.

Where parameter a means adding to the compressed package

Parameter-R means recursive addition

After the issue of the command has been resolved, the following is how to invoke the command in Qt, and there are a number of problems in actually invoking the command in Qt.

Invoking the command line in QT

Calling external commands in QT generally uses the member functions provided by the Qprocess class, which use the following specific code:

Qprocess p (0);p. Start (Command,args); command is the order to execute, args is the parameter p.waitforfinished ();//Waits to complete qdebug () <<qstring::fromlocal8bit ( P.readallstandarderror ());

Apply the above code to get the following:

Qprocess p (0); QString command = "E:/test_rar_course/rar.exe"; Qstringlist args;args.append ("a"); Args.append ("-K"); Args.append ("-R"); Args.append ("-S"); Args.append ("-m1"); Args.append ("E:/test_rar_course/test.rar"); Args.append ("e:/test_rar_course/direct1/"); Args.append ("E:/test_rar _course/direct2/"), Args.append (" E:/test_rar_course/test.txt ");p. Execute (Command,args);//command is the command to execute, Args is the parameter p.waitforfinished (); Qdebug () <<qstring::fromlocal8bit (P.readallstandarderror ());

Test.rar can be generated however, the path e:/test_rar_course is also compressed in the package, and I need to open the compressed package only to see DIRECT1,DIRECT2, TEST.TXT3 a project, then it is not possible to set up a working directory:

Qprocess p (0);p. Setworkingdirectory ("e:/test_rar_course/");//Specifies the working directory of the process qstring command = "e:/test_rar_course/ Rar.exe "; Qstringlist args;args.append ("a"); Args.append ("-K"); Args.append ("-R"); Args.append ("-S"); Args.append ("-m1"); Args.append ("-we:/test_rar_course/");//Specify the working directory of the Rar.exe args.append ("Test.rar"); Args.append ("direct1/"); Args.append ("direct2/"), Args.append ("Test.txt");p. Execute (Command,args),//command is the command to execute, Args is the parameter p.waitforfinished (); Qdebug () <<qstring::fromlocal8bit (P.readallstandarderror ());//Get Output

Not only do I add the-w parameter, which is the command-line argument for Rar.exe, to specify the working directory, to specify the working directory for the Rar.exe command, but also to use P.setworkingdirectory () to specify the working directory for the started process, and then to run an error, saying that the file could not be found. I guess it may still be the work directory problem, but do not know where the problem, check a lot of information is useless, eventually abandoned this attempt, changed to the following attempt:

Qprocess p (0);p. Setworkingdirectory ("e:/test_rar_course/");//Specifies the working directory of the process qstring command = "e:/test_rar_course/ Test.bat ";p. Start (command);p. waitforfinished (); Qdebug () <<qstring::fromlocal8bit (P.readallstandarderror () );

The contents of Test.bat are as follows:

cd/d e:/test_rar_course/e:/test_rar_course/rar.exe a-k-r-s-m1-we:/test_rar_course/test.rar direct1/direct2/test.t Xt

I switch the working directory directly in the bat with the CD command, and then compress it, in order to avoid the compression of the absolute path, DIRECT1,DIRECT2, Test.txt use is relative path, direct mouse double-click the Test.bat run OK, put in Qt run OK, seems to solve the problem perfectly.

But I found that if there is a () parenthesis character in the table of contents, the path is truncated in the qprocess execution error when the bracket character is present, and then I enclose the path in quotation marks without effect:

"\"E:/test_rar_course(xx)/test.bat\""

According to the information found on the Internet, using the ^ symbol to escape the parentheses without truncation of the error, but the command execution or no effect, the console also did not error:

"E:/test_rar_course^(xx^)/test.bat"

Here I do not know how to achieve my results, the only feeling qprocess how so difficult to use, if there is known qt God, please tell me. I think of another way to achieve, is to write a DLL in C + + implementation, and then call in Qt.

Invoking a DLL created by C + + in QT

The main code is actually called the system function, but if there are parentheses in the path, or if you need to escape with the ^ symbol, there is a problem with the system execution:

void Systemtool::generateindexrar (char * command) {    if (command = = NULL) return;    System (command);

But in addition to the parentheses to escape, there is a very uncomfortable problem, that is, every execution will pop up the cmd black window, after execution, the window disappears, the code is changed to the following:

#include <windows.h>void systemtool::generateindexrar (char * command) {         if (command = = NULL) return;          /**            winexec Windows calls, you can hide command-line black windows through parameters            Sw_hide and the path to the command is a          *         /WinExec (command,sw_hide) with parentheses;}

This perfectly solves the problem by not having to escape the parentheses and also hiding the black window. Of course, the above-mentioned Test.bat content to be generated dynamically in the program, using the appropriate path to replace the path in the Test.bat.

The last call is as follows:SystemTool::GenerateIndexRar("E:/test_rar_course(xx)/test.bat");

The painful experience of qprocess invoking command line in QT

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.