Results 1 to 4 of 4

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

  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

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

    Default Re: Can't adjust font size of QGraphicsTextItem in QGraphicsRectItem

    Quote Originally Posted by leinad View Post
    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.

    CustomRect:
    CustomRectItem::CustomRectItem()
    {

    }

    //subclass QGraphicsRectItem to allow mouse events
    void CustomRectItem::mousePressEvent(QGraphicsSceneMous eEvent *event)
    {
    int xyCellSize = 20;

    if(event->isAccepted())
    {
    if(event->button() == Qt::LeftButton)
    {

    int x = ((event->scenePos().x() - sceneStartCornerX_) / xyCellSize) + 2; //add one to accomodate the cell for the pingee and pinger text - 32
    int y = ((event->scenePos().y() - sceneStartCornerY_) / xyCellSize) + 2; // 217
    Q_EMIT sendRectClicked(y, x);
    }
    }
    }
    void CustomRectItem::getViewCoordinates(int x, int y)
    {
    sceneStartCornerX_ = x;
    sceneStartCornerY_ = y;
    }

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

    In my main to change the fonts size:
    CustomRectItem *rect = new CustomRectItem;
    rect->setRect(12 + (XY_CELL_SIZE * column), sceneStartCornerY_ + (XY_CELL_SIZE * rowPlacement), XY_CELL_SIZE, XY_CELL_SIZE);
    QGraphicsTextItem *appIDText = new QGraphicsTextItem(rect);
    appIDText->setPos(12 + (XY_CELL_SIZE * column), sceneStartCornerY_ + (XY_CELL_SIZE * rowPlacement));
    QFont times("Times", 10);
    appIDText->setFont(times);
    appIDText->setPlainText(QString::number(appID));
    scene->addItem(rect);

    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!
    No one has any ideas?

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Can't adjust font size of QGraphicsTextItem in QGraphicsRectItem

    Have you set the QGraphicsItem::ItemIgnoresTransformations flag on your QGraphicsTextItem? This will prevent the QGraphicsRectItem from scaling the text according to its own scaling transformations (which are applied when the scene is fit into the view).

    No one has any ideas?
    You should be a little more patient. This forum has been slow lately, and not everyone reads it every day.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    Default Re: Can't adjust font size of QGraphicsTextItem in QGraphicsRectItem

    Quote Originally Posted by d_stranz View Post
    Have you set the QGraphicsItem::ItemIgnoresTransformations flag on your QGraphicsTextItem? This will prevent the QGraphicsRectItem from scaling the text according to its own scaling transformations (which are applied when the scene is fit into the view).



    You should be a little more patient. This forum has been slow lately, and not everyone reads it every day.
    My apologies. I'll take a look at what you mentioned. Thanks.

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

    d_stranz (3rd November 2021)

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.