Minimize mainwindow but keep dock window up?



  • I have a main window which has a dock window. Users are asking for a way to minimize the main window but keep the dock window up.
    Is that possible with qt?

    QWidget doc says this which I take to mean there is not a way to do this:
    Calling setVisible(true) or show() sets the widget to visible status if all its parent widgets up to the window are visible. If an ancestor is not visible, the widget won't become visible until all its ancestors are shown.

    Any ideas or suggestions?

    Thanks



  • You can fake it by reparenting the dock widget content with something like this:

    @#include <QtGui>

    class MainWindow: public QMainWindow
    {
    Q_OBJECT
    public:
    MainWindow(QWidget *p=0): QMainWindow(p) {
    QTextEdit *e = new QTextEdit(this);
    setCentralWidget(e);

        dockContent = new QFrame(this);
        dockContent->setFrameStyle(QFrame::Panel | QFrame::Plain);
    
        dockWidget = new QDockWidget(this);
        dockWidget->setWidget(dockContent);
    
        addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
    }
    void showEvent(QShowEvent *event) {
        if (event->spontaneous() && isMinimized()) {
            dockWidget->setWidget(dockContent);
        }
    }
    void hideEvent (QHideEvent *event) {
        if (event->spontaneous() && !isMinimized()) {
            dockContent->setParent(0);
            dockContent->show();
        }
    }
    void closeEvent(QCloseEvent *event) {
        // make sure the dock is cleaned up somewhere
        delete dockContent;
        event->accept();
    }
    

    private:
    QDockWidget *dockWidget;
    QFrame *dockContent;
    };

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

    MainWindow m;
    m.show();
    
    return app.exec();
    

    }
    #include "main.moc"
    @

    Needs some tidying up, e.g. putting the re-parented content in the same place it was inside the dockwidget, and testing... lots of testing.



  • Thanks for the hint.

    I cobbled up this program which almost works as I desire.

    It brings up a main window with a docking window docked to it. When I run the program and click on the minimize button on the main window, the main window goes down and the docked window stays up. I can move the docked window around with the mouse. All is good.

    However, if I run the program, undock the docked window and then click on the minimize button on the main window. Then the main window goes down and the dock window stays up. However, I cannot move the dock window around with the mouse.

    Why?

    I tried to dock the docked window in the main window's hideEvent before changing the parent of the dock window. This did not do anything. I mean, I don't think the addDockWidget did anything because after calling it, the dock window's isFloating is still true.

    Any ideas?

    Here is the small program:

    main.cpp
    @#include <QApplication>
    #include <QTextEdit>
    #include "MainWindow.hpp"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QApplication::setStyle("plastique");

    QMainWindow* mainWin = new MainWindow;
    QTextEdit* centerWin = new QTextEdit;
    QTextCharFormat boldFormat;
    QTextCursor cursor(centerWin->textCursor());
    cursor.movePosition(QTextCursor::Start);
    cursor.insertText("MAIN WINDOW", boldFormat);
    
    mainWin->setCentralWidget(centerWin);
    
    mainWin->show();
    return app.exec();
    

    }
    @

    MainWindow.hpp
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QHideEvent>
    #include <QDebug>
    #include "DockingWin.hpp"

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    MainWindow();

    void showEvent(QShowEvent *event);

    void hideEvent (QHideEvent *event);
    void closeEvent(QCloseEvent *event);

    private:
    DockingWin* myDockWindow;

    };

    #endif // MAINWINDOW_H
    @

    MainWindow.cpp
    @#include <QtGui>
    #include "DockingWin.hpp"
    #include "MainWindow.hpp"

    MainWindow::MainWindow()
    {
    myDockWindow = new DockingWin(this);
    addDockWidget(Qt::LeftDockWidgetArea, myDockWindow);
    }

    void MainWindow::showEvent(QShowEvent *event) {
    qDebug() << "in showEvent";
    if (event->spontaneous() && isMinimized()) {
    qDebug() << "in spontaneous && !isMinimized";
    qDebug() << "isFloating=" << myDockWindow->isFloating();
    qDebug() << "features=" << myDockWindow->features();
    myDockWindow->setParent(this);
    qDebug() << "isFloating=" << myDockWindow->isFloating();
    addDockWidget(Qt::LeftDockWidgetArea, myDockWindow);
    qDebug() << "isFloating=" << myDockWindow->isFloating();
    }
    };

    void MainWindow::hideEvent (QHideEvent *event) {
    qDebug() << "in hideEvent";
    if (event->spontaneous() && !isMinimized()) {
    qDebug() << "in spontaneous && !isMinimized";
    qDebug() << "isFloating=" << myDockWindow->isFloating();
    qDebug() << "features=" << myDockWindow->features();

    if (myDockWindow-&gt;isFloating()) {
      addDockWidget(Qt::LeftDockWidgetArea, myDockWindow);
      //myDockWindow-&gt;setFloating(false);
    }
    qDebug() &lt;&lt; "isFloating=" &lt;&lt; myDockWindow-&gt;isFloating();
    myDockWindow-&gt;setParent(0);
    myDockWindow-&gt;show();
    qDebug() &lt;&lt; " ";
    

    }
    };

    void MainWindow::closeEvent(QCloseEvent *event) {
    qDebug() << "in closeEvent";
    delete myDockWindow;
    event->accept();
    };
    @

    DockingWin.hpp
    @#ifndef DOCKING_WIN_H
    #define DOCKING_WIN_H

    #include <QDockWidget>
    #include <QDebug>
    #include <QWidget>

    class DockingWin : public QDockWidget
    {
    Q_OBJECT

    public:
    DockingWin(QWidget* parent=0);

    };

    #endif // DOCKING_WIN_H
    @

    DockingWin.cpp
    @#include <QtGui>
    #include "DockingWin.hpp"

    DockingWin::DockingWin(QWidget* parent)
    : QDockWidget(parent)
    {
    setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    QString title = "Docking Window";
    setWindowTitle(title);
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);

    QTextEdit *centerWin = new QTextEdit;
    QTextCursor cursor(centerWin->textCursor());
    cursor.movePosition(QTextCursor::Start);
    QTextCharFormat boldFormat;
    cursor.insertText("DOCK WINDOW", boldFormat);

    setWidget(centerWin);
    }
    @

    Thanks



  • Hmmm, my version also does that unless I resize the content widget enough to get the title on the title bar, then I can move it. No obvious solution springs to mind. You could perhaps play with the window flags to remove the system menu etc.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.