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



  • 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?



  • 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.



  • 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.


  • Lifetime Qt Champion

    Hi,

    Did you consider implementing it with drag & drop ?



  • 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.


  • Lifetime Qt Champion

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



  • 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).



  • 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.



  • 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.



  • 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. :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.