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.

Sprites

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
QCanvasPixmapArray
  setSequence(mouse);
  newTarget();
}

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();
       snake->go(newkey)
}

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;
    }
    detectCrash();
}
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.

______________________

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