Results 1 to 5 of 5

Thread: subclassing QGraphicsItem

  1. #1
    Join Date
    Nov 2016
    Location
    Ridgecrest California
    Posts
    33
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default subclassing QGraphicsItem

    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 

  2. #2
    Join Date
    Dec 2016
    Location
    New England, US
    Posts
    31
    Thanks
    6
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: subclassing QGraphicsItem

    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?

  3. #3
    Join Date
    Nov 2016
    Location
    Ridgecrest California
    Posts
    33
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: subclassing QGraphicsItem

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: subclassing QGraphicsItem

    Btw, inside getRect(qreal) you are calling getRect() without using its return value.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: subclassing QGraphicsItem

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Subclassing QGraphicsItem to work as a layer
    By kefir in forum Qt Programming
    Replies: 7
    Last Post: 10th March 2011, 10:42
  2. Replies: 7
    Last Post: 29th November 2010, 20:20
  3. subclassing QGraphicsItem, QGraphicsPixmapItem
    By rogerholmes in forum Newbie
    Replies: 3
    Last Post: 27th August 2009, 00:12
  4. QGraphicsItem Subclassing
    By QbelcorT in forum Newbie
    Replies: 4
    Last Post: 19th November 2008, 02:06
  5. Casting QGraphicsItem child from QGraphicsItem
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 29th August 2008, 16:37

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.