Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Checkable QAction doesn't change its state
Qt 6.11 is out! See what's new in the release blog

Checkable QAction doesn't change its state

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 3.2k 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.
  • A abdoemr11

    I don't know what is wrong
    I use this code to create checkable QAction

    void MainWindow::showContextMenu(const QPoint &p) {
        qDebug() << "hit here";
        myMenu = new QMenu();
        testAction = myMenu->addAction("testAction");
        testAction->setCheckable(true);
        connect(testAction, &QAction::triggered, this, [=](bool checked){qDebug()<< checked;});
    
        myMenu->exec(mapToGlobal(p));
    }
    

    I can open the context menu but when I click on the action its state never changes either visually or the debugged message. What I am missing here?

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

    @abdoemr11
    Your code looks correct to me. (You might put in testAction->setChecked(true); after setCheckable(true) to make sure the checkbox actually works.) Next step is what platform and which version of Qt, you could search the Qt bugs for any reports of it not working in certain environment?

    1 Reply Last reply
    1
    • A Offline
      A Offline
      abdoemr11
      wrote on last edited by
      #3

      I tried testAction->setChecked(true); and it make the action checked but again it doesn't change after that.
      I tried also the Menu Example and it work just fine!!

      JonBJ 1 Reply Last reply
      0
      • A abdoemr11

        I tried testAction->setChecked(true); and it make the action checked but again it doesn't change after that.
        I tried also the Menu Example and it work just fine!!

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

        @abdoemr11
        Which "Menu Example" is that --- what's the link?

        A 1 Reply Last reply
        0
        • A abdoemr11

          I don't know what is wrong
          I use this code to create checkable QAction

          void MainWindow::showContextMenu(const QPoint &p) {
              qDebug() << "hit here";
              myMenu = new QMenu();
              testAction = myMenu->addAction("testAction");
              testAction->setCheckable(true);
              connect(testAction, &QAction::triggered, this, [=](bool checked){qDebug()<< checked;});
          
              myMenu->exec(mapToGlobal(p));
          }
          

          I can open the context menu but when I click on the action its state never changes either visually or the debugged message. What I am missing here?

          M Offline
          M Offline
          mpergand
          wrote on last edited by
          #5

          @abdoemr11 said in Checkable QAction doesn't change its state:

          myMenu = new QMenu();

          You create a new menu each time, then testAction is a different object ;)

          JonBJ A 2 Replies Last reply
          1
          • M mpergand

            @abdoemr11 said in Checkable QAction doesn't change its state:

            myMenu = new QMenu();

            You create a new menu each time, then testAction is a different object ;)

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

            @mpergand
            I don't understand. Yes, he creates a new QMenu, with a new QAction, each time in showContextMenu(). Apart from the fact that this (may?) leak a QMenu/QAction, how does this affect that which one he is currently showing (i.e. the latest created testAction which gets connect()ed) does not respond to check-clicks??

            M 1 Reply Last reply
            0
            • JonBJ JonB

              @abdoemr11
              Which "Menu Example" is that --- what's the link?

              A Offline
              A Offline
              abdoemr11
              wrote on last edited by
              #7

              @JonB https://doc.qt.io/qt-5/qtwidgets-mainwindows-menus-example.html

              M 1 Reply Last reply
              0
              • A abdoemr11

                @JonB https://doc.qt.io/qt-5/qtwidgets-mainwindows-menus-example.html

                M Offline
                M Offline
                mpergand
                wrote on last edited by mpergand
                #8

                @abdoemr11

                bool menuState=false;  // instance variable of MainWindow
                
                void MainWindow::showContextMenu(const QPoint &p) {
                    qDebug() << "hit here";
                    QMenu myMenu;
                    auto testAction = myMenu.addAction("testAction");
                    testAction->setCheckable(true);
                    testAction->setChecked(menuState);
                    connect(testAction, &QAction::triggered, this, [=](bool checked)
                    {qDebug()<< checked;
                     menuState=checked;});
                
                    myMenu.exec(mapToGlobal(p));
                }
                
                1 Reply Last reply
                0
                • JonBJ JonB

                  @mpergand
                  I don't understand. Yes, he creates a new QMenu, with a new QAction, each time in showContextMenu(). Apart from the fact that this (may?) leak a QMenu/QAction, how does this affect that which one he is currently showing (i.e. the latest created testAction which gets connect()ed) does not respond to check-clicks??

                  M Offline
                  M Offline
                  mpergand
                  wrote on last edited by
                  #9

                  @JonB said in Checkable QAction doesn't change its state:

                  how does this affect that which one he is currently showing (i.e. the latest created testAction which gets connect()ed) does not respond to check-clicks??

                  It responds, although the debug msg reflets the state change, it's not the state of the new created action upon the next click , so it prints always the same state (true if the state of the action is unchecked and vice-versa).

                  Actually I don't understand what you don't understand ;)

                  JonBJ 1 Reply Last reply
                  1
                  • M mpergand

                    @abdoemr11 said in Checkable QAction doesn't change its state:

                    myMenu = new QMenu();

                    You create a new menu each time, then testAction is a different object ;)

                    A Offline
                    A Offline
                    abdoemr11
                    wrote on last edited by
                    #10

                    @mpergand Actually it was the problem thanks for the hint
                    The problem was that the main code base was using this approach (creating the action each time the context menu is shown) it works without problem with ordinary action but checkable action needs to initialized once.

                    1 Reply Last reply
                    0
                    • M mpergand

                      @JonB said in Checkable QAction doesn't change its state:

                      how does this affect that which one he is currently showing (i.e. the latest created testAction which gets connect()ed) does not respond to check-clicks??

                      It responds, although the debug msg reflets the state change, it's not the state of the new created action upon the next click , so it prints always the same state (true if the state of the action is unchecked and vice-versa).

                      Actually I don't understand what you don't understand ;)

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

                      @mpergand said in Checkable QAction doesn't change its state:

                      Actually I don't understand what you don't understand ;)

                      @abdoemr11 said in Checkable QAction doesn't change its state:

                      it works without problem with ordinary action but checkable action needs to initialized once.

                      I had no idea the OP was wanting to repeat showing an action which had to carry its check state forward from one invocation to a later one! I thought he couldn't get the checkbox to check.

                      1 Reply Last reply
                      0

                      • Login

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