PDA

View Full Version : How to set focus (select) a tabbed QDockWidget?



serget
12th March 2012, 21:37
Hi,

I'm adding two QDockWidget controls in this way:

QDockWidget *templateDock = ....
addDockWidget(Qt::LeftDockWidgetArea, templateDock);

QDockWidget *propertyDock = ...

tabifyDockWidget(templateDock, propertyDock);
templateDock->raise();


I want the first dock (template Dock to have a focus - be active), but tabifyDockWidget() makes the second widget active.
raise() method does not work.

Please help.

Spitfire
13th March 2012, 16:37
Maybe setFocus() ?

serget
13th March 2012, 21:02
No, this did not help.

ChrisW67
13th March 2012, 22:19
You don't tell us which platform you are using. QWidget::raise() works just fine here on Linux. It's possible that it won't work on your platform before the widget is shown, i.e. after the constructor is finished. Try this:



MainWindow(QWidget *p = 0): QMainWindow(p) {
...

templateDock = new QDockWidget(tr("Template"), this);
templateDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
templateDock->setWidget(new QTextEdit(templateDock));
addDockWidget(Qt::LeftDockWidgetArea, templateDock);

propertyDock = new QDockWidget(tr("Property"), this);
propertyDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
propertyDock->setWidget(new QTextEdit(propertyDock));
addDockWidget(Qt::LeftDockWidgetArea, propertyDock);

tabifyDockWidget(templateDock, propertyDock);
// templateDock->raise(); // <<<< works here on Linux

QTimer::singleShot(0, this, SLOT(tweakUi())); // <<<< Try deferring it
}

public slots:
void tweakUi() {
templateDock->raise();
}

serget
14th March 2012, 16:40
Chris,

thank you for the hint - delayed raise() did work. I also found the code which prevented the raise() to work in first place:

connect(this, SIGNAL(updateContent(NodeCore &, bool)), propertyView, SLOT(onUpdateContent(NodeCore &, bool)));
tabifyDockWidget(m_templateDock, m_propertyDock);
m_templateDock->raise();

Apparently onUpdateContent() slot belonging to propertyView which is displayed inside of propertyDock was called after this code (which was executed in constructor) and caused propertyDock to become active. I still don't understand the reason why - I commented out the whole onUpdateContent() slot and it was still interfering with raise() by activating propertyDock (presumably from templateDock). Only if I comment out the whole connect() line of code, the problem disappears.

The code is running under Windows XP.