PDA

View Full Version : Subclassing QGraphicsItem to work as a layer



kefir
9th March 2011, 17:53
Hello everyone! I have a little problem: after subclassing QGraphicsItem in this way:


#ifndef LAYER_H
#define LAYER_H
#include <QGraphicsItem>
#include <QPointF>
#include <QRectF>
#include <QPolygonF>
#include <QPainter>
#include <QPainterPath>
#include <QWidget>
class Layer : public QGraphicsItem
{
public:
Layer(int layerId = -1, QString layerName = "", QGraphicsItem *parent = 0);
QRectF boundingRect() const
{
return QRectF(QPoint(0,0), QSizeF(10000.0, 10000.0)); //size can change
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
painter->setRenderHint(QPainter::HighQualityAntialiasing);
}
int getId();
bool setId(int layerId);
QString getName();
bool setName(QString layerName);
signals:
public slots:
private:
int layerId;
QString layerName;
protected:
// void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
};
#endif // LAYER_H


The Layer class should work as a container for map tiles 256x256 px each. I got tiles displayed properly in a QGraphicsScene but when I add a Layer into it and then add same map tiles I get a blank screen. Debugging however shows that my parent Label contains correct number of tiles in it.

Thanx in advance!

wysota
9th March 2011, 18:00
Your drawing code doesn't make sense. Your boundingRect code is not any good either. Painting should be done in local coordinates defined by the boundingRect. It can't depend on the position of the item relative to its parent (which is what pos() represents).

kefir
9th March 2011, 18:06
Sorry about these lines. I'll comment them. but in fact they did draw a line accoording to pos().
boundinBox() is the thing that keeps on puzzling me. I don't quite get how to reimplement this function knowing that size of it may change... or does it mater in my case..

I've updated the head message...

wysota
9th March 2011, 22:35
Sorry about these lines. I'll comment them. but in fact they did draw a line accoording to pos().
Probably only because at the time of calling boundingRect() the value returned by pos() was (0,0).


boundinBox() is the thing that keeps on puzzling me. I don't quite get how to reimplement this function knowing that size of it may change... or does it mater in my case..
Nobody said the size returned by boundingRect() should be constant across the lifetime of the item. It's just important that when bounding rect changes, prepareGeometryChange() has to be called.

kefir
10th March 2011, 00:01
Ok... And so where may lay the source of my problem of child item invisibility?
Should I reimplement itemChange() and put prepareGeometryChange() or should I trace a moment when a child item is added and call it just before then?

wysota
10th March 2011, 00:05
Ok... And so where may lay the source of my problem of child item invisibility?
Probably in the bogus implementation of boundingRect of their parent.


Should I reimplement itemChange() and put prepareGeometryChange() or should I trace a moment when a child item is added and call it just before then?
You should start by reading the docs and understanding how Graphics View's coordinate system works. It will save you a lot of trouble. But in general your layer item should just be a QGraphicsItemGroup.

kefir
10th March 2011, 00:14
Thanks a lot!
Any suggestions on literature... or just QGraphicsView, scene and item reference from Qt Creator?

wysota
10th March 2011, 09:42
Reference manual is a good start.