Thanks for your engagement, but I don't seen go get anywhere.
I managed to implement a subclass of QGraphicsItem, but it does not draw, I have no idea why. Could you help me?
#ifndef SHAPEITEM_H
#define SHAPEITEM_H
#include <QtGui>
#include <QGraphicsItem>
#include <QObject>
{
public:
ShapeItem(void);
ShapeItem
(const QRect *rect
);
private:
int x0,y0,x1,y1,x2,y2,x3,y3;
int xmin,ymin,xmax,ymax;
};
#endif
#ifndef SHAPEITEM_H
#define SHAPEITEM_H
#include <QtGui>
#include <QGraphicsItem>
#include <QObject>
class ShapeItem : public QGraphicsItem
{
public:
ShapeItem(void);
ShapeItem(const QRect *rect);
ShapeItem(const QPolygon *poly);
QRectF boundingRect() const;
QPainterPath shape(void) const;
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget);
private:
int x0,y0,x1,y1,x2,y2,x3,y3;
int xmin,ymin,xmax,ymax;
};
#endif
To copy to clipboard, switch view to plain text mode
#include "shapeitem.h"
ShapeItem
::ShapeItem(const QRect *rect
){
x0 = rect->x();
y0 = rect->y();
x1 = rect->x() + rect->width();
y1 = y0;
x2 = x1;
y2 = y0 + rect->height();
x3 = x0;
y3 = y2;
xmin = x0;
xmax = x1;
ymin = y0;
ymax = y2;
}
ShapeItem
::ShapeItem(const QPolygon *poly
){
poly->point(0,&x0,&y0);
poly->point(1,&x1,&y1);
poly->point(2,&x2,&y2);
poly->point(3,&x3,&y3);
xmin = xmax = x0;
ymin = ymax = y0;
if(x1<xmin) xmin = x1;
if(x2<xmin) xmin = x2;
if(x3<xmin) xmin = x3;
if(y1<ymin) ymin = y1;
if(y2<ymin) ymin = y2;
if(y3<ymin) ymin = y3;
if(x1>xmax) xmax = x1;
if(x2>xmax) xmax = x2;
if(x3>xmax) xmax = x3;
if(y1>ymax) ymax = y1;
if(y2>ymax) ymax = y2;
if(y3>ymax) ymax = y3;
}
ShapeItem::boundingRect() const
{
qreal penWidth = 1;
return QRectF(xmin
- penWidth
/ 2, ymin
- penWidth
/ 2,
xmax + penWidth / 2, ymax + penWidth / 2);
}
ShapeItem::shape() const
{
path.addRect(boundingRect());
return path;
}
void
{
painter->drawLine ( x0,y0,x1,y1 );
painter->drawLine ( x1,y1,x2,y2 );
painter->drawLine ( x2,y2,x3,y3 );
painter->drawLine ( x3,y3,x0,y0 );
}
#include "shapeitem.h"
ShapeItem::ShapeItem(const QRect *rect)
: QGraphicsItem()
{
x0 = rect->x();
y0 = rect->y();
x1 = rect->x() + rect->width();
y1 = y0;
x2 = x1;
y2 = y0 + rect->height();
x3 = x0;
y3 = y2;
xmin = x0;
xmax = x1;
ymin = y0;
ymax = y2;
}
ShapeItem::ShapeItem(const QPolygon *poly)
: QGraphicsItem()
{
poly->point(0,&x0,&y0);
poly->point(1,&x1,&y1);
poly->point(2,&x2,&y2);
poly->point(3,&x3,&y3);
xmin = xmax = x0;
ymin = ymax = y0;
if(x1<xmin) xmin = x1;
if(x2<xmin) xmin = x2;
if(x3<xmin) xmin = x3;
if(y1<ymin) ymin = y1;
if(y2<ymin) ymin = y2;
if(y3<ymin) ymin = y3;
if(x1>xmax) xmax = x1;
if(x2>xmax) xmax = x2;
if(x3>xmax) xmax = x3;
if(y1>ymax) ymax = y1;
if(y2>ymax) ymax = y2;
if(y3>ymax) ymax = y3;
}
QRectF
ShapeItem::boundingRect() const
{
qreal penWidth = 1;
return QRectF(xmin - penWidth / 2, ymin - penWidth / 2,
xmax + penWidth / 2, ymax + penWidth / 2);
}
QPainterPath
ShapeItem::shape() const
{
QPainterPath path;
path.addRect(boundingRect());
return path;
}
void
ShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
painter->drawLine ( x0,y0,x1,y1 );
painter->drawLine ( x1,y1,x2,y2 );
painter->drawLine ( x2,y2,x3,y3 );
painter->drawLine ( x3,y3,x0,y0 );
}
To copy to clipboard, switch view to plain text mode
Not at all optimized, as soon as it draws me anything in the scene, I will test different variants. At this stage, numItems=0 in myQGraphicsItems::drawIntems, so I'm stuck.
I am using it lile this:
ShapeItem item(&outRect);
scene->addItem(&item);
//scene->addRect(outRect,solidBlue);
ShapeItem item(&outRect);
scene->addItem(&item);
//scene->addRect(outRect,solidBlue);
To copy to clipboard, switch view to plain text mode
The commented-out line draws as it should, while the active lines does not.
Bookmarks