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. Can't add menu to QSystemTrayIcon
Forum Updated to NodeBB v4.3 + New Features

Can't add menu to QSystemTrayIcon

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 692 Views 1 Watching
  • 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.
  • S Offline
    S Offline
    Sucharek
    wrote on last edited by
    #1

    Hi, I can't seem to add a menu to QSystemTrayIcon.
    I've followed this tutorial, which showed me how to get the icon to the system tray, but now I can't add a context menu.
    Here's what I have:

    tray = new QSystemTrayIcon(this); //tray declared in .h file as a class
    tray->setVisible(true);
    tray->setIcon(QIcon(":/icons/clock.png"));
    tray->showMessage("Screen on time", "Screen on time service enabled");
    QMenu trayMenu;
    trayMenu.addSection("menu");
    tray->setContextMenu(trayMenu);
    
    JonBJ 1 Reply Last reply
    0
    • S Sucharek

      Hi, I can't seem to add a menu to QSystemTrayIcon.
      I've followed this tutorial, which showed me how to get the icon to the system tray, but now I can't add a context menu.
      Here's what I have:

      tray = new QSystemTrayIcon(this); //tray declared in .h file as a class
      tray->setVisible(true);
      tray->setIcon(QIcon(":/icons/clock.png"));
      tray->showMessage("Screen on time", "Screen on time service enabled");
      QMenu trayMenu;
      trayMenu.addSection("menu");
      tray->setContextMenu(trayMenu);
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Sucharek said in Can't add menu to QSystemTrayIcon:

      QMenu trayMenu;

      Not my area, and I have no idea whether the whole concept works/is supposed to work. But if this your actual code (which who knows, since it's QSystemTrayIcon::setContextMenu(QMenu *menu) and you are passing a QMenu not a QMenu * so it should not compile), the QMenu trayMenu will go out of scope and be destroyed?

      S 1 Reply Last reply
      1
      • JonBJ JonB

        @Sucharek said in Can't add menu to QSystemTrayIcon:

        QMenu trayMenu;

        Not my area, and I have no idea whether the whole concept works/is supposed to work. But if this your actual code (which who knows, since it's QSystemTrayIcon::setContextMenu(QMenu *menu) and you are passing a QMenu not a QMenu * so it should not compile), the QMenu trayMenu will go out of scope and be destroyed?

        S Offline
        S Offline
        Sucharek
        wrote on last edited by
        #3

        Hi @JonB, when I try doing QMenu *trayMenu = nullptr;, it will crash.

        JonBJ 1 Reply Last reply
        0
        • S Sucharek

          Hi @JonB, when I try doing QMenu *trayMenu = nullptr;, it will crash.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Sucharek
          Of course it will! Why should passing it a nullptr for its QMenu be acceptable?
          C/C++ basics: consider what you need to do about the scope/lifetime of the QMenu you pass to it.

          S 1 Reply Last reply
          2
          • JonBJ JonB

            @Sucharek
            Of course it will! Why should passing it a nullptr for its QMenu be acceptable?
            C/C++ basics: consider what you need to do about the scope/lifetime of the QMenu you pass to it.

            S Offline
            S Offline
            Sucharek
            wrote on last edited by
            #5

            Oh, yeah, got it now, thanks.
            Just need to give QMenu proper configuration now, because it just 2 pixels lol.

            JonBJ 1 Reply Last reply
            0
            • S Sucharek

              Oh, yeah, got it now, thanks.
              Just need to give QMenu proper configuration now, because it just 2 pixels lol.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Sucharek
              I imagine you made the QMenu trayMenu or QMenu *trayMenu = new QMenu a member variable of some class, which persists for its desired lifetime. Note that in Qt you often have to consider this lifetime declaration for various things which need to persist; we see lots of posts where user has only created object as a method local variable and does not realise it gets destroyed on scope closure, so you are not the first to have come a cropper on this! :)

              S 1 Reply Last reply
              2
              • JonBJ JonB

                @Sucharek
                I imagine you made the QMenu trayMenu or QMenu *trayMenu = new QMenu a member variable of some class, which persists for its desired lifetime. Note that in Qt you often have to consider this lifetime declaration for various things which need to persist; we see lots of posts where user has only created object as a method local variable and does not realise it gets destroyed on scope closure, so you are not the first to have come a cropper on this! :)

                S Offline
                S Offline
                Sucharek
                wrote on last edited by
                #7

                @JonB yeah, that's exactly what I've done.
                Thanks for your help :)

                1 Reply Last reply
                1

                • Login

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