(C++) Qt example 37: rounded rectangle item
January 10, 2018 · View on GitHub
Qt example 37: rounded rectangle item is a QGraphicsRectItem example that displays some QGraphicsRectItem the are movable and selectable. Additionally, the items display their coordinats.

- Download the Qt Creator project 'QtExample37' (zip)
- Download a Windows executable of 'QtExample37' (zip)
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 12.10 (quantal)
Qt Creator 2.5.2
- G++ 4.7.2
Libraries used:
Qt project file: CppQtExample37.pro
QT += core gui QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Werror TARGET = CppQtExample37 TEMPLATE = app SOURCES += \ qtmain.cpp \ qtwidget.cpp \ qtroundedrectitem.cpp HEADERS += \ qtwidget.h \ qtroundedrectitem.h
qtmain.cpp
#include <QApplication> #include <QDesktopWidget> #include "qtwidget.h" 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,800,600); const QRect screen = QApplication::desktop()->screenGeometry(); w.move( screen.center() - w.rect().center() ); } w.show(); return a.exec(); }
qtroundedrectitem.h
#ifndef QTRECTITEM_H #define QTRECTITEM_H #include <QGraphicsRectItem> struct QtRoundedRectItem : public QGraphicsRectItem { QtRoundedRectItem(QGraphicsItem *parent = 0, QGraphicsScene *scene = 0); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); ///Get the rounded rect corner x radius double getRadiusX() const { return m_radius_x; } ///Get the rounded rect corner y radius double getRadiusY() const { return m_radius_y; } ///Set the rounded rect corner x radius void setRadiusX(const double radius_x) { m_radius_x = radius_x; } ///Set the rounded rect corner y radius void setRadiusY(const double radius_y) { m_radius_y = radius_y; } ///Set the rounded rect void setRoundedRect(const QRectF rect, const double radius_x, const double radius_y); private: ///The rounded rect corner x radius double m_radius_x; ///The rounded rect corner y radius double m_radius_y; }; #endif // QTRECTITEM_H
qtroundedrectitem.cpp
#ifdef _WIN32 //See http://www.richelbilderbeek.nl/CppCompileErrorSwprintfHasNotBeenDeclared.htm #undef __STRICT_ANSI__ #endif //#include own header file as first substantive line of code, from: // * John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Section 3.2, page 110 #include "qtroundedrectitem.h" #include <cassert> #include <sstream> #include <QGraphicsScene> #include <QPainter> QtRoundedRectItem::QtRoundedRectItem(QGraphicsItem *parent, QGraphicsScene *scene) : QGraphicsRectItem(parent,scene) { this->setFlags( QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); } void QtRoundedRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) { if (this->isSelected()) { QPen pen; pen.setStyle(Qt::DashLine); painter->setPen(pen); } painter->drawRoundedRect(this->rect(),m_radius_x,m_radius_y); } void QtRoundedRectItem::setRoundedRect(const QRectF rect, const double radius_x, const double radius_y) { this->setRect(rect); this->setRadiusX(radius_x); this->setRadiusY(radius_y); }
qtwidget.h
#ifndef QTWIDGET_H #define QTWIDGET_H #include <QGraphicsView> ///The widget holding the items struct QtWidget : public QGraphicsView { QtWidget(QWidget *parent = 0); }; #endif // QTWIDGET_H
qtwidget.cpp
#ifdef _WIN32 //See http://www.richelbilderbeek.nl/CppCompileErrorSwprintfHasNotBeenDeclared.htm #undef __STRICT_ANSI__ #endif //#include own header file as first substantive line of code, from: // * John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Section 3.2, page 110 #include "qtwidget.h" #include <cassert> #include <cmath> #include <iostream> #include <QGraphicsScene> #include <QKeyEvent> #include <QGraphicsSimpleTextItem> #include "qtroundedrectitem.h" 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 = 200.0; const double x = std::sin(angle) * ray; const double y = -std::cos(angle) * ray; QtRoundedRectItem * const item = new QtRoundedRectItem; item->setPos(x,y); item->setRoundedRect(QRectF(-32.0,-32.0,64.0,64.0),16.0,16.0); scene()->addItem(item); } { const double ray = 120.0; const double x = std::sin(angle) * ray; const double y = -std::cos(angle) * ray; QGraphicsRectItem * const item = new QGraphicsRectItem; item->setFlags( QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); item->setPos(x,y); item->setRect(-16.0,-16.0,32.0,32.0); scene()->addItem(item); } } }
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 CppQtExample37.pro echo "2/2: making makefile" make echo "Done"