Qt learning path (58): Interaction Between processes

Source: Internet
Author: User
Tags windows manual

IO is actually a data interaction with other devices. In Linux, this concept may be clearer. Linux regards all devices as a file, so all IO is attributed to data interaction with files. Similarly, there is data interaction with other processes. This is the interaction between processes.

Why is interaction between processes required? Although Qt is a huge library, it cannot cover all aspects. It is unrealistic to provide a solution for each requirement. For example, if the operating system provides a command to view all files in the current folder (dir in Windows and ls in Linux), Qt can obtain the information by calling this command. Of course, this is not an appropriate example, because Qt also provides the same operation. However, if you use a version control system, such as SVN, and you want to generate the build number of your system through the SVN version number, you have to call the svn command to obtain the version number of the current repository. These operations involve interaction between processes.

Qt uses the QProcess class to complete interaction between processes. We can start from the example. Because it is relatively simple, I didn't paste the main () function. You can just write it down!

Mainwindow. h

 
 
  1. #ifndef MAINWINDOW_H  
  2. #define MAINWINDOW_H  
  3.  
  4. #include <QtGui>  
  5.  
  6. class MainWindow : public QMainWindow  
  7. {  
  8.     Q_OBJECT  
  9.  
  10. public:  
  11.     MainWindow(QWidget *parent = 0);  
  12.     ~MainWindow();  
  13.  
  14. private slots:  
  15.     void openProcess();  
  16.  
  17. private:  
  18.     QProcess *p;  
  19. };  
  20.  
  21. #endif // MAINWINDOW_H 

Mainwindow. cpp

 
 
  1. #include "mainwindow.h"  
  2.  
  3. MainWindow::MainWindow(QWidget *parent)  
  4.     : QMainWindow(parent)  
  5. {  
  6.     p = new QProcess(this);  
  7.     QPushButton *bt = new QPushButton("execute notepad", this);  
  8.     connect(bt, SIGNAL(clicked()), this, SLOT(openProcess()));  
  9. }  
  10.  
  11. MainWindow::~MainWindow()  
  12. {  
  13.  
  14. }  
  15.  
  16. void MainWindow::openProcess()  
  17. {  
  18.     p->start("notepad.exe");  

This window is very simple. There is only one button. After you click the button, the program will call the Windows notepad. Here we use

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

Statement. QProcess: start () accepts two parameters. The first is the command or program to be executed. Here it is notepad.exe; the second is the data of the QStringList type, that is, the running parameters that need to be passed to this program. Note that this program needs to be found by the system, generally the full path. But why is there only notepad.exe here? Because this program is actually placed in the Windows system folder, it has been added to the system path, so you do not need to add its own path.

Next let's look at a more complex example. Call a system command. Here I am using Windows, so I need to call dir. If you compile in Linux, you need to change it to ls.

Mainwindow. h

 
 
  1. #ifndef MAINWINDOW_H  
  2. #define MAINWINDOW_H  
  3.  
  4. #include <QtGui>  
  5.  
  6. class MainWindow : public QMainWindow  
  7. {  
  8.     Q_OBJECT  
  9.  
  10. public:  
  11.     MainWindow(QWidget *parent = 0);  
  12.     ~MainWindow();  
  13.  
  14. private slots:  
  15.     void openProcess();  
  16.     void readResult(int exitCode);  
  17.  
  18. private:  
  19.     QProcess *p;  
  20. };  
  21.  
  22. #endif // MAINWINDOW_H 

Mainwindow. cpp

 
 
  1. #include "mainwindow.h"  
  2.  
  3. MainWindow::MainWindow(QWidget *parent)  
  4.     : QMainWindow(parent)  
  5. {  
  6.     p = new QProcess(this);  
  7.     QPushButton *bt = new QPushButton("execute notepad", this);  
  8.     connect(bt, SIGNAL(clicked()), this, SLOT(openProcess()));  
  9. }  
  10.  
  11. MainWindow::~MainWindow()  
  12. {  
  13.  
  14. }  
  15.  
  16. void MainWindow::openProcess()  
  17. {  
  18.     p->start("cmd.exe", QStringList() << "/c" << "dir");  
  19.     connect(p, SIGNAL(finished(int)), this, SLOT(readResult(int)));  
  20. }  
  21.  
  22. void MainWindow::readResult(int exitCode)  
  23. {  
  24.     if(exitCode == 0) {  
  25.         QTextCodec* gbkCodec = QTextCodec::codecForName("GBK");  
  26.         QString result = gbkCodec->toUnicode(p->readAll());  
  27.         QMessageBox::information(this, "dir", result);  
  28.     }  
  29. }  

We added only one slot function. In the slot clicked by the button, we run the command through the QProcess: start () function.

Cmd.exe/c dir

In this example, open the cmd program of the system and run the dir command. If you have any questions about the parameter/c, you have to go to the relevant Windows manual. This is no longer a problem with Qt. Then we connect the finished () signal of process to the newly added slot. This signal has an int parameter. We know that for C/C ++ programs, the main () function always returns an int, that is, the exit code, to indicate whether the program Exits normally. The int parameter here is the exit code. In the slot, check whether the exit code is 0. In general, if the exit code is 0, it indicates that the exit code is normal. Then, the result is displayed in QMessageBox. How can we do this? Originally, the QProcess: readAll () function can read the output content of the program. After we use this function to obtain all the output, the returned result is of the QByteArray type, so we can convert it to QString for display. Note that in Windows, the Chinese text uses GBK encoding, while Qt uses Unicode encoding. Therefore, a conversion is required. Otherwise, garbled characters may occur. You can try again.

Now, let's talk about the interaction between processes. By viewing the document, you can find a function that monitors the process using QProcess or allows the Qt program to continue execution after the process ends.

This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/305116

Related Article

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.