(C++) QtExample27

February 24, 2017 · View on GitHub

 

 

 

 

 

(C++) QtExample27

 

QtQt
CreatorLubuntu

 

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 Qt: version 5.4.1 (32 bit)
  • STL STL: GNU ISO C++ Library, version 4.9.2

 

 

 

 

 

Qt project file: ./CppQtExample27/CppQtExample27.pro

 


exists(../../DesktopApplication.pri) {   include(../../DesktopApplication.pri) } !exists(../../DesktopApplication.pri) {   QT += core gui   greaterThan(QT_MAJOR_VERSION, 4): QT += widgets   TEMPLATE = app   CONFIG(debug, debug|release) {     message(Debug mode)   }   CONFIG(release, debug|release) {     message(Release mode)     DEFINES += NDEBUG NTRACE_BILDERBIKKEL   }   QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra   unix {     QMAKE_CXXFLAGS += -Werror   } } exists(../../Libraries/Boost.pri) {   include(../../Libraries/Boost.pri) } !exists(../../Libraries/Boost.pri) {   win32 {     INCLUDEPATH += \       ../../../Projects/Libraries/boost_1_55_0   } } SOURCES += \     main.cpp \     tooltestgravitywidgetmaindialog.cpp \     qtgravitywidgetitem.cpp \     qtgravitywidget.cpp HEADERS  += \     tooltestgravitywidgetmaindialog.h \     qtgravitywidgetitem.h \     qtgravitywidget.h FORMS    += tooltestgravitywidgetmaindialog.ui

 

 

 

 

 

./CppQtExample27/main.cpp

 


#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <QApplication> #include "tooltestgravitywidgetmaindialog.h" #pragma GCC diagnostic pop int main(int argc, char *argv[]) {   QApplication a(argc, argv);   ToolTestGravityWidgetMainDialog w;   w.show();   return a.exec(); }

 

 

 

 

 

./CppQtExample27/qtgravitywidget.h

 


#ifndef QTGRAVITYWIDGET_H #define QTGRAVITYWIDGET_H #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <QGraphicsView> #pragma GCC diagnostic pop struct QGraphicsScene; struct QtGravityWidget : public QGraphicsView {   QtGravityWidget(QWidget * parent = 0);   protected:   void resizeEvent(QResizeEvent *);   private:   QGraphicsScene * const m_scene; }; #endif // QTGRAVITYWIDGET_H

 

 

 

 

 

./CppQtExample27/qtgravitywidget.cpp

 


#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <QGraphicsView> #include <QTimer> #include "qtgravitywidget.h" #include "qtgravitywidgetitem.h" #pragma GCC diagnostic pop QtGravityWidget::QtGravityWidget(QWidget *parent)   : QGraphicsView(parent),     m_scene(new QGraphicsScene(this->rect(),this)) {   this->setScene(m_scene);   //Create the item   {     QtGravityWidgetItem * const item = new QtGravityWidgetItem(0,m_scene);     item->setPos(static_cast<double>(width()) * 0.5,static_cast<double>(height()) * 0.5);   }   //Create the main QTimer   {     QTimer * const timer = new QTimer(this);     timer->setInterval(20);     QObject::connect(timer,SIGNAL(timeout()),m_scene,SLOT(advance()));     timer->start();   }   //Turn off the scrollbars, as they look ugly   this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);   this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); } void QtGravityWidget::resizeEvent(QResizeEvent *) {   m_scene->setSceneRect(this->rect()); }

 

 

 

 

 

./CppQtExample27/qtgravitywidgetitem.h

 


#ifndef QTGRAVITYWIDGETITEM_H #define QTGRAVITYWIDGETITEM_H #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <QGraphicsItem> #pragma GCC diagnostic pop struct QtGravityWidgetItem : public QGraphicsItem {   QtGravityWidgetItem(QGraphicsItem * parent, QGraphicsScene * scene);   ///Must be defined, thanks compiler for telling me!   QRectF boundingRect() const;   ///Must be defined, thanks compiler for telling me!   void paint(QPainter * painter, const QStyleOptionGraphicsItem *, QWidget *);   ///Can be defined optionally, this will contain the falling motion   void advance(int);   private:   ///The vertical speed   double m_dy;   ///The rectangle this QtGravityWidgetItem is in   const QRectF m_rect;   ///Read-only pointer to the QGraphicsScene this QtGravityWidgetItem is part of   const QGraphicsScene * const m_scene; }; #endif // 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

 


#ifndef TOOLTESTGRAVITYWIDGETMAINDIALOG_H #define TOOLTESTGRAVITYWIDGETMAINDIALOG_H #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <QDialog> #pragma GCC diagnostic pop namespace Ui {   class ToolTestGravityWidgetMainDialog; } class ToolTestGravityWidgetMainDialog : public QDialog {   Q_OBJECT      public:   explicit ToolTestGravityWidgetMainDialog(QWidget *parent = 0);   ToolTestGravityWidgetMainDialog(const ToolTestGravityWidgetMainDialog&) = delete;   ToolTestGravityWidgetMainDialog& operator=(const ToolTestGravityWidgetMainDialog&) = delete;   ~ToolTestGravityWidgetMainDialog() noexcept; private:   Ui::ToolTestGravityWidgetMainDialog *ui; }; #endif // TOOLTESTGRAVITYWIDGETMAINDIALOG_H

 

 

 

 

 

./CppQtExample27/tooltestgravitywidgetmaindialog.cpp

 


#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include <QGraphicsScene> #include <QGraphicsView> #include <QTimer> #include "qtgravitywidget.h" #include "qtgravitywidgetitem.h" #include "tooltestgravitywidgetmaindialog.h" #include "ui_tooltestgravitywidgetmaindialog.h" #pragma GCC diagnostic pop ToolTestGravityWidgetMainDialog::ToolTestGravityWidgetMainDialog(QWidget *parent) :     QDialog(parent),     ui(new Ui::ToolTestGravityWidgetMainDialog) {   ui->setupUi(this); } ToolTestGravityWidgetMainDialog::~ToolTestGravityWidgetMainDialog() noexcept {   delete ui; }