Focusing on a tabified QDockWidget in PyQt
Question
I have three QDockWidgets which are tabbed at startup using QMainWindow.tabifyDockWidget
.
In the main window, after all of the addDockWidget
calls:
self.tabifyDockWidget(self.dock_widget1, self.dock_widget2)
self.tabifyDockWidget(self.dock_widget1, self.dock_widget3)
Based on certain actions, I'd like to select one of these tabs and bring it to focus, or, on top of the other two, if it's not already visible. I've tried using setVisible
and setWindowState(Qt.WindowActive)
, but nothing changes.
Is there a way to programmatically select a tabbed dock widget and bring it to the front?
Solution
Thanks to an answer on the qt-interest mailing list, this is very simple to do with QWidget.raise()
:
http://qt-project.org/doc/qt-4.8/qwidget.html#raise
In PyQt, it's QWidget.raise_()
:
OTHER TIPS
For me:
dock2.show();
dock2.raise();
was enough. But yes, both are needed!
I haven't tested this, but here's what I would try in Qt 4.5+ (I'll leave the PyQt conversion to you):
class MyMainWindow ; // A QMainWindow
void MyMainWindow::bringToFront( QDockWidget* dockIn )
{
QList<QDockWidget*> docks = tabifiedDockWidgets( dockIn ) ;
foreach( QDockWidget* dock, docks )
{
// Move second dock on top of first dock widget.
tabifyDockWidget( dock, dockIn ) ;
}
}
See QMainWindow::tabifiedDockWidgets()
and QMainWindow::tabifyDockWidget()
.
This didn't work for me:
dock2.raise_()
I managed to get it working using:
dock2.show()
parent.tabifyDockWidget(dock1, dock2)
If you are looking for a solution to set the focus to the widget and not leave it on a tabified QDockWidget in the background you can use the visibility-changed signal and a focus proxy like this:
class Dock(QDockWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.visibilityChanged.connect(self.setVisibility)
def setVisibility(self, visible):
if visible: self.setFocus()
def setWidget(self, widget:QWidget):
super().setWidget(widget)
self.setFocusProxy(widget)
a little working example:
app = QApplication([])
main = QMainWindow()
dock1 = Dock('Dock1', main )
dock1.setWidget(QTextEdit(dock1))
dock2 = Dock('Dock2', main )
dock2.setWidget(QTextEdit(dock2))
main.addDockWidget(Qt.LeftDockWidgetArea, dock1)
main.tabifyDockWidget(dock1, dock2)
main.show()
app.exec()
A solution that is working for me is :
tabifyDockWidget(dock1, dock2)
dock2.setVisible(True)
dock2.setFocus()
dock2.raise_()
That 3 functions seem necessary.