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
{
public:
Jet();
QRectF boundingRect
() const Q_DECL_OVERRIDE;
QWidget* widget
= NULL) Q_DECL_OVERRIDE;
private:
int iOrigWidth = 0;
int iOrigHeight = 0;
qreal rScaler;
QString sFileName
= ":/Images/F22Raptor.png";
}
class Jet : public QGraphicsItem
{
public:
Jet();
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;
private:
int iOrigWidth = 0;
int iOrigHeight = 0;
qreal rScaler;
QString sFileName = ":/Images/F22Raptor.png";
QPixmap originalJet;
}
To copy to clipboard, switch view to plain text mode
Source code
Jet::Jet()
{
originalJet.load(sFileName);
rScaler = 16.0;
}
{
sourceRect = getRect();
scaledRect = getRect(rScaler);
pJetPainter
->setRenderHint
(QPainter::Antialiasing);
pJetPainter->drawPixmap(sourceRect, originalJet, scaledRect);
}
{
path.addRect(boundingRect());
return path;
}
QRectF Jet
::boundingRect() const {
scaledBox = getRect(rScaler); [COLOR="#FF0000"]** Error occurs occurs when attempting a build[/COLOR]
return scaledBox;
}
{
iOrigWidth = originalJet.width();
iOrigHeight = originalJet.height();
sourceRect.setWidth(iOrigWidth);
sourceRect.setHeight(iOrigHeight);
return sourceRect;
}
QRectF Jet
::getRect(qreal rFactor
) {
if(iOrigWidth == 0 || iOrigHeight == 0)
getRect();
qreal rScaledWidth = iOrigWidth / rFactor;
qreal rScaledHeight = iOrigHeight / rFactor;
scaledRect.setWidth(rScaledWidth);
scaledRect.setHeight(rScaledHeight);
return scaledRect;
}
Jet::Jet()
{
originalJet.load(sFileName);
rScaler = 16.0;
}
void Jet::Paint(QPainter* pJetPainter,
const QStyleOptionGraphicsItem* option,
QWidget* widget)
{
QRect sourceRect;
QRectF scaledRect;
sourceRect = getRect();
scaledRect = getRect(rScaler);
pJetPainter->setRenderHint(QPainter::Antialiasing);
pJetPainter->drawPixmap(sourceRect, originalJet, scaledRect);
}
QPainterPath Jet::shape() const
{
QPainterPath path;
path.addRect(boundingRect());
return path;
}
QRectF Jet::boundingRect() const
{
QRectF scaledBox;
scaledBox = getRect(rScaler); [COLOR="#FF0000"]** Error occurs occurs when attempting a build[/COLOR]
return scaledBox;
}
QRect Jet::getRect()
{
QRect sourceRect;
iOrigWidth = originalJet.width();
iOrigHeight = originalJet.height();
sourceRect.setWidth(iOrigWidth);
sourceRect.setHeight(iOrigHeight);
return sourceRect;
}
QRectF Jet::getRect(qreal rFactor)
{
QRectF scaledRect;
if(iOrigWidth == 0 || iOrigHeight == 0)
getRect();
qreal rScaledWidth = iOrigWidth / rFactor;
qreal rScaledHeight = iOrigHeight / rFactor;
scaledRect.setWidth(rScaledWidth);
scaledRect.setHeight(rScaledHeight);
return scaledRect;
}
To copy to clipboard, switch view to plain text mode
Bookmarks