A friend complained in the comments that the examples in the previous articles were for Windows, and there was no for Linux or for Mac. Let's talk about cross-platform issues today!
The so-called cross-platform has two meanings: one is cross-hardware platform and the other is cross-software platform. For hardware platforms, we often do not consciously ignore them, because although the hardware differences are large, we have little access to them. Currently, the PC series are basically compatible, and the compiler may help us solve this problem. Therefore, if your program does not use the assembly language, it is difficult to consider this cross-platform support. However, if your program needs to be exposed to hardware, you have to consider this issue either because of functional or performance requirements. For example, the color processing of Photoshop is directly written in assembly language to achieve the best performance. Therefore, you have to consider hardware-related issues. We usually say that cross-platform, more software-only cross-platform, that is, cross-operating system. Currently, the mainstream operating systems Windows and Linux/Unix are basically regarded as two camps. The software between them is incompatible, so we have a lot of contact with such cross-platforms.
Speaking of cross-platform, we have to mention Java. This is one of the selling points of Java: "One write, run everywhere ". The reason why Java can implement cross-platform is that the Java source code is compiled into an intermediate code and the Java program is actually in JVM. The Java program you compile is cross-platform, but the JVM is not cross-platform. You must select JVM based on your operating system. This is a typical application of the adapter mode :-)
If you select a cross-platform library, your workload will be much smaller, such as Qt. Qt has helped us encapsulate many platform-related code. If you open the source code of a Qt file, you may find many operating system judgments. In short, we can use QMainWindow: show () to display a window. On Windows, Qt must call the Win32 API. on Linux, you may need to call the GNOME or kde api. However, in any case, this part of code is not our concern, because Qt has already completed for us. Therefore, if your program does not have any platform-related code, you only need to compile it successfully on Windows, and then re-compile it on Linux to pass some simple tests, or you need to adjust the UI proportion and so on to release it.
However, if some code has to depend on the operating system, for example, we call the command to list directories, Which is dir in Windows and ls in Linux, We have to compile the code based on the platform. Here we will try this example.
Mainwindow. h
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
-
- #include <QMainWindow>
-
- class QProcess;
-
- 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 <QProcess>
- #include <QPushButton>
- #include <QMessageBox>
- #include <QTextCodec>
-
- #include "mainwindow.h"
-
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- {
- p = new QProcess(this);
- QPushButton *bt = new QPushButton("display", this);
- connect(bt, SIGNAL(clicked()), this, SLOT(openProcess()));
- }
-
- MainWindow::~MainWindow()
- {
-
- }
-
- void MainWindow::openProcess()
- {
- #if defined(Q_OS_WIN32)
- p->start("cmd.exe", QStringList() << "/c" << "dir");
- #elif defined(Q_OS_LINUX)
- p->start("ls", QStringList() << "/home/usr_name");
- #endif
- connect(p, SIGNAL(finished(int)), this, SLOT(readResult(int)));
- }
-
- void MainWindow::readResult(int exitCode)
- {
- if(exitCode == 0) {
- #if defined(Q_OS_WIN32)
- QTextCodec* gbkCodec = QTextCodec::codecForName("GBK");
- QString result = gbkCodec->toUnicode(p->readAll());
- #elif defined(Q_OS_LINUX)
- QTextCodec* utfCodec = QTextCodec::codecForName("UTF-8");
- QString result = utfCodec->toUnicode(p->readAll());
- #endif
- QMessageBox::information(this, "dir", result);
- }
- }
The example is the same as above. We just added some code to the cpp file. We use the # if preprocessing commands to generate the corresponding platform-related code by judging whether a macro such as Q_ OS _WIN32 exists. These macros are defined in Qt. We only need to judge whether they exist. In this way, our program can be compiled and run in both Windows and Linux.
For cross-platform programming, you have to be familiar with platform-related things, such as text character encoding. Therefore, it is recommended that you use the standard functions provided by Qt for programming as much as possible, so that you do not have to write a lot of if judgments, and the code is clearer.
This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/308358