Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QSystemTrayIcon icon from resource file isn't showing

[SOLVED] QSystemTrayIcon icon from resource file isn't showing

Scheduled Pinned Locked Moved General and Desktop
iconqsystemtrayiconresource
26 Posts 4 Posters 15.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nitzan
    wrote on 30 Jun 2015, 14:38 last edited by nitzan 7 Aug 2015, 09:23
    #1

    Hi.
    I'm very new to Qt so sorry if I missed something and this is a trivial problem.

    I want to have an app that has no window, it will run in the background and will have a tray icon for user interaction.
    I managed to do that using this code:

    int main(int argc, char* argv[]) {
        QApplication app(argc, argv);
    
        QAction* quitAction = new QAction(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(":/appicon"));
        trayIcon->show();
    
        return app.exec();
    }
    

    This works well, as the app seems to be running and I can close it using the context menu of the tray icon.
    There's one problem though and that is that the icon itself isn't showing, the tray icon is an invisible square.

    My resource file looks like this:

    <!DOCTYPE RCC>
    <RCC version="1.0">
        <qresource>
            <file alias="appicon">images/appicon.png</file>
        </qresource>
    </RCC>
    

    In the Resource Editor of the Qt Creator I see the icon and it's the right one, but I can't seem to make the app use it when it runs.

    What am I doing wrong here?

    Thanks

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 30 Jun 2015, 20:48 last edited by
      #2

      Hi and welcome to devnet,

      Which version of Qt are you using ? On what OS ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nitzan
        wrote on 30 Jun 2015, 21:08 last edited by
        #3

        I'm using the latest Qt (5.4.2) on a windows 8.1 machine.

        Thanks

        1 Reply Last reply
        0
        • A Offline
          A Offline
          ambershark
          wrote on 30 Jun 2015, 21:49 last edited by
          #4

          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.

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nitzan
            wrote on 1 Jul 2015, 08:36 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MartinH
              wrote on 1 Jul 2015, 10:05 last edited by
              #6

              I've the same issue with Qt 5.5.0. I do not got it on Qt 5.4.2 so this may be a new bug in Qt 5.5 ?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MartinH
                wrote on 1 Jul 2015, 10:09 last edited by
                #7

                I've just fixed the bug by removing the alias !

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  nitzan
                  wrote on 1 Jul 2015, 12:54 last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MartinH
                    wrote on 1 Jul 2015, 13:00 last edited by
                    #9

                    Huum if you're using QtCreator, open the file tree, open the tree of your resource file then do a right click on your image file. Click on copy full path (or something like that) then paste the path in your code :)

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      nitzan
                      wrote on 1 Jul 2015, 13:03 last edited by
                      #10

                      I tried that yesterday but it did not help, and looked weird as this is what I get:

                      ://images/appicon.png
                      

                      which has two slashes while I usually see only one used.

                      in any case, I tried it now again and to no avail, I get the same result.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        MartinH
                        wrote on 1 Jul 2015, 13:04 last edited by
                        #11

                        No, theorically qt creator propose to you a second option wich start with

                        qrc:/

                        You've it or not ?

                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          nitzan
                          wrote on 1 Jul 2015, 13:11 last edited by
                          #12

                          Nope.

                          All I have when I right click on the image in the resource editor is "Copy Resource Path to Clipboard" and the result is what I posted.

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            MartinH
                            wrote on 1 Jul 2015, 13:50 last edited by
                            #13

                            No, not in the resource editor, in the file tree on the left !

                            1 Reply Last reply
                            0
                            • N Offline
                              N Offline
                              nitzan
                              wrote on 1 Jul 2015, 13:55 last edited by
                              #14

                              Oh. sorry about that.

                              I tried that now as well, and the same thing happens (or doesn't happen).

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                ambershark
                                wrote on 2 Jul 2015, 00:46 last edited by
                                #15

                                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.

                                My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                                1 Reply Last reply
                                0
                                • N Offline
                                  N Offline
                                  nitzan
                                  wrote on 2 Jul 2015, 10:02 last edited by
                                  #16

                                  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.

                                  A 1 Reply Last reply 2 Jul 2015, 23:40
                                  0
                                  • N nitzan
                                    2 Jul 2015, 10:02

                                    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.

                                    A Offline
                                    A Offline
                                    ambershark
                                    wrote on 2 Jul 2015, 23:40 last edited by
                                    #17

                                    @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. :)

                                    My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                                    1 Reply Last reply
                                    0
                                    • N Offline
                                      N Offline
                                      nitzan
                                      wrote on 5 Jul 2015, 11:16 last edited by
                                      #18

                                      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.

                                      1 Reply Last reply
                                      0
                                      • A Offline
                                        A Offline
                                        ambershark
                                        wrote on 7 Jul 2015, 07:10 last edited by
                                        #19

                                        @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.

                                        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                                        1 Reply Last reply
                                        0
                                        • N Offline
                                          N Offline
                                          nitzan
                                          wrote on 7 Jul 2015, 10:29 last edited by
                                          #20

                                          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.

                                          1 Reply Last reply
                                          0

                                          9/26

                                          1 Jul 2015, 13:00

                                          17 unread
                                          • Login

                                          • Login or register to search.
                                          9 out of 26
                                          • First post
                                            9/26
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved