Using Qt environment macros to differentiate platforms

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData Template:ArticleNeedsUpdate

Overview[edit | edit source]

This code snippet demonstrates how to write platform-specific code using the 76ytuiytuityutyutututyutyutyu, 76ytuiytuityutyutututyutyutyu, and 76ytuiytuityutyutututyutyutyu environment macros. You can use the macros with Qt code if you need to, for example, define a different screen size for each platform, or use each platform's native language (for example, Symbian C++ in Symbian and C in Maemo), and still be able to build the project for all targets. Note that in the latter case you need to declare in the project file which platform-specific libraries you use.

In the project file, platform-specific code must be placed in scopes. For Symbian, use 76ytuiytuityutyutututyutyutyu; for Maemo, use 76ytuiytuityutyutututyutyutyu; and for Simulator, use 76ytuiytuityutyutututyutyutyu. Note that scope 76ytuiytuityutyutututyutyutyu is valid for both Simulator and Windows (desktop target) so when writing code only for Windows use scope 76ytuiytuityutyutututyutyutyu.

Preconditions[edit | edit source]

None.

Source[edit | edit source]

platformlabel.pro QT += core gui

TARGET = platformlabel TEMPLATE = app

SOURCES += main.cpp

symbian {

   # To lock the application orientation
   LIBS += -lcone -leikcore -lavkon

}

main.cpp

  1. include <QApplication>
  2. include <QLabel>
  1. ifdef Q_OS_SYMBIAN

// Need these includes to lock orientation in Symbian

  1. include <eikenv.h>
  2. include <eikappui.h>
  3. include <aknenv.h>
  4. include <aknappui.h>
  5. endif

int main(int argc, char *argv[]) {

   QApplication app(argc, argv);
   QLabel label;
  1. ifdef Q_OS_SYMBIAN
   // Lock orientation to portrait in Symbian
   CAknAppUi* appUi = dynamic_cast<CAknAppUi*> (CEikonEnv::Static()->AppUi());
   TRAP_IGNORE(
       if(appUi) {
           appUi->SetOrientationL(CAknAppUi::EAppUiOrientationPortrait);
       }
   );
  1. endif
  1. ifdef Q_WS_MAEMO_5
   // Lock orientation to portrait in Maemo
   label.setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
  1. endif
  1. if defined(Q_OS_SYMBIAN)
   // Symbian^3 specific code can be defined using the SV_S60_5_2 macro
   if (QSysInfo::s60Version() == QSysInfo::SV_S60_5_2) {
       label.setText("Symbian^3");
   }
   else {
       label.setText("Symbian");
   }
  1. elif defined(Q_WS_MAEMO_5)
   label.setText("Maemo");
  1. elif defined(Q_WS_SIMULATOR)
   label.setText("Simulator");
  1. else
   label.setText("Some other platform");
  1. endif
  1. if defined(Q_OS_SYMBIAN)
   label.showMaximized();
  1. else
   // On Maemo and Simulator QMainWindow (to which the only QLabel will map behind the scene)
   // is automatically shown as maximised and we don't need to ifdef platform specific code
   // for it.
   label.show();
  1. endif
   return app.exec();

}

Postconditions[edit | edit source]

The code snippet demonstrated the use of environment macros to write platform-specific code.