Results 1 to 14 of 14

Thread: QGraphicsRectItem and QGraphicsTextItem.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    19
    Thanked 2 Times in 2 Posts

    Default Re: QGraphicsRectItem and QGraphicsTextItem.

    Quote Originally Posted by wagmare View Post
    create a custom QGraphicsItem with both QGraphicsRect and QGraphicsText
    and in QGraphicsView class (parent)
    QTransform transform;
    transform.rotate( 90);
    grpahicsItem->setTransform(transform);
    Ok, It seems a good solution, but how to obtain a text centered both vetically and horizontally in the rectangle?

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    116
    Thanked 42 Times in 41 Posts

    Angry Re: QGraphicsRectItem and QGraphicsTextItem.

    but how to obtain a text centered both vetically and horizontally in the rectangle?
    thats what i am suggesting .. combine both the text and rectangle into single QGraphicsItem ..

    paint such that
    create a custom QGraphicsItem()
    Qt Code:
    1. class CustomItem : public QGraphicsRectItem
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. CustomItem(const QRectF &rect);
    7. void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *);
    8. QRectF boundingRect();
    9.  
    10. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. CustomItem::CustomItem(const QRectF &rect)
    2. {
    3. textItem->setDefaultTextColor(QColor(255,255,255,255));
    4. textItem->setPos(x, y); //here u set position of text
    5. textItem->setPlainText(message); //set your text here
    6. }
    7.  
    8. void CustomItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *o, QWidget *widget)
    9. {
    10. painter->setBrush(QBrush(QColor(21, 155, 210, 255)));
    11. painter->drawRect(rect());
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    now the item is ready in horizontal ... now in parent graphicsView() rotate it using previous code ...
    "Behind every great fortune lies a crime" - Balzac

Similar Threads

  1. Subclassing QGraphicsTextItem and QGraphicsLayoutItem
    By psih128 in forum Qt Programming
    Replies: 2
    Last Post: 15th October 2008, 21:12
  2. resizing a qgraphicstextitem
    By dreamer in forum Qt Programming
    Replies: 6
    Last Post: 22nd May 2008, 16:43

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.