Latest

Archive

Community news

C++

Communities and Content

Databases

Editorials

Emacs

General

HTML

Java

Notices

PHP

XML

Apache

C++

Database

General

HTML

Java

Javascript

Linux

Object oriented programming

Open source

Perl

PHP

Python

Ruby

SOAP

XML

Suggest a link

Advertise on zez

Contribute

Contact us

About zez


Creating games with QCanvas





A close-up of the planet and stars

The classes


We use 3 classes: Ball, which is subclassed from QCanvasSprite (which inherits QCanvasItem). This is the bouncing globe, it contains routines to control its movement. BounceCanvasView is subclassed from QCanvasView, and contains the mouse interaction functions. Finally, BounceWidget (a QWidget) is the application window, it contains the canvasview and a "Quit" button.

BounceWidget


The BounceWidget constructor creates a QCanvas and sets the BounceCanvasView to view it. We then tell the canvas to update every 10 ms, and create a planet and set its initial location and speed. We also set the graphics for the planet and the background. To change them, simply replace "stars.png" and "earth.png" with something else. (Note: The image of the earth is of course square, not a circle. You must use a format that supports transparency, like PNG or GIF, to achieve the illusion of a circle.)

BounceWidget constructor



BounceWidget::BounceWidget( QWidget *parent, const char *name )
    : QWidget( parent, name )
{
    // Create and setup canvas and view
    Canvas = new QCanvas( this, "Canvas" );
    Canvas->setBackgroundColor( Qt::black );
    Canvas->setBackgroundPixmap( QPixmap( "stars.png" ) );
    Canvas->setAdvancePeriod( 10 );
    Canvas->resize( 500, 500 );
    CanvasView = new BounceCanvasView( Canvas, this, "CanvasView" );

    // setup the quit button
    QuitButton = new QPushButton( "&Quit", this, "QuitButton" );
    connect( QuitButton, SIGNAL( clicked() ), qApp, SLOT( quit() ) );

    // setup the layout manager
    QVBoxLayout *vlay = new QVBoxLayout( this );
    vlay->setResizeMode( QLayout::Fixed );
    vlay->addWidget( CanvasView );
    vlay->addWidget( QuitButton );

    // add a planet to our scenery
    Ball *sphere = new Ball( new QCanvasPixmapArray( "earth.png" ), Canvas );
    sphere->move( 100, 150 );
    sphere->setXVelocity( 2 );
    sphere->setYVelocity( 4 );
    sphere->show();
}


Attached files:


<< Previous page | 1 | < 2 > | 3 | 4 | 5 | 6 | Next page >> | Printer-friendly page |

Comment List


Topic: Author:
Time:
Very much Informative Riphat satti 29.08.2007 10:47

I think this is very useful exapme to learn about qcanvas..

each and every thing describe in full detail.
a layman can easily understand this example.


Bounce Canvas Demo Dr M 15.05.2005 05:57

Bounce is a very nicely written demo.
I wish QT would also distribute it
on their CD as an example on how to
write a QCanvas Application.

The example given in the QT book is
way too much for a newcomer.


Very usefull Arun Sivakumar 01.12.2001 17:18

The article featuring the QCanvas was really usefull and simple to understand. Hope you release more article covering various other features of Qt.

Please do inform if there are any other site with equally good articles on Qt.

Thank you
s_arunn@yahoo.com




Forgot your password?

Register a new user

Results

Polls