Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) QtExample28

 

QtQt CreatorLubuntu

 

Qt example 28: bouncing rectangles widget is a Qt Example that uses QGraphicsView to create a simple widget in which the items (rectangles) respond to collision with others.

 

 

 

 

 

Downloads

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppQtExample28/CppQtExample28.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 += \
    qttooltestbouncingrectswidgetmaindialog.cpp \
    qtmain.cpp \
    qtbouncingrectswidget.cpp \
    qtbouncingrect.cpp

HEADERS  += \
    qttooltestbouncingrectswidgetmaindialog.h \
    qtbouncingrectswidget.h \
    qtbouncingrect.h

FORMS    += qttooltestbouncingrectswidgetmaindialog.ui

 

 

 

 

 

./CppQtExample28/qtbouncingrect.h

 

#ifndef QTBOUNCINGRECT_H
#define QTBOUNCINGRECT_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <QGraphicsItem>
#pragma GCC diagnostic pop

struct QGraphicsScene;

struct QtBouncingRect : public QGraphicsItem
{
  QtBouncingRect(QGraphicsItem *parent, QGraphicsScene *scene);

  ///Make the balls move
  void advance(int phase);

  ///Thanks compiler for reminding me to add this member function!
  QRectF boundingRect() const;

  ///Thanks compiler for reminding me to add this member function!
  void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

  private:
  ///Use simplified physics in this example
  ///Horizontal speed (delta x)
  double m_dx;

  ///Use simplified physics in this example
  ///Vertical speed (delta y)
  double m_dy;

  ///The scene this class is in
  QGraphicsScene * const m_scene;
};

#endif // QTBOUNCINGRECT_H

 

 

 

 

 

./CppQtExample28/qtbouncingrect.cpp

 

#include <cassert>
#include <cmath>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QList>
#include <QPainter>
#include "qtbouncingrect.h"
#pragma GCC diagnostic pop

QtBouncingRect::QtBouncingRect(QGraphicsItem *parent, QGraphicsScene *scene)
: QGraphicsItem(parent),
   m_dx(1.0), m_dy(1.0), m_scene(scene)
{
  assert(m_scene && "An initialized QGraphicsScene must be supplied");
  m_scene->addItem(this);
}

void QtBouncingRect::advance(int /* phase */)
{
  if (x() + m_dx + (boundingRect().width() * 0.5) > m_scene->width()) m_dx = -std::abs(m_dx);
  else if (x() + m_dx - (boundingRect().width() * 0.5) < 0.0) m_dx = std::abs(m_dx);
  if (y() + m_dy + (boundingRect().height() * 0.5) > m_scene->height()) m_dy = -std::abs(m_dy);
  else if (y() + m_dy - (boundingRect().width() * 0.5) < 0.0) m_dy = std::abs(m_dy);
  this->setPos(x() + m_dx, y() + m_dy);

  //Respond to collision with other item
  const QList<QGraphicsItem *> others = collidingItems();
  if (others.isEmpty()) return;
  const QGraphicsItem * const other = others[0];
  if (this->x() < other->x()) m_dx = -std::abs(m_dx);
  else if (this->x() > other->x()) m_dx =  std::abs(m_dx);
  if (this->y() < other->y()) m_dy = -std::abs(m_dy);
  else if (this->y() > other->y()) m_dy =  std::abs(m_dy);
  this->setPos(x() + m_dx, y() + m_dy);
  this->setPos(x() + m_dx, y() + m_dy);

}

QRectF QtBouncingRect::boundingRect() const
{
  return QRectF(-16.0,-16.0,32.0,32.0);
}

void QtBouncingRect::paint(
  QPainter *painter,
  const QStyleOptionGraphicsItem * /* option */,
  QWidget * /* widget */)
{
  painter->drawRect(this->boundingRect());
}

 

 

 

 

 

./CppQtExample28/qtbouncingrectswidget.h

 

