Home · Overviews · Examples 


Qt Tutorial 10 - Smooth as Silk



In this example, we add a force control.

Line by Line Walkthrough

t10/cannonfield.h

t10/cannonfield.h The CannonField now has a force value in addition to the angle. Missing snippet: tutorials/tutorial/t10/cannonfield.h. The interface to the force follows the same practice as for the angle. Missing snippet: tutorials/tutorial/t10/cannonfield.h. We have put the definition of the cannon's enclosing rectangle in a separate function. Missing snippet: tutorials/tutorial/t10/cannonfield.h. The force is stored in the currentForce variable.

t10/cannonfield.cpp

t10/cannonfield.cpp Missing snippet: tutorials/tutorial/t10/cannonfield.cpp. The force is initialized to zero. Missing snippet: tutorials/tutorial/t10/cannonfield.cpp. We have made a slight change in the setAngle() function. It repaints only the portion of the widget that contains the cannon. Missing snippet: tutorials/tutorial/t10/cannonfield.cpp. The implementation of setForce() is quite similar to that of setAngle(). The only difference is that because we don't show the force value, we don't need to repaint the widget. Missing snippet: tutorials/tutorial/t10/cannonfield.cpp. We paint as in Chapter 9. Missing snippet: tutorials/tutorial/t10/cannonfield.cpp. The cannonRect() function returns the rectangle enclosing the cannon in widget coordinates. First we create a rectangle with the size 50 x 50 and then move it so its bottom-left corner is equal to the widget's own bottom-left corner.

The QWidget::rect() function returns the widget's enclosing rectangle in the widget's own coordinates. The top-left corner of the rectangle is always (0, 0).

t10/main.cpp

t10/main.cpp Missing snippet: tutorials/tutorial/t10/main.cpp. The constructor is mostly the same, but some new bits have been added. Missing snippet: tutorials/tutorial/t10/main.cpp. Missing snippet: tutorials/tutorial/t10/main.cpp. We add a second LCDRange, which will be used to set the force. Missing snippet: tutorials/tutorial/t10/main.cpp. Missing snippet: tutorials/tutorial/t10/main.cpp. Missing snippet: tutorials/tutorial/t10/main.cpp. Missing snippet: tutorials/tutorial/t10/main.cpp. We connect the force widget and the cannonField widget, just like we did for the angle widget. Missing snippet: tutorials/tutorial/t10/main.cpp.
Missing snippet: tutorials/tutorial/t10/main.cpp. In Chapter 9, we put angle in the lower-left cell of the layout. Now we want to have two widgets in that cell, so we make a vertical box, put the vertical box in the grid cell, and put each of angle and range in the vertical box. Missing snippet: tutorials/tutorial/t10/main.cpp. We initialize the force value to 25.

Running the Application

We now have a force control.

Exercises

Make the size of the cannon barrel be dependent on the force.

Put the cannon in the bottom-right corner.

Try adding a better keyboard interface. For example, make + and - increase and decrease the force and enter shoot. If you're bothered by the way the Left and Right keys work, change that too. [Hint: Reimplement QWidget::keyPressEvent().]


Copyright © 2008 Trolltech Trademarks
Qt Jambi 4.4.0_01