Qt World Summit: Register Today!

Get first QDockWidget from a given area

  • How can I find the first QDockWidget for a given dock area (ie,. Qt::BottomDockWidgetArea) ?


  • There isn't really an API provided for achieving this. You could check the positioning of the dockwidgets though to figure out which one is first in the area. The example below illustrates this idea. Does following this approach help?

    #include <QtGui>

    class MainWindow : public QMainWindow

    QTextEdit *edit1 = new QTextEdit(this);

    QDockWidget *topDock = new QDockWidget(this);
    topDock->setObjectName("Top dock");
    topDock->setWidget(new QLineEdit("Top dock", topDock));
    addDockWidget(Qt::TopDockWidgetArea, topDock);

    dock1 = new QDockWidget(this);
    dock1->setObjectName("Dock 1");
    dock1->setWidget(new QLineEdit("First dock", dock1));
    dock2 = new QDockWidget(this);
    dock2->setObjectName("Dock 2");
    dock2->setWidget(new QPushButton("Second dock", dock2));

    addDockWidget(Qt::BottomDockWidgetArea, dock1);
    addDockWidget(Qt::BottomDockWidgetArea, dock2, Qt::Vertical);
    QTimer timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(testSlot()));
    dockList = findChildren<QDockWidget

    public slots:
    void testSlot()
    for (int i = 0; i < dockList.count(); i++) {
    QDockWidget *dock = dockList.at(i);

    int dockTopLeft = dock->geometry().topLeft().y();
    int  centralWidBottom = centralWidget()->geometry().bottomLeft().y();
    int offset = dockTopLeft  - centralWidBottom;
    if (dockWidgetArea(dock) == Qt::BottomDockWidgetArea && dock->geometry().bottomLeft().x() == 0 && offset < 10) {
     qDebug() << dock->objectName() << " is first in the bottom dock area";


    QDockWidget *dock1;
    QDockWidget dock2;
    > dockList;


    #include "main.moc"
    int main(int argc, char** argv)
    QApplication app(argc, argv);
    MainWindow window;
    return app.exec();


  • Thanks, I ended up doing something similar. I'm surprised there are not direct api calls to help with this.

Log in to reply