Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set image with QLabel in Qt?

Tags:

qt

ui->label->setStyelSheet("image:url(:/1.png); border-image:url(:/2.png);");  

Why can't the image be displayed after run? But the border-image display normal.
It can display normal in Qtcreator. It can display image in compiler even did not run.

like image 983
Curie Avatar asked Aug 29 '12 10:08

Curie


People also ask

How do I add an image to a QLabel in Qt?

ui->label->setStyelSheet("image:url(:/1. png); border-image:url(:/2. png);");


1 Answers

I think the image property is for subcontrol only (see doc ), while border-image is valid for labels. Use

 QPixmap::QPixmap ( const QString & fileName, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor );
 QLabel::setPixmap ( const QPixmap & );

Like this:

QPixmap pix(":/1.png");
ui->label->setStyleSheet("border-image:url(:/2.png);");
ui->label->setPixmap(pix);
like image 166
UmNyobe Avatar answered Sep 24 '22 14:09

UmNyobe