Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) QGraphicsPolygonItemExample1

 

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: ./CppQGraphicsPolygonItemExample1/CppQGraphicsPolygonItemExample1.pro

 

exists(../../DesktopApplication.pri) {
  include(../../DesktopApplication.pri)
}
!exists(../../DesktopApplication.pri) {
  QT       += core gui
  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

  win32 {
    greaterThan(QT_MAJOR_VERSION, 4): QT += svg
  }

  TEMPLATE = app

  CONFIG(debug, debug|release) {
    message(Debug mode)
  }

  CONFIG(release, debug|release) {
    message(Release mode)
    DEFINES += NDEBUG NTRACE_BILDERBIKKEL
  }

  QMAKE_CXXFLAGS += -std=c++1y -Wall -Wextra -Weffc++

  unix {
    QMAKE_CXXFLAGS += -Werror
  }
}

exists(../../Libraries/Boost.pri) {
  include(../../Libraries/Boost.pri)
}
!exists(../../Libraries/Boost.pri) {
  INCLUDEPATH += \
    ../../Libraries/boost_1_55_0
}

SOURCES += main.cpp

 

 

 

 

 

./CppQGraphicsPolygonItemExample1/main.cpp

 

#include <cassert>

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

struct MyItem : public QGraphicsPolygonItem
{
  MyItem()
  {
    this->setPolygon(CreatePolygon());
  }

  //A complex shape
  static const QPolygonF CreatePolygon() noexcept
  {
    const double meter = 200.0;
    const double milli = 0.001;

    //Draw frame with opening
    const double ax( 0.1 * meter); const double ay(-0.4 * meter);
    const double bx( 0.1 * meter); const double by(-0.2 * meter);
    const double cx( 0.3 * meter); const double cy(-0.2 * meter);
    const double dx( 0.3 * meter); const double dy( 0.29 * meter);
    const double ex(-0.1 * meter); const double ey( 0.29 * meter);
    const double fx(-0.1 * meter); const double fy( 1.29 * meter);
    const double gx(-0.2 * meter); const double gy( 1.29 * meter);
    const double hx(-0.2 * meter); const double hy(-0.4 * meter);
    const QPolygonF p_outside(
      QVector<QPointF>(
        {
          QPointF(ax,ay),
          QPointF(bx,by),
          QPointF(cx,cy),
          QPointF(dx,dy),
          QPointF(ex,ey),
          QPointF(fx,fy),
          QPointF(gx,gy),
          QPointF(hx,hy)
        }
      )
    );
    //Draw rect in frame
    const double rx((-47.4-25.0) * milli * meter);
    const double ry(( 56.4-25.0) * milli * meter);
    const double rw((160.0+50.0) * milli * meter);
    const double rh((130.0+50.0) * milli * meter);
    const QRectF r(rx,ry,rw,rh);
    const QPolygonF p_inside(
      QVector<QPointF>(
        {
          QPointF( rx      ,ry),
          QPointF((rx + rw),ry),
          QPointF((rx + rw),(ry+rh)),
          QPointF( rx      ,(ry+rh))
        }
      )
    );
    const QPolygonF p( p_outside.subtracted(p_inside));
    return p;
  }
};

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QGraphicsView w;

  assert(!w.scene());
  w.setScene(new QGraphicsScene);
  assert(w.scene());

  {
    MyItem * const item = new MyItem;
    w.scene()->addItem(item);
  }
  {
    MyItem * const item = new MyItem;
    item->setBrush(QBrush(QColor(0,0,0)));
    item->setPos(300.0,0);
    w.scene()->addItem(item);
  }
  w.show();

  return a.exec();
}

 

 

 

 

 

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