Results 1 to 4 of 4

Thread: Can't adjust font size of QGraphicsTextItem in QGraphicsRectItem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2019
    Posts
    32
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Can't adjust font size of QGraphicsTextItem in QGraphicsRectItem

    Hi,

    I created a custom QGraphicsRectItem to allow me to handle mouse events which works fine. The issue I'm having is when I try to set the font of a QGraphicsTextItem which happens to be in my custom QGraphicsRectItem, it won't change I can't even change the text width. Below is a sample code.

    Qt Code:
    1. CustomRect:
    2. CustomRectItem::CustomRectItem()
    3. {
    4.  
    5. }
    6.  
    7. //subclass QGraphicsRectItem to allow mouse events
    8. void CustomRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
    9. {
    10. int xyCellSize = 20;
    11.  
    12. if(event->isAccepted())
    13. {
    14. if(event->button() == Qt::LeftButton)
    15. {
    16.  
    17. int x = ((event->scenePos().x() - sceneStartCornerX_) / xyCellSize) + 2; //add one to accomodate the cell for the pingee and pinger text - 32
    18. int y = ((event->scenePos().y() - sceneStartCornerY_) / xyCellSize) + 2; // 217
    19. Q_EMIT sendRectClicked(y, x);
    20. }
    21. }
    22. }
    23. void CustomRectItem::getViewCoordinates(int x, int y)
    24. {
    25. sceneStartCornerX_ = x;
    26. sceneStartCornerY_ = y;
    27. }
    To copy to clipboard, switch view to plain text mode 

    ------------------------------

    In my main to change the fonts size:
    Qt Code:
    1. CustomRectItem *rect = new CustomRectItem;
    2. rect->setRect(12 + (XY_CELL_SIZE * column), sceneStartCornerY_ + (XY_CELL_SIZE * rowPlacement), XY_CELL_SIZE, XY_CELL_SIZE);
    3. QGraphicsTextItem *appIDText = new QGraphicsTextItem(rect);
    4. appIDText->setPos(12 + (XY_CELL_SIZE * column), sceneStartCornerY_ + (XY_CELL_SIZE * rowPlacement));
    5. QFont times("Times", 10);
    6. appIDText->setFont(times);
    7. appIDText->setPlainText(QString::number(appID));
    8. scene->addItem(rect);
    To copy to clipboard, switch view to plain text mode 
    The text is in the rect just can't change the font size. Can someone please explain to me what I'm doing wrong?

    Thanks!
    Last edited by d_stranz; 3rd November 2021 at 15:56. Reason: missing [code] tags

Similar Threads

  1. Replies: 1
    Last Post: 30th March 2012, 17:46
  2. QGraphicsRectItem and QGraphicsTextItem.
    By cydside in forum Qt Programming
    Replies: 13
    Last Post: 20th July 2009, 13:11
  3. Font size in QGraphicsTextItem
    By rippa in forum Qt Programming
    Replies: 1
    Last Post: 7th December 2008, 20:11
  4. adjust font size to QLabel-size
    By hunsrus in forum Qt Programming
    Replies: 0
    Last Post: 9th July 2008, 15:33
  5. adjust Font size to a given rect when drawText
    By yartov in forum Qt Programming
    Replies: 5
    Last Post: 14th May 2008, 20:03

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.