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. Creating a widget which can hold an image and text

Creating a widget which can hold an image and text

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

    I have created a widget which contain an image and a text, also there is a space between the image and the text.

    I am painting the image and text in the paintevent but it is not aligned properly.

    NB: I want the image + " space " + text placed exactly at the middle..

    Please help me in this

       QPainter painter(this);
    QRect paintRect = rect();
    QPixmap pixmap = icon().pixmap(m_frameSize);
    painter.fillRect(rect(), QColor(170, 30, 30));   // filling the widget with background color
    
    int iconXCoordinate = (paintRect.width()) / 4;
    int iconYCoordinate = (paintRect.height() - pixmap.height()) / 2;
    painter.drawPixmap(iconXCoordinate,iconYCoordinate,pixmap);  //drawing image
    painter.setPen(QColor(255, 255, 255));
    int textXCoordinate = iconXCoordinate + pixmap.width() +2 ; //2 pixel space
    int textYCoordinate = iconYCoordinate;
    QFontMetrics fontMetrics = painter.fontMetrics();
    QRect fontRect = fontMetrics.tightBoundingRect(m_Text);
    painter.drawText((paintRect.width()/2), textYCoordinate + round( ( (double) fontRect.height())), m_Text);
    

    This is not brining the image and text at the centre of the rectange. can you provide any logic to achieve thus

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

      Hi,

      The coordinate system is not based on the center of what you want to paint.

      Please take a look at the corresponding chapter in Qt's documentation.

      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

      • Login

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