Qt Tutorial 10 - Smooth as SilkFiles:
In this example, we add a force control. Line by Line Walkthrought10/cannonfield.hThe CannonField now has a force value in addition to the angle. int angle() const { return currentAngle; } int force() const { return currentForce; } public slots: void setAngle(int angle); void setForce(int force); signals: void angleChanged(int newAngle); void forceChanged(int newForce); The interface to the force follows the same practice as for the angle. private: QRect cannonRect() const; We have put the definition of the cannon's enclosing rectangle in a separate function. int currentAngle; int currentForce; }; The force is stored in the currentForce variable. t10/cannonfield.cppCannonField::CannonField(QWidget *parent) : QWidget(parent) { currentAngle = 45; currentForce = 0; setPalette(QPalette(QColor(250, 250, 200))); setAutoFillBackground(true); } The force is initialized to zero. void CannonField::setAngle(int angle) { if (angle < 5) angle = 5; if (angle > 70) angle = 70; if (currentAngle == angle) return; currentAngle = angle; update(cannonRect()); emit angleChanged(currentAngle); } We have made a slight change in the setAngle() function. It repaints only the portion of the widget that contains the cannon. void CannonField::setForce(int force) { if (force < 0) force = 0; if (currentForce == force) return; currentForce = force; emit forceChanged(currentForce); } 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. void CannonField::paintEvent(QPaintEvent * /* event */) { QPainter painter(this); painter.setPen(Qt::NoPen); painter.setBrush(Qt::blue); painter.translate(0, height()); painter.drawPie(QRect(-35, -35, 70, 70), 0, 90 * 16); painter.rotate(-currentAngle); painter.drawRect(QRect(30, -5, 20, 10)); } We paint as in Chapter 9. QRect CannonField::cannonRect() const { QRect result(0, 0, 50, 50); result.moveBottomLeft(rect().bottomLeft()); return result; } 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.cppMyWidget::MyWidget(QWidget *parent) : QWidget(parent) { The constructor is mostly the same, but some new bits have been added. LCDRange *force = new LCDRange; force->setRange(10, 50); We add a second LCDRange, which will be used to set the force. connect(force, SIGNAL(valueChanged(int)), cannonField, SLOT(setForce(int))); connect(cannonField, SIGNAL(forceChanged(int)), force, SLOT(setValue(int))); We connect the force widget and the cannonField widget, just like we did for the angle widget. QVBoxLayout *leftLayout = new QVBoxLayout; leftLayout->addWidget(angle); leftLayout->addWidget(force); QGridLayout *gridLayout = new QGridLayout; gridLayout->addWidget(quit, 0, 0); gridLayout->addLayout(leftLayout, 1, 0); gridLayout->addWidget(cannonField, 1, 1, 2, 1); gridLayout->setColumnStretch(1, 10); 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. force->setValue(25); We initialize the force value to 25. Running the ApplicationWe now have a force control. ExercisesMake 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. [Previous: Chapter 9] [Qt Tutorial] [Next: Chapter 11] |
Publicité
Best OfActualités les plus luesSemaine
Mois
Année
Le Qt Developer Network au hasardLa création de colonnes dans une ListView en QMLLe Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.
CommunautéRessources
Liens utilesContact
Qt dans le magazine |
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 4.1 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com