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. Show and hide tool buttons
QtWS25 Last Chance

Show and hide tool buttons

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 198 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.
  • R Offline
    R Offline
    RalfSt
    wrote 23 days ago last edited by
    #1

    A desktop app has several toolbars with different tool buttons. And each tool button is linked to a menu entry.

    In a ToolBar Settings dialog, the visibility of the individual tool buttons should be switched on or off (show/hide).
    However, you can only activate and deactivate the tool buttons. You can only switch the visibility in the QAction on and off. But then the menu entry also disappears.

    One solution would be to create another QAction and copy the data of the Menu-QAction into it and link them together. And then assign this to the tool button.
    Then you can switch the visibility of the tool button in the dialog on or off (show/hide) and the menu entry remains.

    Is there an easier way in Qt 6.9 ?
    Why is it not possible to switch off the visibility of a tool button?

    Thanks
    Ralf

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Kevin Hoang
      wrote 23 days ago last edited by
      #2

      You'll need to use QWidget *button = toolbar->widgetForAction(action); and then call button->setVisible(false); to hide the button on the toolbar.

      1 Reply Last reply
      1
      • R Offline
        R Offline
        RalfSt
        wrote 22 days ago last edited by
        #3
        auto action1 = new QAction(tr("Action 1"), this);
        
        auto action2 = new QAction(tr("Action 2"), this);
        action2->setCheckable(true);
        
        auto testsMenu = menuBar()->addMenu(tr("Tests"));
        testsMenu->addAction(action1);
        testsMenu->addAction(action2);
        
        auto testsToolbar = new QToolBar(tr("Tests Toolbar"), this);
        testsToolbar->setObjectName(QStringLiteral("testsToolbar"));
        testsToolbar->addAction(action1);
        testsToolbar->addAction(action2);
        addToolBar(testsToolbar);
        
        auto widget1 = testsToolbar->widgetForAction(action1);
        widget1->setEnabled(false); // works
        
        auto widget2 = testsToolbar->widgetForAction(action2);
        widget2->setVisible(false); // doesn't work
        

        Thanks for your help but it doesn't work.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kevin Hoang
          wrote 22 days ago last edited by
          #4

          Try calling testsToolbar->update(); after widget2->setVisible(false);. That could help refresh the layout.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            RalfSt
            wrote 21 days ago last edited by
            #5

            It still doesn't work.

            Note:
            You should use QAction::setVisible() to change the visibility of the widget.
            Using QWidget::setVisible(), QWidget::show() and QWidget::hide() does not work.
            

            Source: https://doc.qt.io/qt-6/qtoolbar.html#addWidget

            That's why I'm currently "cloning" every QAction. The original for the menu, and the clone for the toolbar.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              Kevin Hoang
              wrote 21 days ago last edited by
              #6

              I also tried it, and it turns out it doesn't work either. When a QToolButton is linked to an action, you can't hide the button if the action is still visible. It's one of those quirky things in the implementation.

              I think instead of trying to hide it, you can try using removeAction() and then add or insert it back when needed.

              1 Reply Last reply
              0

              1/6

              15 Apr 2025, 23:28

              • Login

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