Using Qt to Develop for Embedded Linux

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

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(snakelist.at(i)) ) {
      emit dead();      // signal to end game
      autoMoveTimer->stop();
      return;
  }
}

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()) );

Conclusion

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 (nwatson04@optusnet.com.au) 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++.

______________________

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.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState