Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QDockWidget is closed if main window is minimized

Tags:

c++

qt

I'm using Qt 4.7 on Windows 7 Ultimate 32 bit.

The QMainWindow of my program has a QDockWidget. I've noticed that if I minimize the main window by the minimize button on the title bar, after restoring it the dock widget is closed. I didn't write any support for a feature like this!

How does this happen and how to prevent this?

Thanks.

like image 635
Donotalo Avatar asked Jan 08 '11 12:01

Donotalo


2 Answers

I encountered this error when writing my own application. I have QDockWidget with options for my application. Using Qt Creator I created menu with QAction actionMenu which was checkable. Then I connected QDockWidget and QAction like this:

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 order of connection is not important. And then when I minimized application with QDockWidget being visible, after I restored it QDockWidget was closed and actionMenu was unchecked.

In fact there are two solutions. First which works for me is 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 action which you can obtain 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)) was causing in my application closure of QDockWidget.

like image 181
calkapokole Avatar answered Sep 20 '22 10:09

calkapokole


I faced the same problem... I managed to get rid of it by using a method called StoreWindowsLayout and RestoreWindowsLayout.

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

RestoreWindowsLayout will restore that bytearray, and therefore your windows layout, the qdockwidget visibility state and so on...

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

Then I use restoreWindowsLayout in ApplicationMainFrm::showEvent.

Exemple of use of restoreWindowsLayout in my MainForm :

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

    restoreWindowsLayout();
}

Hope it helps !

like image 3
Andy M Avatar answered Sep 20 '22 10:09

Andy M