Results 1 to 2 of 2

Thread: QGraphicsRectItem with text

  1. #1
    Join Date
    Jan 2009
    Posts
    47
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QGraphicsRectItem with text

    I subclass QGraphicsRectItem in order to add a text inside the rectangle.
    1) The text doesn't get re-paint properly. When I created it as
    Qt Code:
    1. RectItem *myRect = new JFIRectItem("A Very Long Long Word");
    2. addItem(myRect);
    To copy to clipboard, switch view to plain text mode 
    it shows up on the QGraphicsScene fine, but the text is missing when I have it in a dropEvent
    Qt Code:
    1. RectItem *item = new RectItem(m_selectedJFIName.c_str());
    2. item->setPos(mouseEvent->scenePos());
    3. addItem(item);
    To copy to clipboard, switch view to plain text mode 
    2) How am I going to wrap the text so that it will fit inside the rectangle and to also re-size the rectangle as needed
    NodeWithText.PNG

    Qt Code:
    1. class RectItem : public QObject, public QGraphicsRectItem
    2. {
    3. Q_OBJECT
    4. public:
    5. RectItem (std::string text=0, QGraphicsItem *parent = 0);
    6. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    7. QWidget *widget = 0);
    8.  
    9. private:
    10. std::string m_text;
    11. };
    12.  
    13. RectItem::RectItem (std::string text, QGraphicsItem *parent)
    14. : QGraphicsRectItem(100, 100, 100, 30, parent)
    15. {
    16. m_text = text;
    17. }
    18.  
    19. void RectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    20. QWidget *widget)
    21. {
    22. QPointF pos = QPointF(rect().x() + 10, rect().y() + 20);
    23. painter->drawText(pos, m_text.c_str());
    24. painter->setPen(QPen(Qt::black, 1));
    25. painter->drawRoundRect(rect());
    26. }
    To copy to clipboard, switch view to plain text mode 

    Thanks.
    Last edited by ttvo; 26th August 2009 at 20:15.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QGraphicsRectItem with text

    You have to deal with QTextDocument, but it might be easier to subclass QGraphicsTextItem and draw the border...

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Replies: 0
    Last Post: 10th April 2009, 15:28
  3. Match the text beetween two string
    By dreamer in forum Qt Programming
    Replies: 4
    Last Post: 20th May 2008, 14:48
  4. QTextEdit slow to insert text
    By thomaspu in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 12:05
  5. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30

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.