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 Updated to NodeBB v4.3 + New Features

Inserting a QAction into a QLayout

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 4 Posters 2.5k 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.
  • J Offline
    J Offline
    JulienMaille
    wrote on 12 Mar 2021, 17:25 last edited by
    #1

    Hello,
    I'm trying to change my app layout and while moving a bunch of buttons from one place to another, I came across a QAction (currently located in a QToolBar) that I would like to move to a QLayout.
    I tried to call myLayout->insertWidget(0, ui->myToolbar->widgetForAction(ui->myAction));
    The button will disappear from the toolbar but does not appear in the layout.
    Is there a way to achieve this?

    Thanks in advance

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 12 Mar 2021, 17:39 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

      J 1 Reply Last reply 12 Mar 2021, 18:29
      1
      • C Christian Ehrlicher
        12 Mar 2021, 17:39

        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.

        J Offline
        J Offline
        JoeCFD
        wrote on 12 Mar 2021, 18:29 last edited by JoeCFD 3 Dec 2021, 18:45
        #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
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 12 Mar 2021, 19:09 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 15 Mar 2021, 11:14
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 12 Mar 2021, 19:14 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
            • C Christian Ehrlicher
              12 Mar 2021, 19:09

              @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 15 Mar 2021, 11:14 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
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 15 Mar 2021, 11:20 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 15 Mar 2021, 14:15
                0
                • C Christian Ehrlicher
                  15 Mar 2021, 11:20

                  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 15 Mar 2021, 14:15 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
                  • C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 15 Mar 2021, 14:21 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 15 Mar 2021, 19:15 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
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 15 Mar 2021, 19:19 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 16 Mar 2021, 10:35 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
                          • C Offline
                            C Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on 16 Mar 2021, 10:46 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 16 Mar 2021, 12:05
                            1
                            • J Offline
                              J Offline
                              JulienMaille
                              wrote on 16 Mar 2021, 12:04 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
                              • C Christian Ehrlicher
                                16 Mar 2021, 10:46

                                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 16 Mar 2021, 12:05 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
                                • C Offline
                                  C Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on 16 Mar 2021, 12:15 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

                                  6/16

                                  15 Mar 2021, 11:14

                                  topic:navigator.unread, 10
                                  • Login

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