How to create stacked QDockWidgets

2,577 views
Skip to first unread message

David Martinez

unread,
Aug 22, 2014, 8:43:33 AM8/22/14
to python_in...@googlegroups.com
Hi,

I have a question in regards on how the 'QDockWidget' works in 'PyQt/PySide'. I have an application that creates an instance of 'QMainWindow' and this one creates two 'QDockWidgets' in the bottom area.

I'd like them to be stacked upon creation but instead they appear next to each other. Once the application is running, I can drop one on top of the other but I'd like to stack them when they get created. Is that possible?

Here is some mock up code:

https://gist.github.com/davidmartinezanim/6c69ca5cca39f390f89a

(I know this way of importing is bad practice but it just for the sake of the example)

Do you have any ideas about how to get this working the way I want?


--
David Martinez - Technical Animator
Email: david.mar...@gmail.com

David Martinez

unread,
Aug 22, 2014, 12:58:14 PM8/22/14
to python_in...@googlegroups.com
Ah!

I've got it working by adding the following line:

    self.tabifyDockWidget(self.bottom_dock_01,self.bottom_dock_02)

Which brings me to my next question... How do I know if there are 'QDockWidgets' already in one of the four zones? I'm asking because I'm creating those dynamically and I will need to do the check before I try to stack them.

Cheers

David Martinez

unread,
Aug 22, 2014, 1:28:45 PM8/22/14
to python_in...@googlegroups.com
It occurs to me that one way of doing it would be to get all the 'Docks' that a given 'QMainWindow' has and then before adding a new one, I could do the following:

        # Creates second bottom Dock
        self.bottom_dock_02 = QDockWidget("Second Bottom Dock", self)
        self.bottom_dock_02.setObjectName('bottom_dock_02')
        self.bottom_dock_02.setAllowedAreas(Qt.BottomDockWidgetArea)
        self.widget_second_dock = QWidget(self.bottom_dock_02)
        self.widget_second_dock.setFixedHeight(150)
        self.bottom_dock_02.setWidget(self.widget_second_dock)
        self.addDockWidget(Qt.BottomDockWidgetArea, self.bottom_dock_02)

        existing_widget = None
  
        for current_dock in self.findChildren(QDockWidget):
            if self.dockWidgetArea(current_dock) == Qt.BottomDockWidgetArea:
                existing_widget = current_dock
                break
   
        if existing_widget:
            self.tabifyDockWidget(self.bottom_dock_02,current_dock)


Does that sound like a good way of doing it? If so, I have a couple of questions more:

  • Is it possible to change the order in which the items have been stacked? I'd like the user to be able to re-arrange them if they want to do so.


Many thanks



--
David Martinez - Technical Animator
 


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/e0d9289e-4208-44f4-8e6d-15a74125c401%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Justin Israel

unread,
Aug 22, 2014, 5:11:08 PM8/22/14
to python_in...@googlegroups.com
You can keep using the tabify method to switch the order of the tabs. tabifyDockWidget(under, over)
import random

class MainWin(QtGui.QMainWindow):

    def __init__(self):
        super(MainWin, self).__init__()

        prev = None
        for i in xrange(5):
            d = QtGui.QDockWidget("Dock %d" % i, self)
            self.addDockWidget(QtCore.Qt.BottomDockWidgetArea, d)
            if prev:
                self.tabifyDockWidget(prev, d)
            prev = d 

        t = QtCore.QTimer(self)
        t.timeout.connect(self.randomTab)
        t.start(2000)

    def randomTab(self):
        a, b = random.sample(self.findChildren(QtGui.QDockWidget), 2)
        print "Setting %s over %s" % (b.windowTitle(), a.windowTitle())
        self.tabifyDockWidget(a, b)



David Martinez

unread,
Aug 22, 2014, 7:47:32 PM8/22/14
to python_in...@googlegroups.com
Thanks Justin. I didn't realize that I could use that same method!

I have a couple of questions:

  • Do you know if there is a way to get the results of self.findChildren(QtGui.QDockWidget) in order of visual appearance? I'm not sure what's defining the order in which they appear but they seem to be different every tme that I select one of the Docks. Ideally, I'd like to get the current Dock and know which docks I have to each side to determine if I can/should use self.tabifyDocWidget().

  • When I right click on the title of a Dock, it triggers a menu. This menu is also shared with toolbars to define what's visible and whatnot. Ideally I want to have different menus depending on which is the item in which I right click. I've been able to get rid of the menu by using the following in my QMainWindow:

    def createPopupMenu(self):
        pass

Does that mean that I will need to overload this method, check what's under the cursor and depending on that, create the menu? Or is there another way to do this?


Thanks in advance




--
David Martinez - Technical Animator
 


Justin Israel

unread,
Aug 22, 2014, 8:22:09 PM8/22/14
to python_in...@googlegroups.com
On Sat, Aug 23, 2014 at 11:47 AM, David Martinez <david.mar...@gmail.com> wrote:
Thanks Justin. I didn't realize that I could use that same method!

I have a couple of questions:

  • Do you know if there is a way to get the results of self.findChildren(QtGui.QDockWidget) in order of visual appearance? I'm not sure what's defining the order in which they appear but they seem to be different every tme that I select one of the Docks. Ideally, I'd like to get the current Dock and know which docks I have to each side to determine if I can/should use self.tabifyDocWidget().
