setText and setPixmap using
-
Hi,
I am using setText function and setPixmap functions to show png icons and some values like this :ui->label->setText(value);
QPixmap pix1(":/resources/images/images/01n.png"); ui->label_2->setPixmap(pix1);
I want to combine both of them in one setText function like this:
ui->label_3->setText(pix1 + value);
How can i do this?
-
Hi @rapid84 ,
QLabel->setBackgroundPixmap(QPixmap(":/resources/images/images/01n.png");
and then use:
QLabel->setText("value"); -
@Ni.Sumi
well, i dont want to set background image, i want to show an icon and a text in one label. -
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); -
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);@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>] ??
-
@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>] ??
-
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.
@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.
-
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 ? .
-
@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.
@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 :) -
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.
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.