[SOLVED] QSystemTrayIcon icon from resource file isn't showing
-
Hi and welcome to devnet,
Which version of Qt are you using ? On what OS ?
-
Try setting an icon on your quit action to see if the resource is being loaded properly, i.e.
QAction *quitAction = new QAction(QIcon(":/appicon"), "&Quit", &app);
Now when you click into your menu on the systray the Quit option should have your icon next to it. If it doesn't you have an issue loading your resource.
-
I tried your suggestion and there's no icon in the menu item as well (only a blank transparent square).
What might be the problem with loading the resource?The resource file code is in the original post, here's the part in the .pro file that is relevant:
RESOURCES += qml.qrc \ images.qrc
And as I wrote before when I look at the images.qrc in the resource editor I can clearly see the image, it has no problem loading it then.
Thanks.
-
I tried removing the alias now as well but it did not fix things, the same thing.
Maybe I don't use the correct path without the alias, is this correct (using the same example as before):trayIcon->setIcon(QIcon(":/images/appicon.png"));
Also, maybe it has something to do with the image size?
Can I use images of any size and ratio? or am I limited in that?Thanks.
-
It tries to scale images, but it you were using a really crazy size it may exhibit behavior like that. What happens if you do this:
QLabel *label = new QLabel(); label->setPixmap(QPixmap(":/images/appicon.png"); label->show();
If it shows there then you may have an issue with it being converted to a QIcon in which case I would check your sizing of the image. I.e. try to get it no more than like 32x32 for that area since it is going to scale down to 16x16 more than likely.
-
Well, doing that was a bit over my Qt skills (controlling qml from c++), but I tried using a different image.
I created a 16x16 one color png, added that to the resource file and tried using that, but once again to no avail.
So it's probably not the image I was trying to use, but a problem with the resources or how I try to fetch them.Any ideas?
Thanks. -
@nitzan I just noticed in your resource file you don't have a prefix. Maybe try the following qrc file:
<!DOCTYPE RCC> <RCC version="1.0"> <qresource prefix="/"> <file>images/appicon.png</file> </qresource> </RCC>
Notice that prefix="/" on
<qresource>
.I also removed the alias so there isn't any issue there. Then in your load code do:
trayIcon->setIcon(QIcon(":/images/appicon.png"));
I think that might work. If not let me know any I'll write up a quick example that does work and send you the code and you can compare it to yours and figure out what is missing. :)
-
I added the prefix and it had no effect.
Here's my current code.Resource file:
<!DOCTYPE RCC> <RCC version="1.0"> <qresource prefix="/"> <file>images/appicon.png</file> <file>images/simpleIcon.png</file> </qresource> </RCC>
cpp file:
QAction* quitAction = new QAction(QIcon(":/images/appicon.png"), QObject::tr("&Quit"), &app); app.connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); QMenu* trayIconMenu = new QMenu(); trayIconMenu->addAction(quitAction); QSystemTrayIcon* trayIcon = new QSystemTrayIcon(); trayIcon->setContextMenu(trayIconMenu); trayIcon->setIcon(QIcon("qrc:/images/simpleIcon.png")); trayIcon->show();
The appicon is the original icon, with an image which is 83x83, and the simpleIcon is just a 16x16 solid color image.
Both aren't showing.
Thanks. -
@nitzan Sorry, got a ton going on at work right now. I will be able to mess around with your code a bit tomorrow probably and I'll let you know what I find out.
-
Something weird happened.
I haven't changed anything but now I do see an icon in the QAction, but not in the tray, even if I use the same exact object, like so:QIcon icon(":/images/appicon.png"); QAction* quitAction = new QAction(&icon, QObject::tr("&Quit"), &app); ... QSystemTrayIcon* trayIcon = new QSystemTrayIcon(); trayIcon->setIcon(&icon); ...
No idea what causes the icon to show up all of the sudden in the QAction, but fine, whatever..
The question is why will the icon show up there but not in the tray?Thanks.
-
That sounds like a build issue. Try making sure your build environment is totally clean and rebuild. Check out a fresh copy from source control even. Or at a bare minimum
make distclean
thenmake
.See if that resolves your icon issues.
I'll still play with your code in a bit, just haven't had time yet.