(C++) QGraphicsRectItemExample3

February 24, 2017 · View on GitHub

 

 

 

 

 

(C++) QGraphicsRectItemExample3

 

QtQt
CreatorLubuntu

 

QGraphicsRectItem example 3: Coordinat display on a linear gradient is a QGraphicsRectItem example that displays multiple QGraphicsRectItems. These items are movable and selectable. Additionally, the items display their coordinats on a linear gradient.

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

  • STL STL: GNU ISO C++ Library, version 4.9.2

 

 

 

 

 

Qt project file: ./CppQGraphicsRectItemExample3/CppQGraphicsRectItemExample3.pro

 


include(../../DesktopApplication.pri) SOURCES += \     qtmain.cpp \     qtwidget.cpp \     qtrectitem.cpp HEADERS += \     qtwidget.h \     qtrectitem.h

 

 

 

 

 

./CppQGraphicsRectItemExample3/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 <QDesktopWidget> #include "qtwidget.h" #pragma GCC diagnostic pop int main(int argc, char *argv[]) {   QApplication a(argc, argv);   QtWidget w;   {     //Resize the dialog and put it in the screen center     w.setGeometry(0,0,600,400);     const QRect screen = QApplication::desktop()->screenGeometry();     w.move( screen.center() - w.rect().center() );   }   w.show();   return a.exec(); }

 

 

 

 

 

./CppQGraphicsRectItemExample3/qtrectitem.h

 


#ifndef QTRECTITEM_H #define QTRECTITEM_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 <QGraphicsRectItem> #pragma GCC diagnostic pop struct QtRectItem : public QGraphicsRectItem {   QtRectItem(QGraphicsItem *parent = 0);   void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); }; #endif // QTRECTITEM_H

 

 

 

 

 

./CppQGraphicsRectItemExample3/qtrectitem.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 "qtrectitem.h" #include <cassert> #include <sstream> #include <QGraphicsScene> #include <QLinearGradient> #include <QPainter> #pragma GCC diagnostic pop QtRectItem::QtRectItem(QGraphicsItem *parent) : QGraphicsRectItem(parent) {   this->setFlags(       QGraphicsItem::ItemIsFocusable     | QGraphicsItem::ItemIsMovable     | QGraphicsItem::ItemIsSelectable); } void QtRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {   //Create the text   std::stringstream s;   s << "(" << static_cast<int>(this->pos().x()) << "," << static_cast<int>(this->pos().y()) << ")";   const std::string t = s.str();   //Resize the rect to the text its size (using QFontMetrics to find this out)   QFontMetrics q(painter->font());   const double width = q.width(t.c_str());   const double height = q.height();   const QRectF text_rect(-0.5 * width, -0.5 * height, width, height);   const double padding = 4.0;   this->setRect(text_rect.adjusted(-padding,-padding,padding,padding));   {     QLinearGradient g(this->rect().topLeft(),this->rect().bottomRight());     g.setColorAt(0.0,QColor(128,128,128));     g.setColorAt(1.0,QColor(240,240,240));     this->setBrush(g);   }   //Let QGraphicsRectItem handle the default painting   QGraphicsRectItem::paint(painter,option,widget);   //Draw the text on the drawn rectangle   painter->drawText(text_rect,t.c_str()); }

 

 

 

 

 

./CppQGraphicsRectItemExample3/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 ///The widget holding the items struct QtWidget : public QGraphicsView {   QtWidget(QWidget *parent = 0); }; #endif // QTWIDGET_H

 

 

 

 

 

./CppQGraphicsRectItemExample3/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 "qtwidget.h" #include <cassert> #include <cmath> #include <iostream> #include <QGraphicsScene> #include <QKeyEvent> #include <QGraphicsSimpleTextItem> #include "qtrectitem.h" #pragma GCC diagnostic pop QtWidget::QtWidget(QWidget *parent)   : QGraphicsView(new QGraphicsScene,parent) {   const int n_items = 16;   for (int i=0; i!=n_items; ++i)   {     const double angle = 2.0 * M_PI * (static_cast<double>(i) / static_cast<double>(n_items));     const double ray = 150.0;     const double x =  std::sin(angle) * ray;     const double y = -std::cos(angle) * ray;     QtRectItem * const item = new QtRectItem;     item->setPos(x,y);     scene()->addItem(item);   } }

 

 

 

 

 

./CppQGraphicsRectItemExample3/crosscompiletowindows.sh

 


#!/bin/sh #From http://richelbilderbeek.nl/CppQtCrosscompileToWindowsExample15.htm echo "Cross compiling to Windows" echo "1/2: Creating Windows makefile" i686-pc-mingw32-qmake CppQGraphicsRectItemExample3.pro echo "2/2: making makefile" make echo "Done"