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. [Solved] QToolBar: How to enable the user to (re)move icons?
QtWS25 Last Chance

[Solved] QToolBar: How to enable the user to (re)move icons?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 4.2k 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.
  • T Offline
    T Offline
    thEClaw
    wrote on last edited by
    #1

    I would like to implement a customizable toolbar. Adding icons at runtime isn't too hard, but how to remove things?

    I tried implementing a drag&drop capability, but since left clicks don't penetrate the QToolButtons, that doesn't work in the slightest. Are there any other approaches?
    In addition it would be nice to be able to reorder icons on the toolbar. Maybe there is a whole different class I should use instead that I don't know about yet?

    I am using a QMainWindow as parent for the toolbar (there are a widget and a menubar, too). Is customizing QToolBar complicated enough to switch the QMainWindow for a fully customized widget?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tusharkant15
      wrote on last edited by
      #2

      You can few things.

      First create your own widget by sub-classing qtoolbar. The you can re-implement the right click menu into a checkable list of the items in the toolbar.

      Second, you may want to not use qtoolbar and instead use a hboxlayout to place your buttons and have each of those buttons respond to a right click or a middle click in order to remove the particular item.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        thEClaw
        wrote on last edited by
        #3

        Thanks for your reply. I am still thinking about this since I am not too satisfied with any solution yet. No matter how far I go with subclassing QToolBar, it continues to feel clunky. Moving and removing items should be easy for the user. Removing via a context menu with only an option "Remove" would be a start, but I'd still need some kind of moving-capability. Otherwise inserting and removing would be too stiff and uncomfortable to use (in my opinion).

        If I wanted to use a "normal" widget instead of the QToolBar, I would have to write a lot of functionality from scratch, and subclass QMainWindow or exchange it with a normal widget, too. Which would create new problems.

        I thought this should be an easy thing, but it seems more and more involved.

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

          Hi,

          Did you consider implementing it with drag & drop ?

          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
          • T Offline
            T Offline
            thEClaw
            wrote on last edited by
            #5

            Consider yes, but I wasn't able to do it. For one thing I am not sure if I could change the order of the actions in the toolbar (QWidget::insertAction was not reimplemented for the toolbar). Also, I am not able to receive left clicks, since these are fully eaten up by the QToolButtons. I haven't tried yet to reimplement these (the buttons), since I didn't have a thorough plan how to proceed. But maybe that's the step to take? I would need to change many toolbar functions, though, to make it use the new buttons.

            EDIT : I don't think this would work, since none of the "addAction":http://qt-project.org/doc/qt-5.1/qtwidgets/qtoolbar.html#addAction functions is virtual, so I don't know how to manipulate the mousePress- and mouseMoveEvent of whatever widget I would need to reimplement.

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

              You could try to detect which action is near your drop point with actionAt and then use insertAction.

              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
              • T Offline
                T Offline
                thEClaw
                wrote on last edited by
                #7

                Two lines of code, I don't know why I didn't try this before. Thanks. :)

                Onwards to making them movable then. Even though that kind of works by now (somewhat unwieldy).

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  Stupid question: why do are you trying to use QToolbar itself for modifications to the toolbar? It seems to me that it would be easier to use a custom dialog to do it. That is what Microsoft is doing too, by the way.

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    thEClaw
                    wrote on last edited by
                    #9

                    I thought it would be more intuitive if I could just drag and drop actions onto it/off of it/inside of it. And I do like where things stand so far, so I'll try to continue on that route. A Dialog will probably be necessary in addition at some point, but for the small changes this behavior feels quite right.

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      thEClaw
                      wrote on last edited by
                      #10

                      I will declare this solved for now, even though I'm still not able to move items around in a convenient manner. But everything is kind of doable, one or the other way.

                      Thanks for help. :)

                      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