View Full Version : Images/icons. It's not displayed

12th May 2007, 23:11
Hi everybody!
I'd like to put some icons on my controls (e.g. on a label). I put it into my disigner, here it is displayed, but when I compile my application, all of the images/icons are not displayed! Even the window icon!
I wrote manually the code (into my ui header):


No result...
What is the problem, can you help me?

12th May 2007, 23:42
setWindowIcon() will certainly not result in an icon appearing on your label... If you use relative paths to icons, I suggest you change them to absolute ones or better yet use a resource file containing all the images. You can create such a file from within Designer and then you'll have to add it to RESOURCES variable in your .pro file.

13th May 2007, 04:49
I wrote manually the code (into my ui header):

You can't write that there, because will get overwritten every time the ui is recompiled.
It has already been said in other posts. You must first read this: http://doc.trolltech.com/4.2/designer-using-a-component.html

Also, make sure you read this post: http://www.qtcentre.org/forum/f-newbie-4/t-problem-displaying-my-main-window-7002.html;

To display images in widgets use setPixmap or setIcon.
setWindowIcon has effect only for windows with title bar. It sets the standard pixmap for the system menu icon.


13th May 2007, 09:21
well, setWindowIcon() I used just to put icon as a main window icon.
About paths, qt-designer, I think, use itself absolute paths. I mean that when I put some icon into designer on a label, designer generate the path itself.

I know that my ui header will get overwritten every time the ui is recompiled.
But I need to put here some code... For example I include here my_class.h and my_class.cpp, I create my_class object here, I connect signals to slots from my_class.. and every time when I change my window and recompile my ui, at the beginning I save some parts of my code (connecting slots, declaring object), and, after recompiling put it down again.

13th May 2007, 10:42
What you have done is called "shooting yourself in the leg" :).

Use the single inheritance approach ( it is the simple one ). I your class add all the custom code that you add in the ui_*.h.

This is how it needs to be done. Not because I say so, but a lot of people do it.

The Single Inheritance Approach

In this approach, we use the Ui::ImageDialog object as we did in the simple case, but instead of setting up the dialog from the application's main function, we subclass QDialog (http://doc.trolltech.com/4.2/qdialog.html) and set up the user interface from within the constructor. Components used in this way expose the widgets and layouts used in the form to the QDialog (http://doc.trolltech.com/4.2/qdialog.html) subclass, and provide a standard system for making signal and slot connections between the user interface and other objects in your application.
This approach is used in the Calculator Form (http://doc.trolltech.com/4.2/designer-calculatorform.html) example.
To ensure that we can use the user interface, we need to include the header file that uic generates before referring to Ui::ImageDialog:
#include "ui_imagedialog.h" The subclass is defined in the following way:
class ImageDialog : public QDialog

ImageDialog(QWidget *parent = 0);

Ui::ImageDialog ui;
}; The important features of the class are the private ui object which provides the code for setting up and managing the user interface.
The constructor for the subclass constructs and configures all the widgets and layouts for the dialog just by calling the ui object's setupUi() function. Once this has been done, it is possible to modify the user interface and create items for the combobox:
ImageDialog::ImageDialog(QWidget *parent)
: QDialog(parent)

ui.colorDepthCombo->addItem(tr("2 colors (1 bit per pixel)"));
ui.colorDepthCombo->addItem(tr("4 colors (2 bits per pixel)"));
ui.colorDepthCombo->addItem(tr("16 colors (4 bits per pixel)"));
ui.colorDepthCombo->addItem(tr("256 colors (8 bits per pixel)"));
ui.colorDepthCombo->addItem(tr("65536 colors (16 bits per pixel)"));
ui.colorDepthCombo->addItem(tr("16 million colors (24 bits per pixel)"));

connect(ui.okButton, SIGNAL(clicked()), this, SLOT(accept()));
connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
} We can connect signals from the user interface widgets to slots in the dialog in the usual way, taking care to prefix the ui object to each widget used.
The main advantages of this approach are its simple use of inheritance to provide a QDialog (http://doc.trolltech.com/4.2/qdialog.html)-based interface, and its encapsulation of the user interface widget variables within the ui data member. We can use this method to define a number of user interfaces within the same widget, each of which is contained within its own namespace, and overlay (or "compose") them. This approach can be used to create individual tabs from existing forms, for example.

Read this http://doc.trolltech.com/4.2/designe...component.html (http://doc.trolltech.com/4.2/designer-using-a-component.html).
Of course, you may also use the multiple inheritance approach.


13th May 2007, 14:43
well, setWindowIcon() I used just to put icon as a main window icon.
About paths, qt-designer, I think, use itself absolute paths. I mean that when I put some icon into designer on a label, designer generate the path itself.

I suggest you open the file generated by Designer and see for yourself then... Think logically - what sense would it make to use an absolute path for an image? What would happen if you installed the application in another path on a different machine?

14th May 2007, 05:08
thx, it was useful! know I don't write code into ui header and it works nice. thx :)

well, I think I must not put the path for my image by myself, let the disigner do it..
But, I'll say again, using disigner for putting images make my app not to display it... Into disigner (and into preview-mode) images are displayed, but after compilation and running application images are not displayed..
In some hours i'll show you the code generated by disigner, know I need to go.

marcel, wysota:
thx 4 your help :)

