Subclassing QGraphicsItem to work as a layer
Hello everyone! I have a little problem: after subclassing QGraphicsItem in this way:
Code:
#ifndef LAYER_H
#define LAYER_H
#include <QGraphicsItem>
#include <QPointF>
#include <QRectF>
#include <QPolygonF>
#include <QPainter>
#include <QPainterPath>
#include <QWidget>
{
public:
{
}
{
painter
->setRenderHint
(QPainter::HighQualityAntialiasing);
}
int getId();
bool setId(int layerId);
signals:
public slots:
private:
int layerId;
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!
Re: Subclassing QGraphicsItem to work as a layer
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).
Re: Subclassing QGraphicsItem to work as a layer
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...
Re: Subclassing QGraphicsItem to work as a layer
Quote:
Originally Posted by
kefir
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).
Quote:
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.
Re: Subclassing QGraphicsItem to work as a layer
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?
Re: Subclassing QGraphicsItem to work as a layer
Quote:
Originally Posted by
kefir
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.
Quote:
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.
Re: Subclassing QGraphicsItem to work as a layer
Thanks a lot!
Any suggestions on literature... or just QGraphicsView, scene and item reference from Qt Creator?
Re: Subclassing QGraphicsItem to work as a layer
Reference manual is a good start.