PDA

View Full Version : Push Button with image?



steve.bush
17th March 2011, 01:24
I am trying to display an image on a Push button using the icon property.

Here is the compiled ui file:




pushButton_3 = new QPushButton(tab);
pushButton_3->setObjectName(QString::fromUtf8("pushButton_3"));
pushButton_3->setGeometry(QRect(500, 30, 211, 131));
QIcon icon2;
icon2.addFile(QString::fromUtf8("resource/image.jpg"), QSize(), QIcon::Normal, QIcon::Off);
pushButton_3->setIcon(icon2);
pushButton_3->setIconSize(QSize(100, 100));



The image is displayed inside Qt creator, but the compiled executable does not have the image in the button.

I have also tried creating a resource file and including images to the resource file.

Some help would be great!

wysota
17th March 2011, 01:38
Try with a png file and with an absolute file path. If it works then either you are missing an image plugin for jpeg in the environment where you run the application or the file you are trying to load can't be found or accessed.

steve.bush
17th March 2011, 01:44
THANK YOU!!

Using the complete path worked!

szisziszilvi
17th March 2011, 12:10
If you want to use the resource system, is it not that there is a ":/" missing? In my application I use resiurce files like this:
QFile qfi(":/texts/res/usageNotes.txt");
This works:


QIcon icon;
QPixmap qpm;

if(qpm.load(":/img/myImage.jpg"))
{
icon.addPixmap(qpm);
ui->pushButton->setIcon(icon);
}
else ui->label->setText("nincs betöltve");


Otherwise I have the guess that the absolute path works because the executable file is not generated in the same directory as the code, so the executable file whilst working is trying to find the image on the relative path from itself, where is actually nothing, as there might be even no directory like that.

Gokulnathvc
17th March 2011, 12:20
Simplest way to set image on the Push Button::
btn = (QPushButton *)ui->btn;
btn->setIconSize((QSize(56,68)));
QPixmap* pixmap1 = new QPixmap(":/images/orange.bmp");
QIcon icon1(*pixmap1);
btn->setIcon(icon1);

I think this might help you..

Octal
17th March 2011, 12:23
btn = (QPushButton *)ui->btn;


Why this cast ? (which is an old C-style cast anyway).

Gokulnathvc
17th March 2011, 12:46
Anyway its works fine. Finding the simplest way to use yet more powerful

unit
17th March 2011, 16:29
ui->btn->setIconSize((QSize(56,68))); isn't work?

and in qt don't use old c-style cast. you have static_cast and object_cast

wysota
18th March 2011, 00:47
You shouldn't need any casts here. Be it implicit or explicit, C or C++ style.

Gokulnathvc
18th March 2011, 07:35
btn = (QPushButton *)ui->btn;
btn->setIconSize((QSize(56,68)));

wysota
18th March 2011, 07:48
btn = (QPushButton *)ui->btn;
btn->setIconSize((QSize(56,68)));

But why the cast? Not that this is relevant to the subject (it was about a missing file) but since we're already here, let's solve this situation too.