Using Qt to Develop for Embedded Linux

Qt/Embedded extends the ease and speed of Qt to embedded application development.

In January of 2001, I began an internship at Trolltech's embedded development office in Brisbane, Australia. My goal was twofold: to learn C++ and to learn how to develop embedded applications. I worked at Trolltech for a period of five weeks, which was long enough for me to achieve both goals (although truly mastering C++ takes a lifetime, of course). It should be noted that before starting at Trolltech, my prior programming experience consisted of one year of Java study at university, and I had never used C++ or Qt. At the end of five weeks, I had developed two complete 2-D games, both of which are now included in the Qt Palmtop Environment (QPE).

For those who don't know, Qt/Embedded is the embedded version of Trolltech's Qt, a cross-platform C++ GUI application framework that supports Windows, UNIX, Mac and embedded Linux. Qt and Qt/Embedded are available to developers under an elegant dual-license program. For programmers developing free software, Qt is available at no charge (under GPL and QPL; Qt/Embedded is available only under the GPL). For programmers developing commercial applications, the framework is available under a commercial license. Both commercial and open-source versions are available directly from Trolltech at http://www.trolltech.com/.

Getting Started

I believe anyone with basic programming skills can learn to use Qt and quickly develop useful, professional-looking applications. I found the API quite intuitive, which greatly shortened the learning time. While C++ can be overwhelming in terms of language textures, Qt uses a modest subset, so you don't have to be a C++ expert to use it.

This article explains techniques used to develop a simple game. Before attempting this, however, you should be familiar with four areas: the Qt tutorial, basic programming, object-oriented programming and Qt reference documentation.

The first few lessons of the Qt tutorial will take you through developing a simple application, such as a ``hello world'' program. They cover several specific skills, including the creation of source and header files, and how to compile and run a Qt program.

Qt is an application framework for C++, but I learned both the framework and basic programming at the same time. If you don't know C++, I would recommend a good textbook to use as a reference, especially to learn about pointers.

I had studied Java at university and found object-oriented programming techniques to be an excellent foundation for learning Qt. Qt is designed to make object-oriented programming easier, so familiarity with multiple classes, objects and other component programming techniques is helpful.

To use Qt you will need to ensure you have installed the necessary software and are familiar with the Qt reference documentation. A C++ compiler is necessary and a debugger is very useful, both of which are included in all Linux distributions. A text editor, such as vi or Emacs, is also required for creating source files. Most importantly, you will need to install Qt for X11 and for embedded Linux.

The tools I used to create my Qt/Embedded applications come with most Linux distributions, including GCC and GDB. I also used TMake, a simple tool from Trolltech to create and maintain makefiles; the Qt documentation (available at http://doc.trolltech.com/); and a Qt textbook, Kalle Dalheimer's Programming with Qt. Features in the Qt documentation that you should look at include an explanation of how Qt makes it very easy to create simple applications. Its API contains several classes that are designed to make common tasks much easier and faster.

In particular, for creating a 2-D game, you should look at three main features to get started: QMainWindow, which provides a typical application window complete with toolbars or menus and status bars; QCanvas, a 2-D graphic area on which QCanvasItems (graphical objects) can be placed; and QCanvasView, which provides a view of the canvas. Looking at the Qt documentation is a good starting point because it will give you an idea of the rich functionality that Qt offers. Also, many things you will want to do have already been done.

This next portion of this article highlights Qt features that make it easy to create a simple game, using example code from a Snake game I have written (see Figure 1). The principles explained here can be applied to create many other 2-D games.

MainWindow

The first thing you will need is a main class. This should be located in a file called main.cpp. It will contain only a main method that creates an instance of your program. The main method from Snake appears below.

int main(int argc, char **argv)
{
   QPEApplication app(argc,argv);
   SnakeGame* m = new SnakeGame;
      // creates an instance of Snake
   m->resize(m->sizeHint());
   qApp->setMainWidget(m);
   m->show();
   return app.exec();
}

If you have already written Qt programs, you will find some of this code familiar. It is a standard way to start a program.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Good Article

Luis Abarca's picture

Hey nice article !!

Muy buen articulo, me despejaste varias dudas que tenia al respecto.

Saludos.

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix