PDA

View Full Version : Problems with QPushButton displaying an icon



John82
2nd July 2009, 11:07
Hi

I tried initializing a QPushButton with an icon ( image file in the resource file ). But the button is not displaying the icon. I have tried changing the layout direction. I also checked the icon size and it is giving me a size of 16 x 16. The original icon size is 40 x 34. So I am assuming that the icon has been assigned to the push button but it is not being displayed. Does anyone out there have any ideas?

Thanks

Lykurg
2nd July 2009, 14:13
Can we see your code? Probably you have an error in your file, and have you bind the resource file to your application properly?

John82
2nd July 2009, 14:22
The code is simple :


QPushButton* button = new QPushButton (QIcon (":/options.png"), tr ("Options"), this);

As far as binding is concerned, I am using Visual Studio.net with Qt integration and the resource file (.qrc) does have the above mentioned icon.

Thanks

IRON_MAN
2nd July 2009, 14:32
Try like this:


QPixmap buttonImage("argazkiak/flecha.bmp");
QIcon Icon;
Icon.addPixmap ( buttonImage, QIcon::Normal, QIcon::Off );
QPushButton *Button = new QPushButton();
Button->setIcon(Icon);
Button->setIconSize(QSize(100,100));

Lykurg
2nd July 2009, 14:35
Are you using other resource files without any problem?
what does
QPixmap p(":/options.png");
qWarning() << p.isNull();
say?

John82
2nd July 2009, 15:08
Are you using other resource files without any problem?
what does
QPixmap p(":/options.png");
qWarning() << p.isNull();
say?

I checked out your code and yes I think I am having problems with the resource file. QPixmap is giving me a null value; though I don't know why.

Lykurg
2nd July 2009, 15:24
and yes I think I am having problems with the resource file.


How does your main function look like (Q_INIT_RESOURCE?)
did you use prefixes in your qrc file?

John82
2nd July 2009, 16:00
I did not use Q_INIT_RESOURCE earlier because I thought it would be loaded automatically. After adding the line I am now facing a new error while compilation. It is giving me an "unresolved external symbol" error.


Error message :

unresolved external symbol "int __cdecl qInitResources_project(void)" (?qInitResources_project@@YAHXZ) referenced in function _main


My resource file is stored in a folder called Resources.


Contents :

<RCC>
<qresource prefix="Project">
<file>options.png</file>
</qresource>
</RCC>

Main function :


int main (int argc, char* argv[])
{
Q_INIT_RESOURCE (project);

// Create a QApplication with passed arguments
QApplication app (argc, argv);

// Create the main program window
MainWindow mw;

// Main window geometry management
int width = 800;
int height = 640;

// Show the main window.
mw.show();

return app.exec();
}

Lykurg
2nd July 2009, 16:49
Q_INIT_RESOURCE (project);
If you use that, your resource file has to be named project.qrc. And you are using a prefix so also ":/options.png" is wrong. I suggest you read this first (http://doc.qtsoftware.com/4.5/resources.html).

John82
2nd July 2009, 18:42
My resource file is named "project". After some trial and error, I got the code to work. It seems every time I add files to the .qrc file, I have to reload it in Visual studio. So icons are being read now. Thanks for the help!!