Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't adjust font size of QGraphicsTextItem in QGraphicsRectItem
Qt 6.11 is out! See what's new in the release blog

Can't adjust font size of QGraphicsTextItem in QGraphicsRectItem

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    leinad
    wrote on last edited by
    #1

    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(QGraphicsSceneMouseEvent *event)
    {
    int xyCellSize = 20;

    if(event->isAccepted())
    {
       if(event->button() == Qt::LeftButton)
       {
           //convert scenePos to row/col values
    
        //   qDebug() << event->scenePos().x() <<  event->scenePos().y();
           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
        //   qDebug() << "row = " << y << "col = " << x;
           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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What if you create an item with its text content rather than its size ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leinad
        wrote on last edited by
        #3

        I'm not sure what you mean. Can you please provide a simple example? I create a QGraphicsRectItem and add an QGraphicsTextItem to it. Is that not valid?

        1 Reply Last reply
        0
        • JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by
          #4

          you may need to override
          void QGraphicsRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr)
          to paint your text with different font size.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @leinad said in Can't adjust font size of QGraphicsTextItem in QGraphicsRectItem:

            QGraphicsTextItem *appIDText = new QGraphicsTextItem(rect);

            Pass the text rather than a text here.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • L Offline
              L Offline
              leinad
              wrote on last edited by
              #6

              If you mean:
              QGraphicsTextItem *appIDText = new QGraphicsTextItem(QString::number(appID));
              appIDText->setPos(12 + (XY_CELL_SIZE * column), sceneStartCornerY_ + (XY_CELL_SIZE * rowPlacement));
              QFont times("Times", 10);
              times.setPointSize(2);
              appIDText->setFont(times);

              I get the same results :(

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Do you have the same issue if you just add a QGraphicsTextItem to your scene ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  leinad
                  wrote on last edited by
                  #8

                  Yes, I tried this and same results

                  // QGraphicsTextItem *appIDText = new QGraphicsTextItem(rect);
                  QGraphicsTextItem *appIDText = new QGraphicsTextItem(QString::number(appID));
                  appIDText->setPos(12 + (XY_CELL_SIZE * column), sceneStartCornerY_ + (XY_CELL_SIZE * rowPlacement));
                  QFont times("Times", 10);
                  times.setPointSize(2);
                  appIDText->setFont(times);
                  // appIDText->setPlainText(QString::number(appID));
                  scene->addItem(appIDText);

                  JoeCFDJ 1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Why are you setting the point size to two after creating your QFont object with a size of 10 ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • L leinad

                      Yes, I tried this and same results

                      // QGraphicsTextItem *appIDText = new QGraphicsTextItem(rect);
                      QGraphicsTextItem *appIDText = new QGraphicsTextItem(QString::number(appID));
                      appIDText->setPos(12 + (XY_CELL_SIZE * column), sceneStartCornerY_ + (XY_CELL_SIZE * rowPlacement));
                      QFont times("Times", 10);
                      times.setPointSize(2);
                      appIDText->setFont(times);
                      // appIDText->setPlainText(QString::number(appID));
                      scene->addItem(appIDText);

                      JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by
                      #10

                      @leinad override paint and set font to painter. It will work.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved