Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Get first QDockWidget from a given area



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

    Thanks.



  • 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
    {
    Q_OBJECT
    public:
    MainWindow()
    {

    QTextEdit *edit1 = new QTextEdit(this);
    setCentralWidget(edit1);

    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);
    timer->start(3000);
    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";
    }
    

    }}

    private:
    QDockWidget *dock1;
    QDockWidget dock2;
    QList<QDockWidget
    > dockList;

    };

    #include "main.moc"
    int main(int argc, char** argv)
    {
    QApplication app(argc, argv);
    MainWindow window;
    window.show();
    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