Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.



Qt example 27: create a simple gravity widget is a Qt Example
that uses QGraphicsView to create a simple widget
in which the item (a ball) is responding as if there is gravity, bouncing perfectly elasticly.
Downloads
Technical facts
Application type(s)
Operating system(s) or programming environment(s)
IDE(s):
Project type:
C++ standard:
Compiler(s):
Libraries used:
Qt: version 5.4.1 (32 bit)
STL: GNU ISO C++ Library, version 4.9.2
Qt project file: ./CppQtExample27/CppQtExample27.pro
./CppQtExample27/main.cpp
./CppQtExample27/qtgravitywidget.h
./CppQtExample27/qtgravitywidget.cpp
./CppQtExample27/qtgravitywidgetitem.h
./CppQtExample27/qtgravitywidgetitem.cpp
#include <cmath>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <QGraphicsScene>
#include <QPainter>
#include "qtgravitywidgetitem.h"
#pragma GCC diagnostic pop
QtGravityWidgetItem::QtGravityWidgetItem(
QGraphicsItem * parent,
QGraphicsScene * scene)
: QGraphicsItem(parent),
m_dy(0.0),
m_rect(-16.0,-16.0,32.0,32.0),
m_scene(scene)
{
scene->addItem(this);
}
void QtGravityWidgetItem::advance(int)
{
const double acceleration = 0.1;
//If the new y coordinat (after acceleration) causes the bottom of the QtGravityWidgetItem
//to be beyond the edge of the QGraphicsScene, turn the vertical speed upwards
if (y() + m_dy + acceleration + (m_rect.height() * 0.5) > m_scene->height()) m_dy = -std::abs(m_dy + acceleration);
//Accelerate the QtGravityWidgetItem
m_dy+=acceleration;
//Move the QtGravityWidgetItem
setY(y() + m_dy);
//Move the QtGravityWidgetItem up when the QGraphicsScene is resized so much
//that the QtGravityWidgetItem gets out of sight
if (y() + (m_rect.height() * 0.5) > m_scene->height())
{
//Move the QtGravityWidgetItem to the bottom of the QGraphicsScene
setY(m_scene->height() - (m_rect.height() * 0.5));
//Set the vertical speed to zero, otherwise there is need to check if the QtGravityWidgetItem
//leaves the top of the QGraphicsScene
m_dy = 0.0;
}
}
QRectF QtGravityWidgetItem::boundingRect() const
{
return m_rect;
}
void QtGravityWidgetItem::paint(QPainter * painter, const QStyleOptionGraphicsItem *, QWidget *)
{
painter->drawEllipse(m_rect);
}
|
./CppQtExample27/tooltestgravitywidgetmaindialog.h
./CppQtExample27/tooltestgravitywidgetmaindialog.cpp
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.

This page has been created by the tool CodeToHtml