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. Inserting a QAction into a QLayout
Forum Update on Monday, May 27th 2025

Inserting a QAction into a QLayout

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 4 Posters 2.4k 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    An action is not a widget so you can't add it to a layout. What do you really want to do? If you want to execute an action after e.g. a button was pressed then add a button and connect the appropriate signal to the action.

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

    JoeCFDJ 1 Reply Last reply
    1
    • Christian EhrlicherC Christian Ehrlicher

      An action is not a widget so you can't add it to a layout. What do you really want to do? If you want to execute an action after e.g. a button was pressed then add a button and connect the appropriate signal to the action.

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #3

      @Christian-Ehrlicher ui->myToolbar->widgetForAction(ui->myAction) is a widget, not an action.
      I would prefer to create a new widget for this action. Otherwise, the same widget will be in two layouts.

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @JoeCFD said in Inserting a QAction into a QLayout:

        I would prefer to create a new widget for this action

        Then do this.

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

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

          Hi,

          You can associate an action with several widgets for example you can use the same action in a toolbar and a menu.

          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
          • Christian EhrlicherC Christian Ehrlicher

            @JoeCFD said in Inserting a QAction into a QLayout:

            I would prefer to create a new widget for this action

            Then do this.

            J Offline
            J Offline
            JulienMaille
            wrote on last edited by
            #6

            @Christian-Ehrlicher said in Inserting a QAction into a QLayout:

            @JoeCFD said in Inserting a QAction into a QLayout:

            I would prefer to create a new widget for this action

            Then do this.

            He's not the original poster :)
            I would like the widget to react to the QAction visible/disable state (event the tooltip text is shared accross widgets), juste like other QWidgets associated to that QAction.
            As @SGaist said a QAction can be present in several toolbars and menus.

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #7

              So still don't understand the problem. Then add the action to e.g. a QPushButton or similar... and action can be associated to more than one widget: https://doc.qt.io/qt-5/qaction.html#associatedWidgets

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

              J 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                So still don't understand the problem. Then add the action to e.g. a QPushButton or similar... and action can be associated to more than one widget: https://doc.qt.io/qt-5/qaction.html#associatedWidgets

                J Offline
                J Offline
                JulienMaille
                wrote on last edited by
                #8

                @Christian-Ehrlicher said in Inserting a QAction into a QLayout:

                So still don't understand the problem. Then add the action to e.g. a QPushButton or similar... and action can be associated to more than one widget: https://doc.qt.io/qt-5/qaction.html#associatedWidgets

                I'm not sure I understood how I can add my own QPushButton to the associatedWidgets

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  Via QWidget::addActionas described in the link. But it will not help much since a QPushButton does not react on QEvent::ActionChanged - only a QToolButton or QMenu and some others do. Therefore either derive from QPushButton and do it by your own or, as I already said in my first post: 'If you want to execute an action after e.g. a button was pressed then add a button and connect the appropriate signal to the action.'

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

                  1 Reply Last reply
                  1
                  • J Offline
                    J Offline
                    JulienMaille
                    wrote on last edited by
                    #10

                    As you said addAction() does not help with my request. It will note even trigger the associated action

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

                      Why use a QPushButton if you want to move a QToolButton around ?

                      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
                      • J Offline
                        J Offline
                        JulienMaille
                        wrote on last edited by
                        #12

                        @SGaist what are you suggesting? Is there a way I can easily insert a QAction in a layout as a QToolButton?
                        If I need to create my own toolbutton, connect all the signals and icon/title changes myself, then it defeats the whole purpose of using a QAction

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          Add a QToolButton to the layout and add the action with QWidget::addAction() as I already told you and the documentation tells you.

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

                          J 1 Reply Last reply
                          1
                          • J Offline
                            J Offline
                            JulienMaille
                            wrote on last edited by
                            #14

                            I have added a QToolBar in my layout, and add the actions to the toolbar, it works and I think that is the right way to do this.

                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Christian Ehrlicher

                              Add a QToolButton to the layout and add the action with QWidget::addAction() as I already told you and the documentation tells you.

                              J Offline
                              J Offline
                              JulienMaille
                              wrote on last edited by
                              #15

                              @Christian-Ehrlicher said in Inserting a QAction into a QLayout:

                              Add a QToolButton to the layout and add the action with QWidget::addAction() as I already told you and the documentation tells you.

                              This doesn't work. It will add a dropdown icon suggesting the toolbutton has a menu and nothing else

                              1 Reply Last reply
                              0
                              • Christian EhrlicherC Offline
                                Christian EhrlicherC Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                Then use signals/slots

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

                                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 Extensions
                                • Unsolved