Qt learning path (59): compiling cross-platform programs

Source: Internet
Author: User

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

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

Mainwindow. cpp

 
 
  1. #include <QProcess>  
  2. #include <QPushButton>  
  3. #include <QMessageBox>  
  4. #include <QTextCodec>  
  5.  
  6. #include "mainwindow.h"  
  7.  
  8. MainWindow::MainWindow(QWidget *parent)  
  9.     : QMainWindow(parent)  
  10. {  
  11.     p = new QProcess(this);  
  12.     QPushButton *bt = new QPushButton("display", this);  
  13.     connect(bt, SIGNAL(clicked()), this, SLOT(openProcess()));  
  14. }  
  15.  
  16. MainWindow::~MainWindow()  
  17. {  
  18.  
  19. }  
  20.  
  21. void MainWindow::openProcess()  
  22. {  
  23. #if defined(Q_OS_WIN32)  
  24.     p->start("cmd.exe", QStringList() << "/c" << "dir");  
  25. #elif defined(Q_OS_LINUX)  
  26.     p->start("ls", QStringList() << "/home/usr_name");  
  27. #endif  
  28.     connect(p, SIGNAL(finished(int)), this, SLOT(readResult(int)));  
  29. }  
  30.  
  31. void MainWindow::readResult(int exitCode)  
  32. {  
  33.     if(exitCode == 0) {  
  34. #if defined(Q_OS_WIN32)  
  35.         QTextCodec* gbkCodec = QTextCodec::codecForName("GBK");  
  36.         QString result = gbkCodec->toUnicode(p->readAll());  
  37. #elif defined(Q_OS_LINUX)  
  38.         QTextCodec* utfCodec = QTextCodec::codecForName("UTF-8");  
  39.         QString result = utfCodec->toUnicode(p->readAll());  
  40. #endif  
  41.         QMessageBox::information(this, "dir", result);  
  42.     }  
  43. }  

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

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.