14th May 2007, 09:46
well, I think I must not put the path for my image by myself, let the disigner do it..
Let me say it again - Designer inserts paths relative to the directory the form is being saved in, so it works when previewing. But when you compile the application under Windows, it is most often moved to a directory called debug or release (depending on the build type), which changes the path thus making Qt fail to load images. This gives you three choices:

1. Copy images to a proper folder so that relative paths are still valid.
2. Use Qt resource system (http://doc.trolltech.com/latest/resources.html) as I already suggested.
3. Register a function (with the form) that will return pixmaps for the form.

14th May 2007, 10:47
Ohh, finally I understand what is the problem!!! Thanks a lot.
But can you explain (or to give link to some docs) the third step, about registering the function? :)

14th May 2007, 11:02
It's rarely used, so I suggest you go for solution #2. But to satisfy your curiosity - open a form in Designer and choose "Form settings" from the "Form" menu. There you'll be able to activate the "Pixmap function" option and enter the name of a function that is being passed a QString and returns a pixmap. It could be implemented like this:

QPixmap getPixmap(const QString &name){
return QPixmap(QApplication::applicationDirPath()+QDir::s eparator()+name);

14th May 2007, 11:47
ok, thanks, know it's cleare for me.
and one more question - can I display on my form animated gif? When I add some pictures, the supported format is jpg and some others (I suggest), so within designer I can't add gif..

14th May 2007, 11:52
You have to enable gif support and recompile Qt. But it's not enough to make animated gifs work. You have to use QMovie.

14th May 2007, 12:32
ok, with animations i'll "play" later :)

know, I tried to use resources, as you said...
.qrc file:

<!DOCTYPE RCC><RCC version="1.0">

In my .pro I added RESOURCES = res.qrc .
And finally, in my constructor i put down

No result..:(

14th May 2007, 13:46
Is the path correct during compilation (debug/release folder issues again)? What does QFile::exists(":/images/icon.jpg") return?

14th May 2007, 14:36
well, QFile::exists() return true. What about the path, tutorial says that

The specified paths are relative to the directory containing the .qrc file.
And nothing about debug/release folders path..

14th May 2007, 16:47
What happens if you try to display a PNG file instead of a JPEG? And please test on a QLabel and not using a window icon.

14th May 2007, 19:33
No result... I add to /images an png image - image.png
To res.qrc I add -

And my .cpp:

//qSpeed is a label

14th May 2007, 20:07
Have you recompiled your resource?

14th May 2007, 21:36
First of all, did you run qmake after adding the resource file to the project?

14th May 2007, 22:23
marcel, wysota:
sure :)

But, can I put my icon on a label, by using QLabel::setWindowIcon(QIcon(...)) ? Maybe this is the problem?...

15th May 2007, 05:13
Use QLabel::setPixmap.

15th May 2007, 16:08
no result.....:-\

15th May 2007, 18:22
Are you sure that qSpeed is inside a proper layout?
Try also qSpeed->setMinimumSize( pixmap.size() ).

15th May 2007, 22:16
Are you sure that qSpeed is inside a proper layout?
Hm.. I'm just beginner, can you explain how to verify it?

15th May 2007, 23:06
Are you sure that qSpeed is inside a proper layout?
Try also qSpeed->setMinimumSize( pixmap.size() ).

It's not needed. If "qSpeed" is a label and is given a pixmap, it's minimum size will be set to the size of pixmap.

@LMZ: Could you provide a small compilable example that reproduces your problem? It might be easier for us to fix it then.

17th May 2007, 23:18
Hi guys! Sorry for that long pause... Well, I have create a small simple project. I made a form in a designer, designer was generate myform.h file. I put my pict within a disigner on a form. Picture are described in resources.. well, download it and take a look..
Thx a lot! )
URL (http://rapidshare.com/files/31895433/Template_EMPTY.zip.html)

17th May 2007, 23:36
Sorry, but could you use the attachment feature of the forum instead of using 3rd party commercial sites?

18th May 2007, 05:01
sure, sorry ))

18th May 2007, 08:20
I bet this is because of the jpeg plugin. Have you tried loading a PNG instead? Just to see if it works.

EDIT -I can see it also ( in designer )

18th May 2007, 08:21
I can see the image. Could you again verify that PNG images don't work either?

18th May 2007, 11:09
Finally! An png image are displayed. Thanks a lot!
Imediatly I tried to put on my label an animated gif. I add it to resorces, in projectdialog.cpp add code:

QMovie *movie = new QMovie(":/new/prefix1/avatar.gif");


No result... What can be the problem know?

18th May 2007, 11:11
Probably you did not compiled Qt with GIF support. Neither with jpeg support I assume.

18th May 2007, 11:27
What files do you have in your plugins/imageformats directory under your Qt installation dir?

18th May 2007, 11:36
in plugins/imageformats are next files - libqjpeg1.a, libqmng1.a, libqsvg1.a and three dll's with the same names

18th May 2007, 14:19
hmm... Those "1" seem suspicious.

18th May 2007, 14:25
should I download another dll's?

18th May 2007, 14:30
No, try removing the "1" from those!

18th May 2007, 14:55
hm, I don't think it's a good idea...take a look here (http://www.qtcentre.org/forum/archive/index.php/t-3423.html)..
But I think, I'm sattisfied with using of png pictures.. Know I need to use gif animations..