Using Qt to Develop for Embedded Linux

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

At this stage you have a QMainWindow containing a QCanvasView with a canvas that contains items, some of which may be moving. The next step is to make your program more interesting by detecting collisions between the items. You can get information about collisions between QCanvasItems using collidesWith() and collisions(). collidesWith() is used to test if one item will collide with another particular item, and collisions() returns a list of all items with which a specific item will collide. In the Snake game, collisions() is used because the snake can collide with itself, the wall or the target. Listing 2 shows how collisions were handled in the game.

Listing 2. Handling Collisions

The list of QCanvasItems that the head of the snake collides with are assigned to a QCanvasItemList called ``l''. The false argument to collisions() specifies testing for collisions in inexact mode. This will return items that are near the head, but it works much faster than exact mode. If the item is a useful candidate, it is tested with collidesWith().

The ``for'' loop is used to iterate over the list l, assigning each item to a variable called ``item''. Each QCanvasItem on the canvas can have a specified rtti() value, an integer that allows you to identify the items. The target's rtti() value is 1,500, and this information was used in the if statement to test whether the item in the list was the target. If it was the target, the function returns; otherwise it moves on to the next if statement to test whether the item is the wall. Note that this code segment is for illustrative purposes only. In the actual Snake game, there are other if statements in the for loop to handle other situations. The ``do something'' is replaced with code.

Signals and Slots

Another important consideration when developing any program is the design. Qt offers assistance with component programming--otherwise known as object-oriented programming--through its signals and slots mechanism. This is a great feature that saves a lot of time and effort, as it allows easy communication among the different objects in your program. It allows an object to emit a signal() that activates a slot() in another object. Slots can be normal member functions, and parameters can be also be passed into them.

I found this feature particularly useful when developing Snake. An example of the use of signals and slots occurs when the game ends. The snake object emits a dead() signal when it collides with a wall. Then the snake's body, or the edges of the screen, connects to a gameOver() slot in the interface class to end the game and begin a new one. The code for this is in snake.cpp:

//check if snake hit itself
for (uint i = 3; i < snakelist.count(); i++) {
  if (head->collidesWith( ) {
      emit dead();      // signal to end game

And the code from file interface.cpp in the newGame() function is:

// connect the signal to the slot
connect(snake, SIGNAL(dead()), this, SLOT(gameOver()) );


If the test of an application framework is the ability to quickly build professional, powerful and bug-free code, then Qt/Embedded passes with flying colors. As an intern with fairly limited programming experience, I was able to create a 2-D computer game good enough to be included in the suite of applications for the Qt Palmtop Environment. And I did it in under five weeks of actual development time. The tagline for Qt/Embedded is ``Small Enough for the Job''. I think that's pretty accurate. It was powerful, with a rich set of functionalities that made programming very easy, and it took up an amazingly small amount of main memory and Flash. If the other tools I come across in my career are even half this good, I'll be one happy developer.

Natalie Watson ( is currently a student at Griffith University, where she is working toward a Bachelor degree in Information Technology. Her programming experience includes Java and C++.



Comment viewing options


Good Article

Luis Abarca's picture

Hey nice article !!

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


Geek Guide
The DevOps Toolbox

Tools and Technologies for Scale and Reliability
by Linux Journal Editor Bill Childers

Get your free copy today

Sponsored by IBM

Upcoming Webinar
8 Signs You're Beyond Cron

Scheduling Crontabs With an Enterprise Scheduler
11am CDT, April 29th
Moderated by Linux Journal Contributor Mike Diehl

Sign up now

Sponsored by Skybot