Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Positioning drawtext inside a rectangle

    General and Desktop
    qgraphicsview scrollbar
    3
    4
    879
    Loading More Posts
    • 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
      lansing last edited by lansing

      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 Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        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 has to stay free or it will die.

        L 1 Reply Last reply Reply Quote 1
        • L
          lansing @Christian Ehrlicher last edited by

          @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 Reply Quote 0
          • A
            Asperamanca @lansing last edited by

            @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 Reply Quote 0
            • First post
              Last post