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):
qApp.focusWidget()
or
qApp.activeWindow()
don't work.
Also, any hints to documentation are welcome.
Thanks in advance,
Jan
-
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 ;-)... )
-
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.
-
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":http://www.qtcentre.org/attachment.php?attachmentid=5397&d=1288020905 - 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?
-
"This":http://bugreports.qt.nokia.com/browse/QTBUG-3420 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 "http://doc.trolltech.com/4.7/qtabwidget.html":http://doc.trolltech.com/4.7/qtabwidget.html
(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:
continue
if tab.indexOf(w) != -1:
tabWidgets.append(tab)
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:
tab[0].setCurrentIndex(tab[1])
self.activeTabs = []@ -
Can Qt add a QMainWindow::setDockAreaVisible(Qt::DockWidgetArea area, bool visible) to their API?
Looking at:
http://qt.gitorious.org/qt/qt/blobs/4.7/src/gui/widgets/qdockarealayout_p.h
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
docks[pos].clear();
} else {
docks[pos] = hiddenDocks[pos];
}
@And add to QMainWindow::setVisible(area, bool) which does:
layoutState.dockAreaLayout.setVisible(toDockPos(area), bool);
invalidate();Hiding/showing a sidepanel is such a common use-case :) This is a really needed feature.
I made a "bug report here":http://bugreports.qt.nokia.com/browse/QTBUG-14725
-
[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 "http://bugreports.qt.nokia.com":http://bugreports.qt.nokia.com. Or find the developers on the #qt-labs IRC channel on irc.freenode.net for a chat.