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. How to keep rectangle name to its center ?
Forum Updated to NodeBB v4.3 + New Features

How to keep rectangle name to its center ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 214 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.
  • T Offline
    T Offline
    tushu
    wrote on last edited by tushu
    #1

    I have a QGraphicsScene that contains rectangle.
    I know rectangles top left and bottom right x and y co-ordinates.
    I want to place name of rectangle such that, half name will come to left half of rectangle and half will come to right half of rectangle.

    LIke this :

    2ee39674-4b2e-4ce0-9a5a-132040385e6f-image.png

    I tried this way :

    QFontMetrics fontMetrics(QApplication::font());
        int textWidth = fontMetrics.width(instanceTextString) / 2; // Half of the name width
        int textHeight = fontMetrics.height() ;
    
        int rectWidthMidPoint = (x2  - x1 ) / 2;
        int textStartXPoint = rectWidthMidPoint - textWidth;
        int textStartYPoint = y1 - textHeight;
        text->DrawText(textStartXPoint  ,textStartYPoint );
    

    But name position is far to the left.
    How to solve it ?

    JoeCFDJ 1 Reply Last reply
    0
    • T tushu

      I have a QGraphicsScene that contains rectangle.
      I know rectangles top left and bottom right x and y co-ordinates.
      I want to place name of rectangle such that, half name will come to left half of rectangle and half will come to right half of rectangle.

      LIke this :

      2ee39674-4b2e-4ce0-9a5a-132040385e6f-image.png

      I tried this way :

      QFontMetrics fontMetrics(QApplication::font());
          int textWidth = fontMetrics.width(instanceTextString) / 2; // Half of the name width
          int textHeight = fontMetrics.height() ;
      
          int rectWidthMidPoint = (x2  - x1 ) / 2;
          int textStartXPoint = rectWidthMidPoint - textWidth;
          int textStartYPoint = y1 - textHeight;
          text->DrawText(textStartXPoint  ,textStartYPoint );
      

      But name position is far to the left.
      How to solve it ?

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

      @tushu

      int textStartXPoint = x1 + rectWidthMidPoint - textWidth;
      
      T 1 Reply Last reply
      2
      • JoeCFDJ JoeCFD

        @tushu

        int textStartXPoint = x1 + rectWidthMidPoint - textWidth;
        
        T Offline
        T Offline
        tushu
        wrote on last edited by
        #3

        @JoeCFD Thanks.

        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