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. Draw an object horizontally and with a vertical offset on QPainter
Qt 6.11 is out! See what's new in the release blog

Draw an object horizontally and with a vertical offset on QPainter

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 1.3k Views 2 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.
  • H Offline
    H Offline
    helenebro
    wrote on last edited by
    #1

    Hi,
    I want create a image composed of three elements : one image and two text. To do this, I use QPainter. I can draw elements but I can't succeed to correctly align them. I want each element align horizontally and placed below each other.

     ___________________
    |      _______      |
    |     | image |     |
    |     |_______|     |
    |                   |
    |     my text 1     |
    |     my text 2     |
    |___________________|
    

    For the moment, I placed them approximately.

    QImage image(152, 152, QImage::Format_ARGB32_Premultiplied);
    
    QPainter painter(&image);
    painter.fillRect(image.rect(), Qt::white);
    QImage pictureSensor("image.png");
    int posX = (image.width()-pictureSensor.width())/2;
    painter.drawImage(posX,10, pictureSensor);
    
    painter.drawText(30,110, "my text 1");
    
    painter.drawText(20,140, "my text2);
    
    image.save("output.png");
    

    I can horizontally placed text but in this case I can't set 'y' position :

        painter.drawText(image.rect(), Qt::AlignHCenter, QString("text"));
    

    Is there any solution ?

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

      @helenebro said:

      painter.drawText(image.rect(), Qt::AlignHCenter, QString("text"));

      Hi the image.rect() defines the rect where is aligns.
      So if you offset the rect.y (in a copy QRect)
      u can move start of area.

      Alternatively, you can use
      http://doc.qt.io/qt-5/qfontmetrics.html
      To get width() of you text and do the centering your self.

      1 Reply Last reply
      1

      • Login

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