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. setText and setPixmap using
Forum Updated to NodeBB v4.3 + New Features

setText and setPixmap using

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 24.8k 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.
  • Ni.SumiN Ni.Sumi

    Hi @rapid84 ,

    QLabel->setBackgroundPixmap(QPixmap(":/resources/images/images/01n.png");
    and then use:
    QLabel->setText("value");

    R Offline
    R Offline
    rapid84
    wrote on last edited by
    #3

    @Ni.Sumi
    well, i dont want to set background image, i want to show an icon and a text in one label.

    kshegunovK 1 Reply Last reply
    0
    • R rapid84

      @Ni.Sumi
      well, i dont want to set background image, i want to show an icon and a text in one label.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #4

      @rapid84
      If you want to have them one beside the other just use two labels one for text and one for the image.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • Ni.SumiN Offline
        Ni.SumiN Offline
        Ni.Sumi
        wrote on last edited by Ni.Sumi
        #5

        If you set both image and text side by side on the same label. then you can Qpainter

        QImage image("(":/resources/images/images/01n.png");

        QPainter* p = new QPainter(address of image to ctor);
        p->setPen(Qt::black);
        p->setFont(QFont("Arial", 12));
        p->drawText(image.rect(), Qt::AlignCenter, "Text on Image/ Value"); //play around to display your text exactly how you need.

        QLabel* Label1 = new QLabel();
        Label->setPixmap(QPixmap::fromImage(image));
        Label1->setAlignment(Qt::AlignLeft);

        the_T 1 Reply Last reply
        0
        • Ni.SumiN Ni.Sumi

          If you set both image and text side by side on the same label. then you can Qpainter

          QImage image("(":/resources/images/images/01n.png");

          QPainter* p = new QPainter(address of image to ctor);
          p->setPen(Qt::black);
          p->setFont(QFont("Arial", 12));
          p->drawText(image.rect(), Qt::AlignCenter, "Text on Image/ Value"); //play around to display your text exactly how you need.

          QLabel* Label1 = new QLabel();
          Label->setPixmap(QPixmap::fromImage(image));
          Label1->setAlignment(Qt::AlignLeft);

          the_T Offline
          the_T Offline
          the_
          wrote on last edited by
          #6

          @Ni.Sumi said:

          If you set both image and text side by side on the same label. then you can Qpainter

          QImage image("(":/resources/images/images/01n.png");

          QPainter* p = new QPainter(address of image to ctor);
          p->setPen(Qt::black);
          p->setFont(QFont("Arial", 12));
          p->drawText(image.rect(), Qt::AlignCenter, "Text on Image/ Value"); //play around to display your text exactly how you need.

          QLabel* Label1 = new QLabel();
          Label->setPixmap(QPixmap::fromImage(image));
          Label1->setAlignment(Qt::AlignLeft);

          This draws the text centered onto the image and has same effect as setting a background image and text. If i correctly understood the question @rapid84 , you want to place an icon and text side by side into a label like this [<icon><some text>] ??

          -- No support in PM --

          R 1 Reply Last reply
          0
          • the_T the_

            @Ni.Sumi said:

            If you set both image and text side by side on the same label. then you can Qpainter

            QImage image("(":/resources/images/images/01n.png");

            QPainter* p = new QPainter(address of image to ctor);
            p->setPen(Qt::black);
            p->setFont(QFont("Arial", 12));
            p->drawText(image.rect(), Qt::AlignCenter, "Text on Image/ Value"); //play around to display your text exactly how you need.

            QLabel* Label1 = new QLabel();
            Label->setPixmap(QPixmap::fromImage(image));
            Label1->setAlignment(Qt::AlignLeft);

            This draws the text centered onto the image and has same effect as setting a background image and text. If i correctly understood the question @rapid84 , you want to place an icon and text side by side into a label like this [<icon><some text>] ??

            R Offline
            R Offline
            rapid84
            wrote on last edited by
            #7

            @the_

            Yes exactly :) , if possible...

            the_T 1 Reply Last reply
            0
            • R rapid84

              @the_

              Yes exactly :) , if possible...

              the_T Offline
              the_T Offline
              the_
              wrote on last edited by
              #8

              @rapid84

              One way you could try is to use HTML.

              //assume you have img.png in your ressource file
              QLabel *l = new QLabel;
              l->setTextFormat(Qt::RichText);
              l->setText("<img src=:/img.png>Text");
              

              But this is not guaranteed to work as expected.

              -- No support in PM --

              kshegunovK R 2 Replies Last reply
              0
              • the_T the_

                @rapid84

                One way you could try is to use HTML.

                //assume you have img.png in your ressource file
                QLabel *l = new QLabel;
                l->setTextFormat(Qt::RichText);
                l->setText("<img src=:/img.png>Text");
                

                But this is not guaranteed to work as expected.

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #9

                @the_, @rapid84
                Don't take offence but this seems like trying to kill a mosquito with a cannon. What's wrong with aggregating two labels, like this:

                QWidget * container = new QWidget(this);
                QVBoxLayout * layout = new QVBoxLayout(container);
                QLabel * textLabel = new QLabel(container);
                QLabel * imageLabel = new QLabel(container);
                
                layout->addWidget(imageLabel);
                layout->addWidget(textLabel);
                
                imageLabel->setPixmap(...);
                textLabel->setText(...);
                

                or its equivalent in the designer? This just puts the text under the image and they're nicely fit into a layout. The label will hold either text or image and that's noted in the documentation. Why try to circumvent the framework, when you could instead focus on using it ...

                Kind regards.

                Read and abide by the Qt Code of Conduct

                the_T 1 Reply Last reply
                0
                • Ni.SumiN Offline
                  Ni.SumiN Offline
                  Ni.Sumi
                  wrote on last edited by Ni.Sumi
                  #10

                  Hi @the_ ,

                  I am sorry but seems I understand it correctly. My idea is to make Qpainter widget inside the label or Qpainter as custom Label, to set image on one side and other side text. The code/idea is rough one because I don't know , how Mr. @rapid84 wants to display the text.

                  @kshegunov seems your idea is simple and but will it surely ? .

                  1 Reply Last reply
                  0
                  • kshegunovK kshegunov

                    @the_, @rapid84
                    Don't take offence but this seems like trying to kill a mosquito with a cannon. What's wrong with aggregating two labels, like this:

                    QWidget * container = new QWidget(this);
                    QVBoxLayout * layout = new QVBoxLayout(container);
                    QLabel * textLabel = new QLabel(container);
                    QLabel * imageLabel = new QLabel(container);
                    
                    layout->addWidget(imageLabel);
                    layout->addWidget(textLabel);
                    
                    imageLabel->setPixmap(...);
                    textLabel->setText(...);
                    

                    or its equivalent in the designer? This just puts the text under the image and they're nicely fit into a layout. The label will hold either text or image and that's noted in the documentation. Why try to circumvent the framework, when you could instead focus on using it ...

                    Kind regards.

                    the_T Offline
                    the_T Offline
                    the_
                    wrote on last edited by
                    #11

                    @kshegunov said:

                    @the_, @rapid84
                    Don't take offence but this seems like trying to kill a mosquito with a cannon.

                    If the mosquito is dead afterwards :)
                    No, just joking.

                    To summarize:
                    There is no way to set an icon and text to a label with one single command. @kshegunov's solution looks to be the clean way, for quick and dirty solution you can use the rich text format :)

                    -- No support in PM --

                    1 Reply Last reply
                    0
                    • the_T the_

                      @rapid84

                      One way you could try is to use HTML.

                      //assume you have img.png in your ressource file
                      QLabel *l = new QLabel;
                      l->setTextFormat(Qt::RichText);
                      l->setText("<img src=:/img.png>Text");
                      

                      But this is not guaranteed to work as expected.

                      R Offline
                      R Offline
                      rapid84
                      wrote on last edited by
                      #12

                      @the_

                      I have changed your code a little it works:

                      ui->label->setTextFormat(Qt::RichText);
                      ui->label->setText("<img src=:/resources/images/images/01d.png align=middle> " + value);
                      

                      But now, i can set the alignment of the png to middle but the "value" text alignment is shown as top, but from the property i have set it to AlignVCenter and AlignHCenter.

                      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