[PyQt] advice needed for dragging icons off a toolbar

Hans-Peter Jansen hpj at urpla.net
Sat Sep 18 09:30:19 BST 2010


On Saturday 18 September 2010, 01:02:57 danny wrote:
> > The natural choice would be QToolButton of course, but did you also
> > considered the surprising behavior change for your users (that don't
> > expect those items being draggable..)?
>
> Thanks for the response. The natural question now, is how do I get from
> the QToolBar to the QToolButton. I typically construct the toolbar via
> addAction calls. I never construct a QToolButton directly to overload its
> mousePressEvent. I don't actually know how to construct a QToolBar
> directly from QToolButtons.
>
> Looking at the documentation, I don't know how to go from the QToolBar to
> QToolButtons. Is QToolBar.widgetForAction() the right method? Does that
> return the QToolButton? And if so, should I create an instance level
> override of mousePressEvent. That seems unusual to me. Or, reversing the
> question, how do I add an action to a QToolBar that has an overloaded
> QToolButton.

You're going to subclass QToolButton, override mouse{Press,Move}Event and 
add them to the toolbar with QToolBar.addWidget(). It might be sufficient 
to connect the pressed signal to a startDrag method in your main window. 
Check the deleayedencoding example, I've send the other day.

Just start over with a test script, that does nothing else. Present it here, 
when you're stuck.

Pete


More information about the PyQt mailing list