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
Qt Code:
  1. class Jet : public QGraphicsItem
  2. {
  3. public:
  4. Jet();
  5.  
  6. QRectF boundingRect() const Q_DECL_OVERRIDE;
  7.  
  8. QPainterPath shape() const Q_DECL_OVERRIDE;
  9.  
  10. void paint(QPainter* pJetPainter,
  11. const QStyleOptionGraphicsItem* option = 0,
  12. QWidget* widget = NULL) Q_DECL_OVERRIDE;
  13. private:
  14. int iOrigWidth = 0;
  15. int iOrigHeight = 0;
  16. qreal rScaler;
  17.  
  18. QString sFileName = ":/Images/F22Raptor.png";
  19. QPixmap originalJet;
  20. }
To copy to clipboard, switch view to plain text mode 

Source code
Qt Code:
  1. Jet::Jet()
  2. {
  3. originalJet.load(sFileName);
  4. rScaler = 16.0;
  5. }
  6.  
  7. void Jet::Paint(QPainter* pJetPainter,
  8. const QStyleOptionGraphicsItem* option,
  9. QWidget* widget)
  10. {
  11. QRect sourceRect;
  12. QRectF scaledRect;
  13. sourceRect = getRect();
  14. scaledRect = getRect(rScaler);
  15.  
  16. pJetPainter->setRenderHint(QPainter::Antialiasing);
  17. pJetPainter->drawPixmap(sourceRect, originalJet, scaledRect);
  18. }
  19.  
  20. QPainterPath Jet::shape() const
  21. {
  22. path.addRect(boundingRect());
  23. return path;
  24. }
  25.  
  26. QRectF Jet::boundingRect() const
  27. {
  28. QRectF scaledBox;
  29. scaledBox = getRect(rScaler); [COLOR="#FF0000"]** Error occurs occurs when attempting a build[/COLOR]
  30.  
  31. return scaledBox;
  32. }
  33.  
  34. QRect Jet::getRect()
  35. {
  36. QRect sourceRect;
  37.  
  38. iOrigWidth = originalJet.width();
  39. iOrigHeight = originalJet.height();
  40. sourceRect.setWidth(iOrigWidth);
  41. sourceRect.setHeight(iOrigHeight);
  42.  
  43. return sourceRect;
  44. }
  45.  
  46. QRectF Jet::getRect(qreal rFactor)
  47. {
  48. QRectF scaledRect;
  49. if(iOrigWidth == 0 || iOrigHeight == 0)
  50. getRect();
  51.  
  52. qreal rScaledWidth = iOrigWidth / rFactor;
  53. qreal rScaledHeight = iOrigHeight / rFactor;
  54. scaledRect.setWidth(rScaledWidth);
  55. scaledRect.setHeight(rScaledHeight);
  56.  
  57. return scaledRect;
  58. }
To copy to clipboard, switch view to plain text mode