Working with several QDockWidgets

  • Hi everyone,

    when using several QDockWidgets, how do I determine, which is the "active" one? For example, let's take to QDockWidgets which contain a QTreeView each. Now, I'd like to implement an action that generally can work on both docks, but is supposed to work on the one, which is active or had the last focus.

    I thought of implementing a method that queries for the active dock before performing the actions. But things like (I'm using pyqt):




    don't work.

    Also, any hints to documentation are welcome.

    Thanks in advance,


  • I always thought a dock widget was kind of self-contained or affecting an MDI window. The fact that QDockWidget or QMainWindow doesn't really provide an easy way to get to that information kind of suggests to me that the Qt guys didn't intend it to be used that way :). I don't know what the desired behavior of your system is, but I think I would try to find another (maybe nicer) way of handling it.

    However, you can probably catch some focus event in each widget and emit a signal that it has received focus.

  • Yes, it doesn't seem to be intended to be used that way. I'm considering alternatives.

    I could go with giving each dock widget its own buttons for the TreeView specific actions. But then the question of how to handle keyboard shortcuts would arise.

    I also could create my own "active dock widget attribute" and let the user manually set the active dock. Is that a recommended practice? (It might have appeared to you that I'm doing my first steps with Qt ;-)... )

  • Also, I'm not even sure dock widgets can even be "focused" (but the widgets inside them sure can).

  • A dock widget is a widget just like all others. But it is indeed probably more useful to get the focus state of the widgets inside them.

  • I'm hiding the dock widgets on my main window with:
    @for w in self.findChildren(QDockWidget):
    w.setVisible(True) # or false depending on the case@

    If I select the 3rd tab in my dock widgets, hide and then re-show them, then the 1st tab will be selected.
    If I again hide then show them, then this time the 3rd tab will be selected!

    So how can I stop that? If I could get the currently selected tab then I could set that one to be active again afterwards. I'm tempted to actually think this might be a Qt Bug... :p

  • I found this:
    @void QDockWidget::visibilityChanged ( bool visible ) [signal]

    This signal is emitted when the dock widget becomes visible (or invisible). This happens when the widget is hidden or shown, as well as when it is docked in a tabbed dock area and its tab becomes selected or unselected.

    This function was introduced in Qt 4.3.@

    Although quite why there's no method to get that property, I don't know... And it's also not specific enough.

  • There is bound to be a method for the visibility property. A QDockWidget is a QWidget itself. QWidget has the property, so QDockWidget has it as well (probably even requires it).

  • Yeah I don't mean when the dock widget can be seen, but I mean when the dock widget is the selected one on top in a set of tabs.

    I wrote this "small example (click here": - run qmake/make) to show what I mean. Just click show/hide button and notice your currently selected tab position. Is this a Qt bug?

  • Interesting... It seems to be switching to the previously selected widget. Can't say if it's a bug though. I'd need to check a bit more. You could do a search through the bug tracker though.

  • "This": is the closest bug I could find. Seems QDockWidgets are rather sparse in their api so far :) No way to see who's on top, get the tab orderings, find the layout of several dock widgets in the same area :)

  • How can I see if a QDockWidget belongs to a particular tab bar?
    EDIT: this "":
    (see indexOf(widget))

    EDIT2: Or not. Spoke too soon!!! QDockWidgets use QTabBar but they don't use QTabWidget! So how am I meant to see which QDockWidget belongs to which QTabBar so I can restore the state? Very frustrating :(

    Here is my saving code (saving tab positions)
    @ tabWidgetsAll = self.parent().findChildren(QTabWidget, None)
    # tab widgets which contain QDockWidget
    tabWidgets = []
    if len(tabWidgets) > 0:
    print 'yay'
    for w in widgets:
    for tab in tabWidgetsAll:
    if tab in tabWidgets:
    if tab.indexOf(w) != -1:
    for tab in tabWidgets:
    self.activeTabs.append((tab, tab.currentIndex()))@

    In that snippet 'yay' is never printed, indicating that no QTabWidget's exist and that QDockWidget doesn't use that for tabifying itself.

    And here is the restoration code:
    @ for tab in self.activeTabs:
    self.activeTabs = []@

  • any ideas?

  • Can Qt add a QMainWindow::setDockAreaVisible(Qt::DockWidgetArea area, bool visible) to their API?

    Looking at:

    It looks like a trivial change:
    add to

    class QDockAreaLayout
    QDockAreaLayoutInfo hiddenDocks[4]; // hidden dock windows
    bool dockHidden[4]; // initialise to false, false, false, false on initialiser
    QDockAreaLayout::setVisible(QInternal::DockPosition pos, bool visible)

    if (!visible) {
    // copy
    hiddenDocks[pos] = docks[pos];
    dockHidden[pos] = true;
    // clear the dock widget area
    } else {
    docks[pos] = hiddenDocks[pos];

    And add to QMainWindow::setVisible(area, bool) which does:
    layoutState.dockAreaLayout.setVisible(toDockPos(area), bool);

    Hiding/showing a sidepanel is such a common use-case :) This is a really needed feature.

    I made a "bug report here":

  • Maybe it's time to (do a feature request|develop it yourself and do a merge request) for that. I'm not sure these suggestions will be picked up from devnet.

  • Right, but I don't want to waste time building my own Qt, adding that function to the API to have it rejected. Where can I ask whether it would be accepted if the code worked nicely?

  • [quote author="genjix" date="1288186847"]Where can I ask whether it would be accepted if the code worked nicely?[/quote]

    Could start with a feature request at "": Or find the developers on the #qt-labs IRC channel on for a chat.

Log in to reply

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