QDockWidget closes if the main window is minimized

I am using Qt 4.7 for Windows 7 Ultimate 32 bit.

QMainWindow my program has a QDockWidget . I noticed that if I hide the main window with the "Minimize" button in the title bar, after restoring it, the dock widget will be closed. I did not write any support for such a function!

How does this happen and how to prevent it?

Thanks.

+4
source share
2 answers

I had the same problem ... I was able to get rid of it using the StoreWindowsLayout and RestoreWindowsLayout methods.

StoreWindowsLayout will save the ByteArray returned by QMainwindow :: saveState ().

RestoreWindowsLayout will restore this bytearray and therefore your window layout, qdockwidget visibility state, etc ...

I call StoreWindowsLayout on ApplicationMainFrm :: changeEvent, on ApplicationMainFrm :: closeEvent (you'll probably need this) and in ApplicationMainFrm :: hide ().

Then I use restoreWindowsLayout in ApplicationMainFrm :: showEvent.

Example of using restoreWindowsLayout function in my MainForm:

 void ApplicationMainFrm::showEvent(QShowEvent* pEvent) { QMainWindow::showEvent(pEvent); restoreWindowsLayout(); } 

Hope this helps!

+3
source

I encountered this error while writing my own application. I have a QDockWidget with parameters for my application. Using Qt Creator, I created a menu with a QAction actionMenu that could be checked. Then I hooked up QDockWidget and QAction as follows:

 QObject::connect(ui->dockWidget, SIGNAL(visibilityChanged(bool)), ui->actionMenu, SLOT(setChecked(bool))); QObject::connect(ui->actionMenu, SIGNAL(toggled(bool)), ui->dockWidget, SLOT(setVisible(bool))); 

The connection order does not matter. And then, when I minimized the application with QDockWidget visibility, after I restored it, QDockWidget was closed, and actionMenu was not marked.

There are actually two solutions. First, I work to use SIGNAL (triggered (bool)) instead of SIGNAL (toggled (bool)):

 QObject::connect(ui->dockWidget, SIGNAL(visibilityChanged(bool)), ui->actionMenu, SLOT(setChecked(bool))); QObject::connect(ui->actionMenu, SIGNAL(triggered(bool)), ui->dockWidget, SLOT(setVisible(bool))); 

The second solution uses an action that you can get from QDockWidget:

 // Retrieve action from QDockWidget. QAction *action = ui->dockWidget->toggleViewAction(); // Adjust any parameter you want. action->setText(QString("&Menu")); action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_M)); action->setStatusTip(QString("Press to show/hide menu widget.")); action->setChecked(true); // Install action in the menu. ui->menuOptions->addAction(action); 

I know for sure that SIGNAL (toggled (bool)) causes QDockWidget to close in my application.

+5
source

Source: https://habr.com/ru/post/1334745/


All Articles