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. Setting non-expanding text on an expanding Image
Forum Updated to NodeBB v4.3 + New Features

Setting non-expanding text on an expanding Image

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 384 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.
  • U Offline
    U Offline
    Uday More
    wrote on last edited by
    #1

    I have a Qimage on Qlabel which expands based on the window expansion. Hence the Image also expands.
    I have set simple text into the QImage using Qpainter as below

    QPainter* painter = new QPainter(&myImage);
    painter->setPen(Qt::red);
    painter->setFont(QFont("Arial", 5));
    painter->drawText(myImage.rect(), Qt::AlignLeft, "MyText");
    

    But i don't want the text on the image to expand . How can I acheive it ?
    Also the image is going to be grayscale. Hence, the text also changes to grayscale. I want the text to be colorful (Eg : Red, white etc.).

    Any suggestions.

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

      Hi
      I would make my own QLabel and then simply first paint the image, then paint the text.
      So the text is not part of the image but painted dynamically. ( that also allows for colors etc)
      Currently, you make the text part of the image so it scales with it and is kinda hrd to remove again.

      1 Reply Last reply
      4
      • U Offline
        U Offline
        Uday More
        wrote on last edited by
        #3

        @mrjj @SGaist
        This code below is the paintevent override

        void ClickableDistanceLabel::paintEvent(QPaintEvent *event)
        {
        qInfo () << FUNCTION;

        QPainter p(this);
        p.drawImage(this->rect(), pixmap()->toImage());
        
        QLabel *distanceLabel = new QLabel(this);
        distanceLabel->setFrameStyle(QFrame::Panel | QFrame::Raised);
        distanceLabel->setText("Distance");
        distanceLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft);
        distanceLabel->setGeometry(0,0,70,20);
        p.drawRect(distanceLabel->rect());
        distanceLabel->raise();
        

        }

        But i don't get the text "Distance" on it.
        Am I missing something.

        LabelOnLabel.PNG

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

          Hi
          That will generate a billion QLabels !
          since each paintEvent will allocate a new.
          and besides its wrong to do allocate it in the paintEvent, that
          you should do elsewhere if you really must use a QLabel.

          Anyway,
          Why not just draw the text since you have a painter?

          p.drawText( 20,20, "Distance");
          It can also take aligement options etc.

          1 Reply Last reply
          3
          • U Offline
            U Offline
            Uday More
            wrote on last edited by Uday More
            #5

            @mrjj

            The below code works. I need not create a label everytime.

            QPainter painter(this);
            painter.drawImage(this->rect(), pixmap()->toImage());
            painter.setPen(Qt::green);
            painter.setFont(QFont("Arial", 13));
            painter.drawText(QRectF(0,0,90,20), "Distance");
            

            LabelOnLabel.PNG

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

              @Uday-More

              Super.
              If you want you could do
              painter.drawPixmap(this->rect(), pixmap());
              so you dont have to convert to QImage every time you draw.

              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