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
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
-
- #include <QtGui>
-
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
-
- public:
- MainWindow(QWidget *parent = 0);
- ~MainWindow();
-
- private slots:
- void openProcess();
-
- private:
- QProcess *p;
- };
-
- #endif // MAINWINDOW_H
Mainwindow. cpp
- #include "mainwindow.h"
-
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- {
- p = new QProcess(this);
- QPushButton *bt = new QPushButton("execute notepad", this);
- connect(bt, SIGNAL(clicked()), this, SLOT(openProcess()));
- }
-
- MainWindow::~MainWindow()
- {
-
- }
-
- void MainWindow::openProcess()
- {
- 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
- 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
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
-
- #include <QtGui>
-
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
-
- public:
- MainWindow(QWidget *parent = 0);
- ~MainWindow();
-
- private slots:
- void openProcess();
- void readResult(int exitCode);
-
- private:
- QProcess *p;
- };
-
- #endif // MAINWINDOW_H
Mainwindow. cpp
- #include "mainwindow.h"
-
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- {
- p = new QProcess(this);
- QPushButton *bt = new QPushButton("execute notepad", this);
- connect(bt, SIGNAL(clicked()), this, SLOT(openProcess()));
- }
-
- MainWindow::~MainWindow()
- {
-
- }
-
- void MainWindow::openProcess()
- {
- p->start("cmd.exe", QStringList() << "/c" << "dir");
- connect(p, SIGNAL(finished(int)), this, SLOT(readResult(int)));
- }
-
- void MainWindow::readResult(int exitCode)
- {
- if(exitCode == 0) {
- QTextCodec* gbkCodec = QTextCodec::codecForName("GBK");
- QString result = gbkCodec->toUnicode(p->readAll());
- QMessageBox::information(this, "dir", result);
- }
- }
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