![]() |
Home · Overviews · Examples |
Its tasks are to compute the new position, update the screen with the shot in the new position, and if necessary, stop the timer.
First we make a QRegion that holds the old shotRect(). A QRegion is capable of holding any sort of region, and we'll use it here to simplify the painting. shotRect() returns the rectangle where the shot is now. It is explained in detail later.
Then we increment the timerCount, which has the effect of moving the shot one step along its trajectory.
Next we fetch the new shot rectangle.
If the shot has moved beyond the right or bottom edge of the widget we stop the timer, or we add the new shotRect() to the QRegion.
Finally, we repaint the QRegion. This will send a single paint event for just the one or two rectangles that need updating. Missing snippet: tutorials/tutorial/t11/cannonfield.cpp. The paint event function has been simplified since the previous chapter. Most of the logic has been moved to the new paintShot() and paintCannon() functions. Missing snippet: tutorials/tutorial/t11/cannonfield.cpp. Missing snippet: tutorials/tutorial/t11/cannonfield.cpp. This private function paints the shot by drawing a black filled rectangle.
We leave out the implementation of paintCannon(); it is the same as the QWidget::paintEvent() reimplementation from the previous chapter. Missing snippet: tutorials/tutorial/t11/cannonfield.cpp. Missing snippet: tutorials/tutorial/t11/cannonfield.cpp. This private function calculates the center point of the shot and returns the enclosing rectangle of the shot. It uses the initial cannon force and angle in addition to timerCount, which increases as time passes.
The formula used is the standard Newtonian formula for frictionless movement in a gravity field. For simplicity, we've chosen to disregard any Einsteinian effects.
We calculate the center point in a coordinate system where y coordinates increase upward. After we have calculated the center point, we construct a QRect with size 6 x 6 and move its center point to the point calculated above. In the same operation we convert the point into the widget's coordinate system (see The Coordinate System).
The qRound() function is an inline function defined in <QtGlobal> (included by all other Qt header files). qRound() rounds a double to the closest integer.t11/main.cpp
t11/main.cpp
Missing snippet: tutorials/tutorial/t11/main.cpp.
The only addition is the Shoot button.
Missing snippet: tutorials/tutorial/t11/main.cpp.
In the constructor we create and set up the Shoot button exactly like we did with the Quit button.
Missing snippet: tutorials/tutorial/t11/main.cpp.
Connects the clicked() signal of the Shoot button to the shoot() slot of the CannonField.Running the Application
The cannon can shoot, but there's nothing to shoot at.Exercises
Make the shot a filled circle. [Hint: QPainter::drawEllipse() may help.]
Change the color of the cannon when a shot is in the air.
Copyright © 2008 Trolltech | Trademarks | Qt Jambi 4.4.0_01 |