#ifndef QTBOUNCINGRECTSWIDGET_H
#define QTBOUNCINGRECTSWIDGET_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 QtBouncingRectsWidget : public QGraphicsView
{
  QtBouncingRectsWidget(QWidget *parent = 0);


  protected:
  void resizeEvent(QResizeEvent *event);

  private:
  QGraphicsScene * const m_scene;
};

#endif // QTBOUNCINGRECTSWIDGET_H

 

 

 

 

 

./CppQtExample28/qtbouncingrectswidget.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <QTimer>
#include <QGraphicsScene>
#include "qtbouncingrect.h"
#include "qtbouncingrectswidget.h"
#pragma GCC diagnostic pop

QtBouncingRectsWidget::QtBouncingRectsWidget(QWidget *parent)
  : QGraphicsView(parent),
    m_scene(new QGraphicsScene(this->rect(),this))
{
  this->setScene(m_scene);

  for (int i=0; i!=3; ++i)
  {
    QtBouncingRect * const ball = new QtBouncingRect(0,m_scene);
    ball->setPos(
      static_cast<double>((i - 1) * 32) + (0.5 * static_cast<double>(width())),
      static_cast<double>((i - 1) * 32) + (0.5 * static_cast<double>(height())));
  }

  {
    QTimer * const timer = new QTimer(this);
    QObject::connect(timer,SIGNAL(timeout()),m_scene,SLOT(advance()));
    timer->setInterval(20);
    timer->start();
  }

  //Turn off the scrollbars, as they look ugly
  this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}


void QtBouncingRectsWidget::resizeEvent(QResizeEvent *)
{
  m_scene->setSceneRect(this->rect());
}

 

 

 

 

 

./CppQtExample28/qtmain.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <QApplication>
#include "qttooltestbouncingrectswidgetmaindialog.h"
#pragma GCC diagnostic pop

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QtToolTestBouncingRectsWidgetMainDialog w;
  w.show();
  return a.exec();
}

 

 

 

 

 

./CppQtExample28/qttooltestbouncingrectswidgetmaindialog.h

 

#ifndef QTTOOLTESTBOUNCINGRECTSWIDGETMAINDIALOG_H
#define QTTOOLTESTBOUNCINGRECTSWIDGETMAINDIALOG_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 QtToolTestBouncingRectsWidgetMainDialog;
}

class QtToolTestBouncingRectsWidgetMainDialog : public QDialog
{
    Q_OBJECT
    
public:
  explicit QtToolTestBouncingRectsWidgetMainDialog(QWidget *parent = 0);
  QtToolTestBouncingRectsWidgetMainDialog(const QtToolTestBouncingRectsWidgetMainDialog&) = delete;
  QtToolTestBouncingRectsWidgetMainDialog& operator=(const QtToolTestBouncingRectsWidgetMainDialog&) = delete;
  ~QtToolTestBouncingRectsWidgetMainDialog() noexcept;
    
private:
    Ui::QtToolTestBouncingRectsWidgetMainDialog *ui;
};

#endif // QTTOOLTESTBOUNCINGRECTSWIDGETMAINDIALOG_H

 

 

 

 

 

./CppQtExample28/qttooltestbouncingrectswidgetmaindialog.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include "qtbouncingrectswidget.h"
#include "qttooltestbouncingrectswidgetmaindialog.h"
#include "ui_qttooltestbouncingrectswidgetmaindialog.h"
#pragma GCC diagnostic pop

QtToolTestBouncingRectsWidgetMainDialog::QtToolTestBouncingRectsWidgetMainDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QtToolTestBouncingRectsWidgetMainDialog)
{
  ui->setupUi(this);
  ui->layout->addWidget(new QtBouncingRectsWidget(this));
}

QtToolTestBouncingRectsWidgetMainDialog::~QtToolTestBouncingRectsWidgetMainDialog() noexcept
{
  delete ui;
}

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml