October 5, 2010

Knacktus Knacktus
Lab Rat
41 posts

Working with several QDockWidgets

Page  
1

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

17 replies

October 5, 2010

Franzk Franzk
Lab Rat
830 posts

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.

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

October 5, 2010

Knacktus Knacktus
Lab Rat
41 posts

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 ;-)… )

October 5, 2010

MTK358 MTK358
Lab Rat
27 posts

Also, I’m not even sure dock widgets can even be “focused” (but the widgets inside them sure can).

October 5, 2010

Franzk Franzk
Lab Rat
830 posts

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.

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

October 23, 2010

genjix genjix
Lab Rat
20 posts

I’m hiding the dock widgets on my main window with:

  1. for w in self.findChildren(QDockWidget):
  2.     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

October 24, 2010

genjix genjix
Lab Rat
20 posts

I found this:

  1. void QDockWidget::visibilityChanged ( bool visible ) [signal]
  2.  
  3. 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.
  4.  
  5. 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.

October 25, 2010

Franzk Franzk
Lab Rat
830 posts

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

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

October 25, 2010

genjix genjix
Lab Rat
20 posts

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 [qtcentre.org] – 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?

October 25, 2010

Franzk Franzk
Lab Rat
830 posts

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.

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

October 25, 2010

genjix genjix
Lab Rat
20 posts

This [bugreports.qt.nokia.com] 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 :)

October 26, 2010

genjix genjix
Lab Rat
20 posts

I found the answer:
http://developer.qt.nokia.com/faq/answer/how_can_i_check_which_tab_is_the_current_one_in_a_tabbed_qdockwidget [developer.qt.nokia.com]

October 26, 2010

genjix genjix
Lab Rat
20 posts

How can I see if a QDockWidget belongs to a particular tab bar?
EDIT: this http://doc.trolltech.com/4.7/qtabwidget.html [doc.trolltech.com]
(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)

  1.                 tabWidgetsAll = self.parent().findChildren(QTabWidget, None)
  2.                 # tab widgets which contain QDockWidget
  3.                 tabWidgets = []
  4.                 if len(tabWidgets) > 0:
  5.                     print 'yay'
  6.                     for w in widgets:                                                                                    
  7.                         for tab in tabWidgetsAll:
  8.                             if tab in tabWidgets:
  9.                                 continue
  10.                             if tab.indexOf(w) != -1:
  11.                                 tabWidgets.append(tab)
  12.                     for tab in tabWidgets:
  13.                         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:

  1.             for tab in self.activeTabs:
  2.                 tab[0].setCurrentIndex(tab[1])
  3.             self.activeTabs = []

October 26, 2010

genjix genjix
Lab Rat
20 posts

any ideas?

October 27, 2010

genjix genjix
Lab Rat
20 posts

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

  1. class QDockAreaLayout
  2. {
  3.   QDockAreaLayoutInfo hiddenDocks[4];  // hidden dock windows
  4.   bool dockHidden[4];  // initialise to false, false, false, false on initialiser
  5. };
  6. QDockAreaLayout::setVisible(QInternal::DockPosition pos, bool visible)
  7.  
  8. if (!visible) {
  9.     // copy
  10.     hiddenDocks[pos] = docks[pos];
  11.     dockHidden[pos] = true;
  12.     // clear the dock widget area
  13.     docks[pos].clear();
  14. } else {
  15.     docks[pos] = hiddenDocks[pos];
  16. }

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 [bugreports.qt.nokia.com]

October 27, 2010

Franzk Franzk
Lab Rat
830 posts

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.

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

Page  
1

  ‹‹ Color Underline ( stylesheet )      Static function getOpenFileName() of QFileDialog does not produce a native filedialog on KDE4 when linked static. ››

You must log in to post a reply. Not a member yet? Register here!