Results 1 to 2 of 2

Thread: own subclass of QGraphicsItem problem

  1. #1
    Join Date
    May 2013
    Posts
    1
    Thanked 1 Time in 1 Post

    Default own subclass of QGraphicsItem problem

    I've started implementing my own subclass of QGraphicsItem:

    Qt Code:
    1. #ifndef PAWN_H
    2. #define PAWN_H
    3.  
    4. #include <QtGui>
    5.  
    6. class Pawn : public QGraphicsItem
    7. {
    8. private:
    9. static const int FIGURE_SIZE = 50;
    10.  
    11. public:
    12. QRectF boundingRect() const;
    13. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    14. };
    15.  
    16. #endif // PAWN_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QRectF Pawn::boundingRect() const
    2. {
    3. return QRectF(this->x(), this->y(), FIGURE_SIZE, FIGURE_SIZE);
    4. }
    5.  
    6. void Pawn::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    7. {
    8. painter->drawRect(boundingRect());
    9. }
    To copy to clipboard, switch view to plain text mode 

    Then I've added two items to QGraphicsScene:

    Qt Code:
    1. Pawn* first = new Pawn();
    2. first->setPos(0, 0);
    3. Pawn* second = new Pawn();
    4. second->setPos(0, 50);
    5. mainScene.addItem(first);
    6. mainScene.addItem(second);
    To copy to clipboard, switch view to plain text mode 

    It looks as follows:
    screen.png

    Why there is a gap between these two rectangles? Top left vertex of first rectangle is (0, 0) and it's heigth is 50. Top left vertex of second rectangle is (0, 50). They should have a common edge.

  2. The following user says thank you to Azras for this useful post:


  3. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: own subclass of QGraphicsItem problem

    Qt Code:
    1. QRectF Pawn::boundingRect() const
    2. {
    3. return QRectF(0, 0, FIGURE_SIZE, FIGURE_SIZE);
    4. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  4. The following user says thank you to Santosh Reddy for this useful post:


Similar Threads

  1. Replies: 2
    Last Post: 15th April 2013, 06:33
  2. QGraphicsItem subclass compilation error
    By ^NyAw^ in forum Qt Programming
    Replies: 10
    Last Post: 30th January 2013, 14:55
  3. Subclass Problem
    By rleojoseph in forum Newbie
    Replies: 21
    Last Post: 4th February 2011, 06:43
  4. QGraphicsItem subclass and accessing custom properties
    By been_1990 in forum Qt Programming
    Replies: 4
    Last Post: 19th November 2010, 01:48
  5. QGraphicsItem subclass access to QGraphicsView size
    By rubenvb in forum Qt Programming
    Replies: 4
    Last Post: 23rd January 2010, 21:36

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.