Call a system command and read its output value (using Qprocess.readall)

Source: Internet
Author: User

Let's look at a more complex example, invoking a system command, where I'm using Windows, so I need to call dir; if you're compiling on Linux, you need to change to LS.

Mainwindow.h

  1. #ifndef Mainwindow_h
  2. #define Mainwindow_h
  3. #include <QtGui>
  4. Class MainWindow: Public Qmainwindow
  5. {
  6. Q_object
  7. Public
  8. MainWindow (Qwidget *parent = 0);
  9. ~mainwindow ();
  10. Private Slots:
  11. void OpenProcess ();
  12. void Readresult (int exitCode);
  13. Private
  14. Qprocess *p;
  15. };
  16. #endif//Mainwindow_h

Mainwindow.cpp

  1. #include "Mainwindow.h"
  2. Mainwindow::mainwindow (Qwidget *parent)
  3. : Qmainwindow (parent)
  4. {
  5. p = New qprocess (this);
  6. Qpushbutton *BT = new Qpushbutton ("Execute Notepad", this );
  7. Connect (BT, SIGNAL (clicked ()), this , SLOT (OpenProcess ()));
  8. }
  9. Mainwindow::~mainwindow ()
  10. {
  11. }
  12. void Mainwindow::openprocess ()
  13. {
  14. P->start ("cmd.exe", Qstringlist () << "/C" << "dir");
  15. Connect (p, SIGNAL (finished (int)), this , SLOT (readresult (int)));
  16. }
  17. void Mainwindow::readresult (int exitCode)
  18. {
  19. if (ExitCode = = 0) {
  20. qtextcodec* Gbkcodec = Qtextcodec::codecforname ("GBK");
  21. QString result = Gbkcodec->tounicode (P->readall ());
  22. Qmessagebox::information (This, "dir", result);
  23. }
  24. }

We have added only one slot function. In the slot where the button is clicked, we run the instruction through the Qprocess::start () function

cmd.exe/c dir

Here is to say, open the system cmd program and then run the dir command. If you have any questions about the parameter/C, you have to consult the relevant manual for Windows. Oh, that's not a Qt problem anymore. Then our process's finished () signal is connected to the newly added slot. This signal has a parameter int. As we know, the main () function always returns an int, the exit code, for A/C + + program to indicate whether the program exits normally. The int parameter here is the exit code. In the slot, we check if the exit code is 0, generally, if the exit code is 0, the description is normal exit. The results are then displayed in the Qmessagebox. How do we do that? Originally, the qprocess::readall () function can read out the program output. We use this function to get all the output, and since it returns the Qbytearray type, it is then converted to QString display. Also note that in the text Windows uses GBK encoding, and Qt uses Unicode encoding, so need to do a conversion, otherwise there will be garbled, you can try.

Well, interprocess interaction says so, by looking at the document you can find out how to use qprocess to implement process monitoring, or a function that allows the QT program to wait for the process to finish before continuing.

This article is from the "Bean Space" blog, make sure to keep this source http://devbean.blog.51cto.com/448512/305116

Call a system command and read its output value (using Qprocess.readall)

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.