What is considered "visual appearance" in terms of finding all QDockWidget instances in your app? The widgets may be in different dock areas, in tabs stacked with others, or even using the split layout where tabs in a given area are split vertically or horizontally (nested docks).

tabifiedDockWidgets(target) seems to preserve the tab order of the other docks sharing the same tab layout. Although it doesn't really tell you want is to the left or right of your target. 
  • When I right click on the title of a Dock, it triggers a menu. This menu is also shared with toolbars to define what's visible and whatnot. Ideally I want to have different menus depending on which is the item in which I right click. I've been able to get rid of the menu by using the following in my QMainWindow:

    def createPopupMenu(self):
        pass

Does that mean that I will need to overload this method, check what's under the cursor and depending on that, create the menu? Or is there another way to do this?

The context menu event bubbles up to the main window by default. But if you want custom context menus for each QDockWidget, then you just need to implement it in either contextMenuEvent() on the dock widget, or via an event filter:
        aDock.installEventFilter(self) 
        ...
    def eventFilter(self, obj, event):
        if event.type() == event.ContextMenu and isinstance(obj, QtGui.QDockWidget):
            print "Show menu for", obj.windowTitle()
            event.accept()
            return True
        return False

David Martinez

unread,
Aug 23, 2014, 1:43:10 PM8/23/14
to python_in...@googlegroups.com
 It looks like this is going to split in two different subjects so I will cover first the one in regards of the menu:

The context menu event bubbles up to the main window by default. But if you want custom context menus for each QDockWidget, then you just need to implement it in either contextMenuEvent() on the dock widget, or via an event filter:
        aDock.installEventFilter(self) 
        ...
    def eventFilter(self, obj, event):
        if event.type() == event.ContextMenu and isinstance(obj, QtGui.QDockWidget):
            print "Show menu for", obj.windowTitle()
            event.accept()
            return True
        return False
This is an example of a regular QDockWidget with the usual context menu (It only triggers on the title of the QDockWidget):
In order to implement my own menu, I've tried using installEventFilter and this is the result:
This seems to work wonderfully but the menu gets triggered anywhere in the QDockWidget and not just in title.

The only way I've found to get it working is implementing a custom title bar for the QDockWidget and trigger the menu on that new widget instead. The problem with that, is that I will have to manually implement functionality that was already working in the default title bar (close option and visual style). Here is an example:
So here is my question:
  • Is there a way to get it to trigger the menu on the default title bar instead of having to implement my own?

I'm learning a lot with your responses.
Thanks a lot!


--
David Martinez - Technical Animator
Email: david.mar...@gmail.com


Justin Israel

unread,
Aug 23, 2014, 6:22:13 PM8/23/14
to python_in...@googlegroups.com
The functionality that I see for QDockWidget is that any area not occupied by the widget set with setWidget() will trigger the special context menu that contains toggles for toolbars and dock widgets. If you are right clicking on an area contained by the content widget, you won't get the menu. These are with the default settings (not the custom event handling we are doing). The thing that makes it a bit more difficult here is that the QDockWidget doesn't actually give you access to the internal title bar that it uses when you don't give it a custom one. So we can't just watch it specifically for context menu events, and when we watch the entire QDockWidget for context events, we also get the events that were ignored by the content widget that are bubbling up to the dock.

I'm sure there are multiple ways around this. I usually add my own custom title bar anyways, since it isn't too hard to add the buttons I want and connect the signals, and it lets me add more stuff into the title bar. But here is another way you could try, by subclassing QDockWidget:
class Dock(QtGui.QDockWidget):

    def eventFilter(self, obj, event):
        if event.type() == event.ContextMenu:
            # Let the widget do whatever it wants
            obj.contextMenuEvent(event)
            # But ensure the result is an accepted event
            # to prevent it from bubbling up.
            event.accept()
            # Don't redeliver the event
            return True

        return False

    def setWidget(self, widget):
        super(Dock, self).setWidget(widget)
        widget.installEventFilter(self)

    def contextMenuEvent(self, event):
        event.accept()
        print "Context menu for", self.windowTitle()

        # Either do the context menu right here..

        # Or emit a custom signal, and let another object 
        # like the main window parent create the menus
When we set the widget into the QDockWidget, we watch for its ContextMenu events. We don't want to mess with the actual widget and prevent it from behaving the way it wants, so we let it run the contextMenuEvent(), but we check to see if it actually accepted it or not. It may have shown its own menu and accepted. But either way, we accept the event and prevent any further propagation. Now when you right click on widgets that don't define their own context menus, the event won't bubble up to the dock widget. This leaves you with a context menu that only shows up either when you right click the title bar, or right click an area not fully occupied by the content widget.

You can either handle the context menu in the actual Dock class, or you can emit a signal and let your QMainWindow  handle all menus for its dock widgets.










David Martinez

unread,
Aug 25, 2014, 5:01:35 AM8/25/14
to python_in...@googlegroups.com
That's exactly the kind of behavior I was looking for!
Thanks a lot for your reply and your explanations Justin.


--
David Martinez - Technical Animator
 


Justin Israel

unread,
Aug 25, 2014, 5:12:22 AM8/25/14
to python_in...@googlegroups.com
Anytime. Qt is like this little playground made of Play-Doh. You just smoosh it around to make it do different stuff. 


Reply all
Reply to author
Forward
0 new messages