QT Learning Pathway (58): Inter-process interaction (Qprocess.readallstandardoutput can read output from the console)

Source: Internet
Author: User
Tags svn version control system

The so-called IO is really just a data interaction with other devices. This concept may be clearer on Linux. Linux sees all devices as a file, so all IO comes down to the data interaction with the file. Similarly, there is data interaction with other processes, which is inter-process interaction.

Why do we need inter-process interaction? Although Qt is a very large library, but it can not be exhaustive. It is unrealistic to provide a solution for each requirement. For example, the operating system provides a command to view all files under the current folder (dir under Windows, ls under Linux), then Qt can get the information by invoking this command. Of course this is not a very good example, because Qt also provides the same operation. However, if you use a version control system, such as SVN, and then you want to build your system's build number through the SVN version, then you have to call the SVN command to get the version number of the current repository. These operations involve inter-process interaction.

Qt uses the Qprocess class to complete inter-process interactions. Let's start with the example. Because it is relatively simple, so did not put the main () function, everyone write down just fine!

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. Private
  13. Qprocess *p;
  14. };
  15. #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 ("notepad.exe");
  15. }

This window is simple and has only one button, and when you click the button, the program calls Windows Notepad. Here we are using the

    1. P->start ("notepad.exe");

Statement. Qprocess::start () accepts two parameters, the first is the command or program to execute, here is Notepad.exe; the second is a qstringlist type of data, which is the running parameter that needs to be passed to the program. Note that this program needs to be able to be found by the system and is generally a full path. But why is it only notepad.exe here? Because this program is actually placed under the Windows system folder, it is already added to the system path, so you do not need to add its own path.

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

QT Learning Pathway (58): Inter-process interaction (Qprocess.readallstandardoutput can read output from the console)

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.