Results 1 to 14 of 14

Thread: QGraphicsRectItem and QGraphicsTextItem.

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

    Default QGraphicsRectItem and QGraphicsTextItem.

    Hi to all,
    I'm trying to build a rectangle (QGraphicsRectItem) with a text inside (QGraphicsTextItem) perfectly centered both vetically and horizontally as child, if it's the best solution. I've tried the following code:

    Qt Code:
    1. for (int i = 0; i < 12; i++)
    2. {
    3. new QGraphicsRectItem(QRect(margineX + 60 * i, endY - margineY + 2, 60, 30), 0, scene);
    4. month->setPen(QPen(Qt::red, 1, Qt::SolidLine));
    5. month->setBrush(Qt::NoBrush);
    6.  
    7. QGraphicsTextItem *textMonth =
    8. new QGraphicsTextItem(QString("%1").arg(i + 1), month);
    9. }
    To copy to clipboard, switch view to plain text mode 

    and...it doesn't work. Thanks in advance!

  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 and QGraphicsTextItem.

    It's easier to subclass the paint method of the text item and draw a border yourself. Then call the base class paint method for the text drawing.

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

    Default Re: QGraphicsRectItem and QGraphicsTextItem.

    Thanks Lykurg. do you know where can I find an example? if it's not so much trouble

  4. #4
    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 and QGraphicsTextItem.

    It's always better to try by yourself,but if you really want for that simple task:
    Qt Code:
    1. class myItem : public QGraphicsTextItem
    2. {
    3. public:
    4. myItem(QGraphicsItem *parent = 0) : QGraphicsTextItem(parent)
    5. {}
    6. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0)
    7. {
    8. painter->setPen(Qt::black);
    9. painter->setBrush(QColor(230,230,230));
    10. painter->drawRect(option->rect);
    11. QGraphicsTextItem::paint(painter, option, widget);
    12. }
    13. };
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to Lykurg for this useful post:

    cydside (19th July 2009)

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

    Default Re: QGraphicsRectItem and QGraphicsTextItem.

    Thanks again Likurg, I've used your code but I've not centered the problem cause my incomplete explaination. So, I'd like to build histograms (QGraphicsRectItem) and write the value inside the rectangle (see the attachment) and I'm going on trouble to find something that works. Any idea?
    Attached Images Attached Images

  7. #6
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsRectItem and QGraphicsTextItem.

    just use qpainter transforms to write vertical text

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

    Default Re: QGraphicsRectItem and QGraphicsTextItem.

    create a custom QGraphicsItem with both QGraphicsRect and QGraphicsText
    and in QGraphicsView class (parent)
    QTransform transform;
    transform.rotate( 90);
    grpahicsItem->setTransform(transform);
    "Behind every great fortune lies a crime" - Balzac

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

    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?

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

    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

  11. #10
    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 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);
    Instead of setting the transformation to each item you use and in your particular case, it's better to create your own item and draw the text yourself:
    In paint also draw the text via QPainter::drawText() (there are alignment flags) and then rotate the painter by 90°. And don't forget to implement the sizeHint() function.

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

    Default Re: QGraphicsRectItem and QGraphicsTextItem.

    yes lykurg .. its a better option .. in paint() itself we can rotate the text and add the text in that custom item ...
    "Behind every great fortune lies a crime" - Balzac

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

    Default Re: QGraphicsRectItem and QGraphicsTextItem.

    Quote Originally Posted by Lykurg View Post
    Instead of setting the transformation to each item you use and in your particular case, it's better to create your own item and draw the text yourself:
    In paint also draw the text via QPainter::drawText() (there are alignment flags) and then rotate the painter by 90°. And don't forget to implement the sizeHint() function.
    QPainter will be the best solution when the rect area is smallest than the text, but if I try to create a QPainter object for that QGraphicsScene as the following code:
    Qt Code:
    1. new QGraphicsScene(QRect(0, 0, endX, endY), this);
    2.  
    3. for (int i = 0; i < 12; i++)
    4. {
    5. new QGraphicsRectItem(QRect(margineX + 2 + 60 * i, endY - margineY + 2, 60, 30), 0, scene);
    6. // mese->setPen(QPen(Qt::red, 1, Qt::SolidLine));
    7. mese->setPen(Qt::NoPen);
    8. mese->setBrush(Qt::NoBrush);
    9.  
    10.  
    11. QPainter painter(scene);
    12. painter.drawText(mese, Qt::AlignCenter, mesiAnno.at(i));
    13. painter.drawRect(mese);
    To copy to clipboard, switch view to plain text mode 

    the compiler is not agree with me because:

    Qt Code:
    1. error: no matching function for call to `QPainter::QPainter(QGraphicsScene*&)'
    2. note: candidates are: QPainter::QPainter(const QPainter&)
    3. note: QPainter::QPainter(QPaintDevice*)
    To copy to clipboard, switch view to plain text mode 

    where do i have to link my QPainter object???

    thanks guys, I really appreciate your comments!

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

    Default Re: QGraphicsRectItem and QGraphicsTextItem.

    where u are using the painter ...
    if in QGraphicsView () class
    then paint inside

    void QGraphicsView::drawBackground

    if in QGraphicsItem() class
    then paint inside

    void QGraphicsItem:aint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 )
    "Behind every great fortune lies a crime" - Balzac

  15. The following user says thank you to wagmare for this useful post:

    cydside (20th July 2009)

  16. #14
    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 and QGraphicsTextItem.

    That is what I have meant by, do stuff (subclassing) yourself and try to understand!

    Use my first example code and do the painting inside the paint function. You don't want to paint on the screen, you want to the item, and that is done inside the item!

  17. The following user says thank you to Lykurg for this useful post:

    cydside (20th July 2009)

Similar Threads

  1. Subclassing QGraphicsTextItem and QGraphicsLayoutItem
    By psih128 in forum Qt Programming
    Replies: 2
    Last Post: 15th October 2008, 20:12
  2. resizing a qgraphicstextitem
    By dreamer in forum Qt Programming
    Replies: 6
    Last Post: 22nd May 2008, 15: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.