Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. How to unset QMenu from QPushButton?
Forum Updated to NodeBB v4.3 + New Features

How to unset QMenu from QPushButton?

Scheduled Pinned Locked Moved Solved Qt 6
5 Posts 4 Posters 615 Views 3 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.
  • D Offline
    D Offline
    DeeDee14
    wrote on last edited by
    #1

    In order to create a popup menu and then assign it to a QPushButton, we make use of

    QPushButton->setMenu(QMenu *menu)
    

    but if once you have assigned the menu to the button and you want to remove the menu item from the button, how could you do this?

    Also, once I use setMenu(), is there a way to dynamically update the menu?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Call the same method with a nullptr.

      Sure you can change the menu. It's not fixed in stone.

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

      D 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi and welcome to devnet,

        Call the same method with a nullptr.

        Sure you can change the menu. It's not fixed in stone.

        D Offline
        D Offline
        DeeDee14
        wrote on last edited by
        #3

        @SGaist I did try - it didn't work. It still shows the previous menu with those menu items/actions. :(

        Christian EhrlicherC 1 Reply Last reply
        0
        • D DeeDee14

          @SGaist I did try - it didn't work. It still shows the previous menu with those menu items/actions. :(

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @DeeDee14 Then you're doing something wrong. It works as expected, also the code explicitly allows this.

          QPushButton pb;
          QMenu *myMenu = new QMenu;
          pb.setMenu(myMenu);
          QTimer::singleShot(2000, &pb, [&]() { pb.setMenu(nullptr); });
          pb.show();
          

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          ThirdStrandT 1 Reply Last reply
          3
          • Christian EhrlicherC Christian Ehrlicher

            @DeeDee14 Then you're doing something wrong. It works as expected, also the code explicitly allows this.

            QPushButton pb;
            QMenu *myMenu = new QMenu;
            pb.setMenu(myMenu);
            QTimer::singleShot(2000, &pb, [&]() { pb.setMenu(nullptr); });
            pb.show();
            
            ThirdStrandT Offline
            ThirdStrandT Offline
            ThirdStrand
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • D DeeDee14 has marked this topic as solved on

            • Login

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