Using Qt to Develop for Embedded Linux

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

You now need a class to set up the program. Its constructor will create an instance of the program, as used in the main method that appears above. It will inherit from QMainWindow and will create menus or toolbars and possibly a status bar. It also needs to create a QCanvas and a QCanvasView. All of this is shown in Listing 1.

Listing 1. Creating a QCanvas and QCanvasView

This class will also contain some other methods to be used in your program. In Snake, this class contains methods to display a title screen, start a new game, update the score, change levels, clear the screen, end the game and handle keyboard interaction (key events). These methods will be specific to your program, so I have not included the code here.


Now that you have a QCanvas, you can start placing QCanvasItems on it. There are many types of QCanvasItems, and to create a graphical item, we will use QCanvasSprite. See the Qt documentation for information on the other types of QCanvasItems. In the Snake game, the target mouse is a QCanvasSprite created from an image. The mouse is defined in its own class and inherits from QCanvasSprite. The following code shows the constructor for the mouse in a class called Target:

Target::Target(QCanvas* canvas)
   : QCanvasSprite(0, canvas)
       //inherits from QCanvasSprite
  mouse = new

The first thing this code does is load the image into a pixmap array called ``mouse''. If you had several images and wanted to animate your sprite, there is a readPixmaps() function to load all them into the array. You can then use the setSequence() function in QCanvasSprite to tell your sprite to use the images just loaded into the array. This constructor also calls a function newTarget(). This simply generates random integer x and y values at which the new target can be placed. It then calls the move (int x, int y) function in QCanvasSprite to set its position and show() to make it visible. The wall is also created in the same way.

Keyboard Events

Now you can make your program interactive. The Qt class keyEvent collects information about key events. The event handlers keyPressEvent and keyReleaseEvent receive this information. To use these, you will need to implement them as a function in your program. A typical implementation of keyPressEvent would be to use a switch statement to call functions when the user presses certain keys.

In the game, the snake changes direction when one of the arrow keys is pressed. This was implemented with a switch statement that calls a function move(), with the direction pressed as its argument. Here is the implementation of the keyPressEvent() function. It is quite simple because it calls a function in the Snake class that remembers the last key pressed and calls another function, moveSnake(), to determine which direction the snake should move in. To enact this, add these lines to the interface.cpp file:

void SnakeGame::keyPressEvent(QKeyEvent* event)
       int newkey = event->key();

To the snake.cpp file, add:

void Snake::moveSnake()
   switch (last)
      case Key_Up: move(up); break;
      case Key_Left: move(left);  break;
      case Key_Right: move(right); break;
      case Key_Down: move(down);  break;
If the user presses the Up key, the program calls move(up). The move function executes, and the snake moves in the direction of the key pressed: in this case, up. The code in the move() function will be specific to the particular game created.

The actual movement of the snake is quite complex because the snake's body is a collection of little images. To move, the end image is put at the front, and the head and tail pieces are moved. In other situations, such as pressing a key to shoot a bullet, the setVelocity() function in Qt could be used to set a constant speed at which the bullet will move.



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.


Free Dummies Books
Continuous Engineering


  • What continuous engineering is
  • How to continuously improve complex product designs
  • How to anticipate and respond to markets and clients
  • How to get the most out of your engineering resources

Get your free book now

Sponsored by IBM

Free Dummies Books
Service Virtualization

Learn to:

  • Define service virtualization
  • Select the most beneficial services to virtualize
  • Improve your traditional approach to testing
  • Deliver higher-quality software faster

Get your free book now

Sponsored by IBM