Using Qt to Develop for Embedded Linux
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.
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()) );
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++.
email: nwatson04@optusnet.com.au
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
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.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- New Products
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- Validate an E-Mail Address with PHP, the Right Way
- Tech Tip: Really Simple HTTP Server with Python
- New Products
- Trying to Tame the Tablet
- git-annex assistant
3 hours 34 min ago - direct cable connection
3 hours 56 min ago - Agreed on AirDroid. With my
4 hours 7 min ago - I just learned this
4 hours 11 min ago - enterprise
4 hours 41 min ago - not living upto the mobile revolution
7 hours 32 min ago - Deceptive Advertising and
8 hours 8 min ago - Let\'s declare that you have
8 hours 9 min ago - Alterations in Contest Due
8 hours 10 min ago - At a numbers mindset, your
8 hours 11 min ago



Comments
Good Article
Hey nice article !!
Muy buen articulo, me despejaste varias dudas que tenia al respecto.
Saludos.