2

I have a Qt5.11.0 application (on OSX10.13 and CentOS7.4) with multiple QMainWindows. I would like users to be able to dock any given QDockWidget in the application into any of the QMainWindow instances.

The sample code below shows an application with two QMainWindows and one QDockWidget. I can undock and re-dock the QDockWidget from and to the first QMainWindow to which it was attached, however if I hover the detached QDockWidget over the second QMainWindow, the QDockWidget and the second QMainWindow ignore each other, rather than docking together as hoped:

#include <QApplication>
#include <QMainWindow>
#include <QDockWidget>
#include <QLabel>

int
main( int argc, char *argv[] ) {
    QApplication app( argc, argv );

    QMainWindow* mw1 = new QMainWindow();
    QMainWindow* mw2 = new QMainWindow();

    mw1->setWindowTitle( "Main Window 1" );
    mw2->setWindowTitle( "Main Window 2" );

    mw1->setCentralWidget( new QWidget() );
    mw2->setCentralWidget( new QWidget() );

    QDockWidget* dockWidget = new QDockWidget( "Stepchild", mw1 );
    QLabel* label = new QLabel( "Hello World" );
    dockWidget->setWidget( label );

    mw1->addDockWidget( Qt::LeftDockWidgetArea, dockWidget );

    mw1->show();
    mw2->show();

    QRect first = mw1->geometry();
    int pixeloffset = 200;
    mw2->setGeometry( first.x() + pixeloffset, 
                      first.y() + pixeloffset, 
                      first.width(), 
                      first.height() );

    return app.exec();
}

How can I allow my QDockWidget above to dock in any of the QMainWindows in the application?

Thanks --

lcikgl
  • 759
  • 1
  • 8
  • 20
  • After continued searching I've found some other relevant links. First, a very closely related ticket: https://stackoverflow.com/questions/35287958/dragging-qdockwidgets-between-qmainwindows?rq=1 (similar to this post, though the cited question also asks about docking into other dock widgets, which I don't need here) – lcikgl Feb 14 '19 at 15:00
  • Also a bug report, apparently not yet evaluated at the time of this writing, reporting this as an alleged bug in Qt: https://bugreports.qt.io/browse/QTBUG-64595 – lcikgl Feb 14 '19 at 15:01
  • Also a porported solution, though one that has elicited some criticism (above) for shortcomings: https://www.qtcentre.org/threads/41847-Dragging-QDockWidgets-between-QMainWindows – lcikgl Feb 14 '19 at 15:03
  • Also a useful partial clue: https://stackoverflow.com/questions/38221768/how-to-detect-when-a-qdockwidget-is-moved-out-of-a-qmainwindow?rq=1 – lcikgl Feb 14 '19 at 15:06

0 Answers0