diff options
author | Timothy Pearson <[email protected]> | 2011-07-10 15:24:15 -0500 |
---|---|---|
committer | Timothy Pearson <[email protected]> | 2011-07-10 15:24:15 -0500 |
commit | bd0f3345a938b35ce6a12f6150373b0955b8dd12 (patch) | |
tree | 7a520322212d48ebcb9fbe1087e7fca28b76185c /tutorial/t10 | |
download | qt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.tar.gz qt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.zip |
Add Qt3 development HEAD version
Diffstat (limited to 'tutorial/t10')
-rw-r--r-- | tutorial/t10/cannon.cpp | 80 | ||||
-rw-r--r-- | tutorial/t10/cannon.h | 43 | ||||
-rw-r--r-- | tutorial/t10/lcdrange.cpp | 47 | ||||
-rw-r--r-- | tutorial/t10/lcdrange.h | 35 | ||||
-rw-r--r-- | tutorial/t10/main.cpp | 75 | ||||
-rw-r--r-- | tutorial/t10/t10.pro | 9 |
6 files changed, 289 insertions, 0 deletions
diff --git a/tutorial/t10/cannon.cpp b/tutorial/t10/cannon.cpp new file mode 100644 index 0000000..67e6f9a --- /dev/null +++ b/tutorial/t10/cannon.cpp @@ -0,0 +1,80 @@ +/**************************************************************** +** +** Implementation CannonField class, Qt tutorial 10 +** +****************************************************************/ + +#include "cannon.h" +#include <qpainter.h> +#include <qpixmap.h> + + +CannonField::CannonField( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + ang = 45; + f = 0; + setPalette( QPalette( QColor( 250, 250, 200) ) ); +} + + +void CannonField::setAngle( int degrees ) +{ + if ( degrees < 5 ) + degrees = 5; + if ( degrees > 70 ) + degrees = 70; + if ( ang == degrees ) + return; + ang = degrees; + repaint( cannonRect(), FALSE ); + emit angleChanged( ang ); +} + + +void CannonField::setForce( int newton ) +{ + if ( newton < 0 ) + newton = 0; + if ( f == newton ) + return; + f = newton; + emit forceChanged( f ); +} + + +void CannonField::paintEvent( QPaintEvent *e ) +{ + if ( !e->rect().intersects( cannonRect() ) ) + return; + + QRect cr = cannonRect(); + QPixmap pix( cr.size() ); + pix.fill( this, cr.topLeft() ); + + QPainter p( &pix ); + p.setBrush( blue ); + p.setPen( NoPen ); + p.translate( 0, pix.height() - 1 ); + p.drawPie( QRect( -35,-35, 70, 70 ), 0, 90*16 ); + p.rotate( -ang ); + p.drawRect( QRect(33, -4, 15, 8) ); + p.end(); + + p.begin( this ); + p.drawPixmap( cr.topLeft(), pix ); +} + + +QRect CannonField::cannonRect() const +{ + QRect r( 0, 0, 50, 50 ); + r.moveBottomLeft( rect().bottomLeft() ); + return r; +} + + +QSizePolicy CannonField::sizePolicy() const +{ + return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ); +} diff --git a/tutorial/t10/cannon.h b/tutorial/t10/cannon.h new file mode 100644 index 0000000..5a317d1 --- /dev/null +++ b/tutorial/t10/cannon.h @@ -0,0 +1,43 @@ +/**************************************************************** +** +** Definition of CannonField class, Qt tutorial 10 +** +****************************************************************/ + +#ifndef CANNON_H +#define CANNON_H + +#include <qwidget.h> + + +class CannonField : public QWidget +{ + Q_OBJECT +public: + CannonField( QWidget *parent=0, const char *name=0 ); + + QSizePolicy sizePolicy() const; + + int angle() const { return ang; } + int force() const { return f; } + +public slots: + void setAngle( int degrees ); + void setForce( int newton ); + +signals: + void angleChanged( int ); + void forceChanged( int ); + +protected: + void paintEvent( QPaintEvent * ); + +private: + QRect cannonRect() const; + + int ang; + int f; +}; + + +#endif // CANNON_H diff --git a/tutorial/t10/lcdrange.cpp b/tutorial/t10/lcdrange.cpp new file mode 100644 index 0000000..b62cfbb --- /dev/null +++ b/tutorial/t10/lcdrange.cpp @@ -0,0 +1,47 @@ +/**************************************************************** +** +** Implementation of LCDRange class, Qt tutorial 8 +** +****************************************************************/ + +#include "lcdrange.h" + +#include <qslider.h> +#include <qlcdnumber.h> + +LCDRange::LCDRange( QWidget *parent, const char *name ) + : QVBox( parent, name ) +{ + QLCDNumber *lcd = new QLCDNumber( 2, this, "lcd" ); + slider = new QSlider( Horizontal, this, "slider" ); + slider->setRange( 0, 99 ); + slider->setValue( 0 ); + connect( slider, SIGNAL(valueChanged(int)), + lcd, SLOT(display(int)) ); + connect( slider, SIGNAL(valueChanged(int)), + SIGNAL(valueChanged(int)) ); + + setFocusProxy( slider ); +} + +int LCDRange::value() const +{ + return slider->value(); +} + +void LCDRange::setValue( int value ) +{ + slider->setValue( value ); +} + +void LCDRange::setRange( int minVal, int maxVal ) +{ + if ( minVal < 0 || maxVal > 99 || minVal > maxVal ) { + qWarning( "LCDRange::setRange(%d,%d)\n" + "\tRange must be 0..99\n" + "\tand minVal must not be greater than maxVal", + minVal, maxVal ); + return; + } + slider->setRange( minVal, maxVal ); +} diff --git a/tutorial/t10/lcdrange.h b/tutorial/t10/lcdrange.h new file mode 100644 index 0000000..5fa2d21 --- /dev/null +++ b/tutorial/t10/lcdrange.h @@ -0,0 +1,35 @@ +/**************************************************************** +** +** Definition of LCDRange class, Qt tutorial 8 +** +****************************************************************/ + +#ifndef LCDRANGE_H +#define LCDRANGE_H + +#include <qvbox.h> + +class QSlider; + + +class LCDRange : public QVBox +{ + Q_OBJECT +public: + LCDRange( QWidget *parent=0, const char *name=0 ); + + int value() const; + +public slots: + void setValue( int ); + void setRange( int minVal, int maxVal ); + +signals: + void valueChanged( int ); + +private: + QSlider *slider; +}; + + +#endif // LCDRANGE_H diff --git a/tutorial/t10/main.cpp b/tutorial/t10/main.cpp new file mode 100644 index 0000000..6dd2bf5 --- /dev/null +++ b/tutorial/t10/main.cpp @@ -0,0 +1,75 @@ +/**************************************************************** +** +** Qt tutorial 10 +** +****************************************************************/ + +#include <qapplication.h> +#include <qpushbutton.h> +#include <qlcdnumber.h> +#include <qfont.h> +#include <qlayout.h> + +#include "lcdrange.h" +#include "cannon.h" + + +class MyWidget: public QWidget +{ +public: + MyWidget( QWidget *parent=0, const char *name=0 ); +}; + + +MyWidget::MyWidget( QWidget *parent, const char *name ) + : QWidget( parent, name ) +{ + QPushButton *quit = new QPushButton( "&Quit", this, "quit" ); + quit->setFont( QFont( "Times", 18, QFont::Bold ) ); + + connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) ); + + LCDRange *angle = new LCDRange( this, "angle" ); + angle->setRange( 5, 70 ); + + LCDRange *force = new LCDRange( this, "force" ); + force->setRange( 10, 50 ); + + CannonField *cannonField = new CannonField( this, "cannonField" ); + + connect( angle, SIGNAL(valueChanged(int)), + cannonField, SLOT(setAngle(int)) ); + connect( cannonField, SIGNAL(angleChanged(int)), + angle, SLOT(setValue(int)) ); + + connect( force, SIGNAL(valueChanged(int)), + cannonField, SLOT(setForce(int)) ); + connect( cannonField, SIGNAL(forceChanged(int)), + force, SLOT(setValue(int)) ); + + QGridLayout *grid = new QGridLayout( this, 2, 2, 10 ); + grid->addWidget( quit, 0, 0 ); + grid->addWidget( cannonField, 1, 1 ); + grid->setColStretch( 1, 10 ); + + QVBoxLayout *leftBox = new QVBoxLayout; + grid->addLayout( leftBox, 1, 0 ); + leftBox->addWidget( angle ); + leftBox->addWidget( force ); + + angle->setValue( 60 ); + force->setValue( 25 ); + angle->setFocus(); +} + +int main( int argc, char **argv ) +{ + QApplication::setColorSpec( QApplication::CustomColor ); + QApplication a( argc, argv ); + + MyWidget w; + w.setGeometry( 100, 100, 500, 355 ); + a.setMainWidget( &w ); + w.show(); + return a.exec(); +} diff --git a/tutorial/t10/t10.pro b/tutorial/t10/t10.pro new file mode 100644 index 0000000..202b939 --- /dev/null +++ b/tutorial/t10/t10.pro @@ -0,0 +1,9 @@ +TEMPLATE = app +CONFIG += qt warn_on release +HEADERS = cannon.h \ + lcdrange.h +SOURCES = cannon.cpp \ + lcdrange.cpp \ + main.cpp +TARGET = t10 +REQUIRES=full-config |