Results 1 to 2 of 2

Thread: Pass Size and Text to a QGraphicsItem

  1. #1
    Join Date
    Feb 2010
    Posts
    27
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Pass Size and Text to a QGraphicsItem

    I have subclassed QGraphicsItem to draw a roundedRect with two lines of text in.

    I would like to pass the size and the text to the item so the item can be used dynamically for a number of different items.

    the code i have is below:


    Qt Code:
    1. class SimpleItem : public QGraphicsItem
    2. {
    3. public:
    4.  
    5. //SimpleItem(QString Title);
    6.  
    7. QRectF boundingRect() const
    8. {
    9. qreal penWidth = 1;
    10. return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
    11. 20 + penWidth, 20 + penWidth);
    12. }
    13.  
    14. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    15. QWidget *widget)
    16. {
    17. painter->drawRoundedRect(-10, -10, 100, 50, 5, 5);
    18. //painter->brush(Qt::blue);
    19. //QVariant title = title;
    20. painter->drawText(10, 10, "Title");
    21. painter->drawText(10, 30, "Subtitle");
    22. }
    23. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 94 Times in 86 Posts

    Default Re: Pass Size and Text to a QGraphicsItem

    What's the problem? Add a function to set the parameters and trigger an update(); provide a constuctor that takes the parameters as arguments and calls the previous function. Straight-up C++.

Similar Threads

  1. Replies: 0
    Last Post: 2nd August 2010, 09:37
  2. [SOLVED] Painting QGraphicsItem constant size
    By JovianGhost in forum Qt Programming
    Replies: 7
    Last Post: 22nd March 2010, 01:25
  3. get qgraphicsitem size
    By rogerholmes in forum Newbie
    Replies: 10
    Last Post: 18th February 2010, 05:04
  4. QGraphicsItem subclass access to QGraphicsView size
    By rubenvb in forum Qt Programming
    Replies: 4
    Last Post: 23rd January 2010, 21:36
  5. Replies: 1
    Last Post: 24th October 2006, 16:40

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
  •  
Qt is a trademark of The Qt Company.