The painful experience of qprocess invoking command line in QT

Source: Internet
Author: User

Read Catalogue

    • How to create a compressed package
    • Invoking the command line in QT
    • Invoking a DLL created by C + + in QT

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.

Back to top 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.

Back to top call 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:

1234 QProcess p(0);p.start(command,args); //command是要执行的命令,args是参数p.waitForFinished();//等待完成qDebug()<<QString::fromLocal8Bit(p.readAllStandardError());

Apply the above code to get the following:

123456789101112131415 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是要执行的命令,args是参数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:

1234567891011121314151617 QProcess p(0);p.setWorkingDirectory("E:/test_rar_course/");//指定进程的工作目录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/");//指定rar.exe的工作目录args.append("test.rar");args.append("direct1/");args.append("direct2/");args.append("test.txt");p.execute(command,args);//command是要执行的命令,args是参数p.waitForFinished();qDebug()<<QString::fromLocal8Bit(p.readAllStandardError());//获取输出

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:

123456 QProcess p(0);p.setWorkingDirectory("E:/test_rar_course/");//指定进程的工作目录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:

12 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.txt

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.

Back to top the DLL created in Qt called C + +

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:

12345 voidSystemTool::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:

12345678910 #include <windows.h>voidSystemTool::GenerateIndexRar(char* command){         if(command == NULL) return;         /**            WinExec 的windows 调用,可以通过参数SW_HIDE隐藏命令行黑窗口            并且命令的路径是可以带括号的          */         WinExec(command,SW_HIDE);}

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");

Http://www.cnblogs.com/wangqiguo/p/4609228.html

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.