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. Positioning drawtext inside a rectangle
Forum Update on Monday, May 27th 2025

Positioning drawtext inside a rectangle

Scheduled Pinned Locked Moved Unsolved General and Desktop
qgraphicsviewscrollbar
4 Posts 3 Posters 1.6k Views
  • 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
    lansing
    wrote on last edited by lansing
    #1

    Hi I wanted to draw some text inside a rectangle at different position and I ran into a problem. I have a paint function like this

    QRectF MyGraphicsItem::boundingRect() const
    {
        return QRectF(0, 0, 1000, 150);
    }
    
    void MyGraphicsItem::paint(QPainter *painter)
    {
        QPoint pos1(0, 0);
        QPoint pos2(50, 0);
        QPoint pos3(1500, 0);
    
        painter->drawText(pos1, QString("0"));
        painter->drawText(pos2, QString("50"));
        painter->drawText(pos3, QString("1500"));
    }
    

    mainwindow.cpp

        scene = new QGraphicsScene(this);
        myItem= new MyGraphicsItem;
        scene->addItem(myItem);
    
        ui->graphicsView->setScene(scene);
    

    The last Qpooint position of my texts is longer than the width of my qgraphicsview, but my program is not showing a horizontal scroll bar. I tried changing the horizontal sizing policy in the ui but none of them works.

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Where should the scrollbar appear? You're returning a bounding rect smaller than your coordinates for your third string so it will not be visible.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      L 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        Where should the scrollbar appear? You're returning a bounding rect smaller than your coordinates for your third string so it will not be visible.

        L Offline
        L Offline
        lansing
        wrote on last edited by
        #3

        @Christian-Ehrlicher said in Positioning drawtext inside a rectangle:

        Where should the scrollbar appear? You're returning a bounding rect smaller than your coordinates for your third string so it will not be visible.

        Thanks, I tried setting the x position of the third Qpoint and the width of the QgraphicsView in the ui to be less than the bounding rectangle, then the scroll bar came out.

        I'm going to populate and draw the Qpoints from a loop so some of the coordinates may be very large, so I should be declaring my bounding rectangle to be larger than that like QRectF(0, 0, 9999, 100)?

        A 1 Reply Last reply
        0
        • L lansing

          @Christian-Ehrlicher said in Positioning drawtext inside a rectangle:

          Where should the scrollbar appear? You're returning a bounding rect smaller than your coordinates for your third string so it will not be visible.

          Thanks, I tried setting the x position of the third Qpoint and the width of the QgraphicsView in the ui to be less than the bounding rectangle, then the scroll bar came out.

          I'm going to populate and draw the Qpoints from a loop so some of the coordinates may be very large, so I should be declaring my bounding rectangle to be larger than that like QRectF(0, 0, 9999, 100)?

          A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #4

          @lansing
          I'm not sure what you are trying to achieve.
          Normally, I would calculate the correct bounding rect (and ideally cache the result as long as nothing changes, so calls to boundingRect() stay fast)

          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