Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) QGraphicsPixmapItemExample1

 

QGraphicsPixmapItem example 1: basic is a QGraphicsPixmapItem example. This example shows how to put multiple movable QGraphicsPixmapItems on screen. Note that the pixmaps are transparent, because the images supplied were transparent.

 

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

 

exists (../../DesktopApplication.pri) {
  include(../../DesktopApplication.pri)
}
!exists (../../DesktopApplication.pri) {
  QT += core printsupport
  QT += gui
  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets svg
  CONFIG   += console
  CONFIG   -= app_bundle
  TEMPLATE = app
  CONFIG(release, debug|release) {
    DEFINES += NDEBUG NTRACE_BILDERBIKKEL
  }
  QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++
  unix {
    QMAKE_CXXFLAGS += -Werror
  }
}

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

SOURCES += \
    qtmain.cpp \
    qtwidget.cpp \
    qtitem.cpp

HEADERS += \
    qtwidget.h \
    qtitem.h

RESOURCES += \
    CppQGraphicsPixmapItemExample1.qrc

 

 

 

 

 

./CppQGraphicsPixmapItemExample1/qtitem.h

 

#ifndef QTITEM_H
#define QTITEM_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <QGraphicsPixmapItem>
#pragma GCC diagnostic pop

///A QGraphicsPixmapItem that loads its pixmap from resources
struct QtItem : public QGraphicsPixmapItem
{
  QtItem(QGraphicsItem *parent = 0);
};

#endif // QTITEM_H

 

 

 

 

 

./CppQGraphicsPixmapItemExample1/qtitem.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <cassert>
#include <QPainter>
#include "qtitem.h"
#pragma GCC diagnostic pop

QtItem::QtItem(QGraphicsItem *parent)
  : QGraphicsPixmapItem(parent)
{
  assert(this->pixmap().isNull()
    && "Assume no pixmap loaded yet");

  //Load the pixmap from resources
  this->setPixmap(QPixmap(":/images/PicR.png"));

  assert(!this->pixmap().isNull()
    && "Assume pixmap is loaded successfully");

  this->setFlags(QGraphicsItem::ItemIsMovable);
}

 

 

 

 

 

./CppQGraphicsPixmapItemExample1/qtmain.cpp

 

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

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

 

 

 

 

 

./CppQGraphicsPixmapItemExample1/qtwidget.h

 

#ifndef QTWIDGET_H
#define QTWIDGET_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <QGraphicsView>
#pragma GCC diagnostic pop

struct QtWidget : public QGraphicsView
{
  QtWidget();
};

#endif // QTWIDGET_H

 

 

 

 

 

./CppQGraphicsPixmapItemExample1/qtwidget.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <cassert>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include "qtitem.h"
#include "qtwidget.h"
#pragma GCC diagnostic pop

QtWidget::QtWidget()
{
  QGraphicsScene * const scene = new QGraphicsScene(this);
  this->setScene(scene);

  const int n_items = 64;
  for (int i=0; i!=n_items; ++i)
  {
    QtItem * const item = new QtItem;
    //Scatter those items around a bit
    item->setPos(
      - 128 + (std::rand() % 256),
      - 128 + (std::rand() % 256));

    scene->addItem(item);
  }
}

 

 

 

 

 

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