Keep QMainWindow minimized when QDialogs show()

2019-09-04 21:51发布

问题:

I have an application with QMainWindow as the UI which is in minimized state, after some time the application throws a message by calling messageDlg->show() (messageDlg is a QDialog object). Something like this

void MainWindow::WarningDialog() 
{
    m_messageDialog = new QDialog(this);
    m_messageDialog ->show();
}

This results in my QMainWindow in normal mode which I don't want to happen i.e. am trying to keep the application in minimized window even if any QDialog.show() is called.

I don't want the keep checking if the application in minimized mode every time a QDialog->show() is called.

I've tracked all events posted to QMainWindow::event() but the only event I see happening before restoring my window is a QEvent::WindowStateChange i.e. the window state has already changed from minimized mode.

Is there a way to keep the QMainWindow minimized even if any QDialogs are shown?

回答1:

QWidget has slot showMinimized(). You should create QDialog without parent as QMainWindow. In your QDialog set attribute (e.g.

 QDialog *dialog = new QDialog;
 dialog->setAttribute(Qt::WA_DeleteOnClose);

), then you can set showMinimized() for QMainWindow in time when your QDialog had start.