I would like to know how can I add my custom QGraphicsItem in a grid.
custom-tem.cpp
#include "custom-item.h"
{
/* drawPolygon(...)*/
}
QRectF CustomItem
::boundingRect() const {
return QRectF(0,
0,
250,
250);
}
void CustomItem
::paint(QPainter *painter,
{
painter
->setRenderHint
(QPainter::Antialiasing);
painter->setBrush(Qt::blue);
}
{
qDebug() << "Mouse button clicked at position: "
<< event->pos();
}
{
qDebug() << "Mouse button released at position: "
<< event->pos();
}
#include "custom-item.h"
CustomItem::CustomItem(QGraphicsItem *parent)
: QGraphicsItem(parent)
{
/* drawPolygon(...)*/
polygon << QPointF(55, 180) << QPointF(35, 120)
<< QPointF(75, 40) << QPointF(115, 20)
<< QPointF(195, 80) << QPointF(215, 160)
<< QPointF(115, 220);
}
QRectF CustomItem::boundingRect() const
{
return QRectF(0, 0, 250, 250);
}
void CustomItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
painter->setRenderHint(QPainter::Antialiasing);
painter->setBrush(Qt::blue);
}
void CustomItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "Mouse button clicked at position: "
<< event->pos();
}
void CustomItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "Mouse button released at position: "
<< event->pos();
}
To copy to clipboard, switch view to plain text mode
custom-item.h
#ifndef CUSTOM_ITEM_H
#define CUSTOM_ITEM_H
#include <QGraphicsItem>
#include <QPointF>
#include <QRectF>
#include <QPolygonF>
#include <QPainter>
#include <QPainterPath>
#include <QWidget>
#include <QGraphicsSceneMouseEvent>
#include <QDebug>
{
public:
private:
protected:
};
#endif
#ifndef CUSTOM_ITEM_H
#define CUSTOM_ITEM_H
#include <QGraphicsItem>
#include <QPointF>
#include <QRectF>
#include <QPolygonF>
#include <QPainter>
#include <QPainterPath>
#include <QWidget>
#include <QGraphicsSceneMouseEvent>
#include <QDebug>
class CustomItem : public QGraphicsItem
{
public:
CustomItem(QGraphicsItem *parent = 0);
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
private:
QPolygonF polygon;
QPixmap pixmap;
QPainterPath path;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
};
#endif
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include "custom-item.h"
int main(int argc, char **argv)
{
CustomItem *item = new CustomItem();
item->setPos(24, 24);
CustomItem *item2 = new CustomItem();
item2->setPos(80, 24);
scene.addItem(item);
scene.addItem(item2);
view.
setRenderHints( QPainter::Antialiasing );
view.show();
return app.exec();
}
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include "custom-item.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QGraphicsScene scene(0, 0, 300, 300);
CustomItem *item = new CustomItem();
item->setPos(24, 24);
CustomItem *item2 = new CustomItem();
item2->setPos(80, 24);
scene.addItem(item);
scene.addItem(item2);
QGraphicsView view(&scene);
view.setRenderHints( QPainter::Antialiasing );
view.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
I know it is possible to do it using the QGraphicsLayoutItem class. Can someone give me an example of how to do it?
Bookmarks