Results 1 to 5 of 5

Thread: How draw extra border on QGraphicsTextItem?

  1. #1
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default How draw extra border on QGraphicsTextItem?

    Qt Code:
    1. /*
    2. incomming QDomElement
    3. <divtext rotate="90" zindex="2" left="115" top="25" large="300" hight="400" >
    4. <param font="arial" color="#0000ff" size="30">Scribble XHTML Floating DIV box width:300px height:400px top:25px left:115px</param>
    5. </divtext>
    6. */
    7.  
    8. class FloatText : public QGraphicsTextItem
    9. {
    10. Q_OBJECT
    11. public:
    12. FloatText( QDomElement xmlstream , QGraphicsItem *parent, QGraphicsScene *scene )
    13. : QGraphicsTextItem(parent, scene)
    14. {
    15. modus = MODUS_MOVE;
    16. root = xmlstream;
    17. moveBy(GetInt("left"),GetInt("top"));
    18. rotate(GetInt("rotate"));
    19. f = QFont();
    20. int FontSize = root.firstChildElement("param").attributeNode("size").value().toInt();
    21. if (FontSize > 9) {
    22. f.setPixelSize(FontSize);
    23. } else {
    24. f.setPixelSize(10);
    25. }
    26. f.setFamily( root.firstChildElement("param").attributeNode("font").value() );
    27. setDefaultTextColor ( root.firstChildElement("param").attributeNode("color").value() );
    28. human = root.firstChildElement("param").text();
    29. setTextWidth ( GetInt("large") );
    30. setPlainText ( human );
    31. setFont(f);
    32. setZValue( GetInt("zindex") + 300 ); /* to make border - */
    33. SetMoveItem();
    34. QObject::connect(scene, SIGNAL(selectionChanged()), this, SLOT(WakeUpHere()));
    35. /////// ensureVisible(QRectF(GetInt("left"),GetInt("top"),GetInt("large"),GetInt("hight")),80,80);
    36. }
    37.  
    38. void paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget)
    39. {
    40. painter->drawRect (-2,-2,GetInt("large") + 4,8); /* GetInt("hight") + 4 border 2px out side overflow*/
    41. update();
    42. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How draw extra border on QGraphicsTextItem?

    I not make forward paint ..

    but now ony resize window display a correct border why?

    Qt Code:
    1. void paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget)
    2. {
    3. QPen pen;
    4. pen.setStyle( Qt::SolidLine );
    5. pen.setWidth( 2 );
    6. pen.setColor( option->palette.highlight().color() );
    7. /////scene()->clearSelection();
    8. ////prepareGeometryChange();
    9. painter->setPen( pen);
    10. painter->drawRect (-2,-2,GetInt("large") + 4,GetInt("hight") + 4);
    11. QGraphicsTextItem::paint(painter,option,widget);
    12. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How draw extra border on QGraphicsTextItem?

    All painting must be restricted to inside an item's bounding rect.
    J-P Nurmi

  4. #4
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How draw extra border on QGraphicsTextItem?

    Quote Originally Posted by jpn View Post
    All painting must be restricted to inside an item's bounding rect.
    If i set setTextWidth(300); his paint only on this area ... and text break on this limit correct.
    Now i must grab a way to become setTextHight(x) to display text only on this limit, like xhtml div overflow / hidden http://de.selfhtml.org/css/eigenscha...g.htm#overflow

    inside moveBy(10,10); and how i find the way to increase the bounding width whitout destroy textwidht pixel ?

    I not find SetboundingRect(X) area ... and margin? to increase this paint area? to make my border and limit text Width x Hight.

  5. #5
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How draw extra border on QGraphicsTextItem?

    You need to override boundingRect() to return enlarged rect. I did some sample code.
    This worked for me for all rotations and pixel sizes. Here it is.
    Guess this is what you wanted

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class FloatText : public QGraphicsTextItem
    4. {
    5. //Q_OBJECT;
    6. public:
    7. FloatText( QGraphicsScene *scene )
    8. : QGraphicsTextItem(0, scene)
    9. {
    10. setPlainText ( "human ");
    11. QFont f = font();
    12. f.setPixelSize(40);
    13. setFont(f);
    14. rotate(-90);
    15. setFlags(QGraphicsItem::ItemIsMovable);
    16. }
    17.  
    18. QRectF boundingRect() const
    19. {
    20. return QGraphicsTextItem::boundingRect().adjusted(-2,-2,+2,+2);
    21. }
    22.  
    23. void paint(QPainter *painter,const QStyleOptionGraphicsItem *option,QWidget *widget)
    24. {
    25. QGraphicsTextItem::paint(painter,option,widget);
    26. // Isn't this strange to call update in paint event ?
    27. //update();
    28. painter->setPen(Qt::black);
    29. painter->drawRect(boundingRect());
    30. }
    31.  
    32. };
    33.  
    34. int main(int argc, char *argv[])
    35. {
    36. QApplication app(argc,argv);
    37. QGraphicsScene scene(0,0,1024,768);
    38. FloatText t(&scene);
    39. t.setPos(500,500);
    40. QGraphicsView view(&scene);
    41. view.show();
    42. return app.exec();
    43. }
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

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.