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.

Webcast
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers

Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.

Learn More

Sponsored by AMD

White Paper
Red Hat White Paper: Using an Open Source Framework to Catch the Bad Guy

Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6

Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.

Learn more about catching the bad guy in this free white paper.

Learn More

Sponsored by DLT Solutions