1 / 17

Cross Platform Development with Qt

Cross Platform Development with Qt. November 15, 2011 Lloyd Moore, President/Owner. Overview. Available Platforms Installation Directory Structures Qt Classes and Data Types Handling Platform Specifics Best Practices Summary. Available Platforms. Desktop

jeneva
Download Presentation

Cross Platform Development with Qt

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cross Platform Development with Qt November 15, 2011 Lloyd Moore, President/Owner

  2. Overview • Available Platforms • Installation • Directory Structures • Qt Classes and Data Types • Handling Platform Specifics • Best Practices • Summary

  3. Available Platforms • Desktop • Windows (XP, Vista, Vista 64 bit, Win7, Win7 64 bit) • GCC 4.4/MinGW, VS2005, VS2008, VS2010 • Linux / X11 (32 and 64 bit) • GCC 4.4, Intel • MAC OS X (10.5/”Leopard”, 10.6/”Snow Leopard”) • xCode / LLVM • MeeGo (X86, ARM) • GCC (Scratchbox) • Mobile/Embedded • Windows CE/Mobile (5.x, 6.x) • MSVC 2005, MSVC 2008 • Embedded Linux (ARM) • GCC (Codesourcery) • Symbian • RVCT2.2, WINSCW3.25, GCCE • Maemo 5 • GCC (Scratchbox) • Android • Community development effort

  4. Installation Will vary some what based on platform you are using Will focus on Windows , Ubuntu and Mac as they are common and serve as the desktop environments for others http://qt.nokia.com/downloads/ has the installers for each platform Online and offline versions Libraries are included in the main download and can be selected from the Update / Package Manager tool:

  5. Directory Structures • Note: This is primarily a Windows example, other platforms are similar • SDK Root directory installs in C:\QtSDK by default • In this directory are: • Documentation: Demos, Examples and Documentation • Tools: QtCreator, mingw, pythongdb, Simulator • Platforms: Desktop, Symbian, etc Each platform directory is unique but typically contains: • Additional tools such as simulators and cross compilers • Platform libraries • Multiple versions for both platform and supported compilers • Windows has two branches: …/Desktop/Qt/4.74/mingw/… & …/Desktop/Qt/4.74/msvc2008/… • Symbian has several branches: …/Symbian/SDKs/Symbian1Qt473/… & …/Symbian/SDKs/Symbian3Qt474/… & others of the same format.

  6. Qt Classes for Common Operations • Qt provides an abstraction layer for each platform service and resource • In those cases where the underlying platform does not provide a given service directly, Qt provides a replacement • QWS on Qt Embedded replaces the windowing system on desktop (X11, GWES) • Use the APIs available from Qt whenever possible, otherwise you will not be isolated from the underlying operating system! • Commonly Used Classes/Resources (note: this list is not exhaustive) • File System: QFile, QTemporaryFile, QFileInfo, QFileSystemModel • Networking: QNetworkAccessManager, QNetworkProxy, QNetworkInterface • Images: QImage, QPixmap, QMovie, QImageReader, QPalette • Threading: QThread, QMutex, QWaitCondition • Printing: QPrinter, QPrintEngine, QPrinterInfo • Sound: QSound • Dialogs: QFileDialog, QMessageBox, QPrintPreviewDialog, QColorDialog • Each platform displays properly to match the GUI guidelines

  7. Qt Data Types Qt has a set of typedef aliases for data types to “hide” the native C/C++ types • qint8, qint16, qint32, qint64, qlonglong, qreal, quint8, quint16, quint32, qulonglong, uchar, uint, ulong, ushort Generally these add more confusion than they solve • Ok to stick with the native C/C++ types, especially if you know all your target platforms are 32/64 bit native qrealin particular can be misleading • From the Qt documentation: “Typedef for double on all platforms except for those using CPUs with ARM architectures. On ARM-based platforms, qreal is a typedef for float for performance reasons.” • If doing certain types of math (Newton approximations, convergence, etc.) the algorithm may fail completely in single precision • Recommend sticking with float / double here so you specifically know what you have

  8. Wide Character Data Types If developing with wide characters ensure that all libraries and dependencies agree on the data type being used wchar_t can and will map to multiple types – mostly an issue with Visual Studio • unsigned short is the most common standard • Visual Studio keeps this as wchar_t by default, but can also be set to use _wchar_t as a native type which is the recommended practice • Problems will show up at link time as function signature mis-matches Possible solutions • Rebuild target library to match Qt libraries • Rebuild Qt libraries to match target libraries • Provide an interface layer of macros, wrappers, or use a Façade design pattern to keep compiler/linker happy • Can be VERY light weight or no weight as the underlying types are all unsigned 16 bit values MSDN link: http://msdn.microsoft.com/en-us/library/dh8che7s.aspx

  9. Handling Platform Specifics • Use these methods only when you have to, will necessarily make your code less portable and require you to do something specific for EVERY platform • General guideline: You will need to use these techniques when the platform provides some unique resource that has not been generalized into Qt • Qt Mobility provides additional abstractions for typical sensors such as accelerometers, web cams, PIM functionality, messaging, location services • Also look for 3rd party add-ons that provide additional functionality: • QextSerialPort – Provides platform independent serial port • OpenCV integrates with Qt and also abstracts GPU access in many cases • C/C++ standard libraries may also be helpful depending on your requirements • If all else fails you have two general mechanisms at your disposal: • Platform specific project file settings • Conditional compilation macros

  10. Platform Specific Project File Settings • Qmake provides for conditional inclusion based on project type, called “scopes” • The general format is: <platform>:<statement> ie: win32: RC_FILE +=re.res • Block format: Example: <platform> { win32 { <statements> DEFINES += WINDOWS_BUILD }INCLUDEPATH += c:\mingw\include else <platform> { LIBS += c:\mingw\libs <statements> } } else macx { else { DEFINES += MAC_BUILD <statements> } } else unix { DEFINES += LINUX_BUILD } else { DEFINES += UNKNOWN_BUILD } • Mac is also a Unix platform so needs to be placed above “unix” in the chain • Note: Settings values are passed to platform specific tools, and may need to be formatted differently for different tools

  11. Conditional Compilation • Use symbols defined in the .pro file to include or exclude various code using the C/C++ preprocessor. #ifdef WINDOWS_BUILD #include “windows.h” #endif Numerous forms and variations of this pattern. Common symbols (full list at:http://doc.qt.nokia.com/latest/qtglobal.html ) Operating Systems:Compilers: Windows: Q_OS_WIN32 Visual Studio: Q_CC_MSVC Windows CE: Q_OS_WINCE Intel: Q_CC_INTEL Linux: Q_OS_LINUX Gnu: Q_CC_GNU Mac: Q_OS_MAC Symbian: Q_OS_SYMBIAN

  12. Best Practices:Structuring Platform Dependencies • Isolate any platform dependent code to separate modules and make the structure symmetric across all platforms. • Use the “Strategy” design pattern along with an “Abstract Factory” to abstract platform difference when absolutely necessary • Additionally incorporate a “Façade” pattern to simplify the API if necessary • See “Design Patterns, Elements of Reusable Object-Oriented Software”; Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides; ISBN: 0-201-63361-2 for additional detail on these design patterns

  13. Best Practices: Development Environment • Use virtual machines to keep the number physical installations to a minimum and to allow for portability • For more advanced projects you may also find that some configurations interfere with one another or are exclusive. VMs can also be used to isolate these environments • May also be able to hold source code in a shared location, simplifying synchronization • Ensure that ALL development environments are using the SAME version of Qt at the start of your project (Creator, Designer, Linguist, Libraries, etc.) • Use a cloud or network based revision control system to synchronize code between various development platforms • Use the “shadow build” option for each build configuration. This will create a separate output directory for each build at the same level as your project code • Lower case file and directory names • Do your primary development on a case sensitive platform (Linux)

  14. Best Practices:Handling Resources • Ensure that the fonts being used by your application are included on all available platforms. Qt also supplies a set of fonts, all start with DejaVu. • Use the Qt resource bundles to package all of your resources with the executable. This will simplify the creation of installers for each platform. • Windows executable icons still need to be compiled by the Windows Resource Compiler • Create a Windows standard .rc file • Simple way to get one is to copy from existing Visual Studio project • Add “win32:RC_FILE = my_resources.rc” the .pro file to add to build • Will call rc.exe to compile • Other operating systems handle this at program install time

  15. Summary • Qt is unique in its ability to target many platforms • You must follow good development practices to fully realize the cross platform portability that Qt offers • Prefer Qt APIs to native APIs whenever possible, and fully isolate anything that is not provided by Qt based on your target platforms • Virtual machines and network based code repositories can simplify the management of code across multiple platforms • Be aware of platform specifics, and ensure additional libraries are compatible early in project planning

  16. Resources • Books • C++ GUI Programming with Qt4; JasminBlanchette & Mark Summerfield; ISBN: 0132354160 • Foundations of Qt Development; Johan Thelin; ISBN: 1590598318 • Design Patterns, Elements of Reusable Object-Oriented Software”; Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides; ISBN: 0201633612 • Qt Installer Downloads • http://qt.nokia.com/downloads/ • Online Documentation (Version 4.7) • http://doc.qt.nokia.com/4.7/index.html • My Contact Info: • Lloyd@CyberData-Robotics.com • http://www.CyberData-Robotics.com

  17. Questions???? • Will be around a bit after the meeting for individual questions • Feel free to e-mail me

More Related