Extreme confusing problem
-
Hello, guys, I am trying to create an icon in tray.
The problem I met is rather strange.
I added my icon.png to a .qrc file and loaded the image from that. The Image itself never shows up.
Even if I copied the path or url given by qt creator itself, it just didn't work.
However, when I moved my icon.png to a location on my disk say "c://" it worked..pro file:
RESOURCES += \
Ima.qrc.qrc file:
<RCC>
<qresource prefix="/new">
<file>icon.png</file>
</qresource>
</RCC>testing code:
QGraphicsScene scene;
QGraphicsView view(&scene);
QGraphicsPixmapItem item(QPixmap(":/new/icon.png"));
/* qrc:/new/icon.png won't work either */
scene.addItem(&item);
view.show();real code:
QIcon icon("C:\icon.png");
trayIcon->setIcon(icon);
setWindowIcon(icon);
trayIcon->show();
/* it works */"C:\icon.png" directory works fine for both testing code and real code.
Thank you guys! I have already solved it.
It turns out that Q_INIT_RESOURCE(***); is truly needed.
And Unlinked problem when I added this line can be solved in following operation:- Make your "***" in the parentheses is the name of your .qrc file and correctly typed
2.Clean your project
3.Qmake your project
4.Run again and problem solved.
- Make your "***" in the parentheses is the name of your .qrc file and correctly typed
-
Have you tried to use the resource icon in your real code??
To show an Item in a GraphicsView you have to set correctly GraphicsScene rect and the view
This code
ui->label->setPixmap(QPixmap(":/images/image.png"));
works well for me using the resource
<RCC> <qresource prefix="/images"> <file>image.png</file> </qresource> </RCC>
-
@Rondog Thank you Randog, But it is not the problem.
I think that this line might be unnecessary. But you brought this up, I have another question for this line.
I have tried this even before I asked here. And when I add this line to my main.cpp, It told me " LNK2019 unresolved symbol".
There is an example in Qt document using this line, and I had the exact same libraries included.Note:
- resource file has been included in .pro file.
- Q_INIT_RESOURCE(Ima); /* name of my qrc file is Ima.qrc now*/
-
The unresolved linker error will occour if the name inside the macro Q_INIT_RESOURCE cannot be found.
I was not aware that this wasn't required, I tried it and sure enough it worked without it. It is interesting that when you did add it you ended up with a link error which means it can't be loaded (I have always added this line inside of main and get this error when I don't set the name properly).
From a sample program I have the following (that does work):
// qrc file ===================== <!DOCTYPE RCC><RCC version="1.0"> <qresource prefix="/gui"> <file>autocapture.png</file> </qresource> </RCC> // pro file ===================== // note: I put all the resources in a folder called 'res' RESOURCES += res/autocapture.qrc // usage example ===================== d_start_button->setIcon(QIcon(":/gui/autocapture.png"));
A couple of observations that might be helpful.
The resources line in the .PRO file ...
RESOURCES += \ Ima.qrc
... might be a problem. Why not this instead:
RESOURCES += Ima.qrc
The .qrc file is missing the first XML identification line (i.e. '<!DOCTYPE RCC>' ). Maybe the file is not recognized as a resource file without this line?