Search code examples
qtqt5qdockwidgetqt5.3qstatusbar

QDockWidget with QStatusBar possible?


For a QMainWindow I can easily set a status bar. When I have a floating QDockWidget, it behaves like a normal window (from user's perspective).

What I want to archive is to add a QStatusBarto the floating QDockWidget. With the signal topLevelChanged I could hide the status bar when the window is docked.

But can I add a status bar to a QDockWidget? At least in the Qt Creator form builder I can not (I have a context menu "Create Status Bar" for QMainWindow, but not for QDockWidget).

Any way to do it a runtime?


Solution

  • Just do this:

    QStatusBar *bar = new QStatusBar;//in constructor for example
    bar->showMessage(tr("Ready"));
    ui->dockWidget->setWidget(bar);
    

    In this case QStatusBar will be as widget, but you can also use some QWidget as container and add layout where your QStatusBar will be always bottom.

    With QProgressBar:

    QStatusBar *bar = new QStatusBar;
    QProgressBar *pr = new QProgressBar;
    bar->showMessage(tr("Ready"));
    pr->setValue(50);
    bar->addPermanentWidget(pr);
    ui->dockWidget->setWidget(bar);
    

    Result(there is "Ready" too, but you can't see it because of size of my window) :

    enter image description here

    Little example with another part of your question:

    QStatusBar *bar = new QStatusBar;
    QProgressBar *pr = new QProgressBar;
    bar->showMessage(tr("Ready"));
    pr->setValue(50);
    bar->addPermanentWidget(pr);
    ui->dockWidget->setWidget(bar);
    
    connect( ui->dockWidget,&QDockWidget::topLevelChanged,[=](bool visible)
    {
        if(visible)
            bar->hide();
        else
            bar->show();
     });
    

    I used here C++11 (CONFIG += c++11 to .pro file) and new syntax of signals and slots, but of course you can use old syntax if you want.