Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
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.
Technical facts
Application type(s)
Operating system(s) or programming environment(s)
IDE(s):
Project type:
C++ standard:
Compiler(s):
Libraries used:
Qt: version 4.8.3 (32 bit)
STL: GNU ISO C++ Library, version 4.7.2
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
qtroundedrectitem.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
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"
|
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.

This page has been created by the tool CodeToHtml