View Full Version : subclassing QGraphicsItem

8th February 2017, 20:54
I’m attempting to subclass QGraphicsItem as a class with a scaled down version of a pixmap representing an aircraft. I have implemented the pure virtual function paint() as well as boundingRect() and shape(). As far as I can tell these are the only three pure virtual functions of the QGraphicsItem class. The documentation seems to indicate that boundingRect() and paint() are the only two pure virtual public functions that need to be implemented, however it soon appeared that shape() needed to be re-implemented as well. I could not find a specific place in the documentation that lists pure virtual functions for each class. In any event, I continue to get an error stating that passing ‘const Jet’ as ‘this’ argument (full text of error follows). I understand that boundingRect is a const function, but I’m not sure what ‘this’ as an argument, is referring to.

The getRect function is an overloaded function that can operate on either an integer or a qreal value and return a rectangle in either an integer or floating point precision.

I've included a pared down version of the code below.

Text of error
jet.cpp:##: error: passing 'const Jet' as 'this' argument discards qualifiers [-fpermissive]
scaledBox = this->getRect(rScaler);
jet.cpp:1: In file included from jet.cpp:1:0:

note: in call to 'QRectF Jet::getRect(qreal)'
QRectF getRect(qreal rFactor);
Header File

class Jet : public QGraphicsItem

QRectF boundingRect() const Q_DECL_OVERRIDE;

QPainterPath shape() const Q_DECL_OVERRIDE;

void paint(QPainter* pJetPainter,
const QStyleOptionGraphicsItem* option = 0,
QWidget* widget = NULL) Q_DECL_OVERRIDE;
int iOrigWidth = 0;
int iOrigHeight = 0;
qreal rScaler;

QString sFileName = ":/Images/F22Raptor.png";
QPixmap originalJet;

Source code

rScaler = 16.0;

void Jet::Paint(QPainter* pJetPainter,
const QStyleOptionGraphicsItem* option,
QWidget* widget)
QRect sourceRect;
QRectF scaledRect;
sourceRect = getRect();
scaledRect = getRect(rScaler);

pJetPainter->drawPixmap(sourceRect, originalJet, scaledRect);

QPainterPath Jet::shape() const
QPainterPath path;
return path;

QRectF Jet::boundingRect() const
QRectF scaledBox;
scaledBox = getRect(rScaler); ** Error occurs occurs when attempting a build

return scaledBox;

QRect Jet::getRect()
QRect sourceRect;

iOrigWidth = originalJet.width();
iOrigHeight = originalJet.height();

return sourceRect;

QRectF Jet::getRect(qreal rFactor)
QRectF scaledRect;
if(iOrigWidth == 0 || iOrigHeight == 0)

qreal rScaledWidth = iOrigWidth / rFactor;
qreal rScaledHeight = iOrigHeight / rFactor;

return scaledRect;

9th February 2017, 04:42
The method Jet::boundingRect() const is a const method. It calls a non-const method getRect(qreal) violating the const-qualifier. Shouldn't it be const-qualified?

9th February 2017, 15:24
That was it! I spent too much time trying to figure that out and it was just obvious. I'm calling that a "Brain Fart of the Third Kind". :( Thank you so much for your response.

10th February 2017, 09:31
Btw, inside getRect(qreal) you are calling getRect() without using its return value.


10th February 2017, 19:19
Btw, inside getRect(qreal) you are calling getRect() without using its return value.

The OP is using the side effects of getRect() to set the member variables used later. This will be problematic for declaring these methods as const...

On the other hand, this can't be the real code, because most of the methods implemented in the "Source code" don't even appear in the header.