1

I have a QMainWindow opening a QDialog (not modal). When I minimize the main window, the dialog is closed as well. Can I somehow keep it open? (the other way round as in Keep QMainWindow minimized when QDialogs show() ).

One thing I have tried is to ignore the event, but to my surprise I never see such a state. Actually I only see ActivationChange (0n99) there.

    void CMyDialog::changeEvent(QEvent *evt)
    {
        QEvent::Type t = evt->type();
        if (t == QEvent::WindowStateChange)
        {
            evt->ignore();
            hide();
        }
        else
        {
            QDialog::changeEvent(evt);
        }
    }

Question in Qt center dealing with a similar topic: http://www.qtcentre.org/threads/24765-Intercept-minimize-window-event


Here I create it as member:

QScopedPointer<MyDialog> m_navigator{new MyDialog(this)}; // this here is the main application window

It is displayed by a public slot:

void MyDialog::toogleNavigator()
{
   this->setVisible(!this->isVisible());
}

and is a QDialog derived class:

class MyDialog : public QDialog { ...

---- Edit 2 ------

First Wouter has mentioned it , then Alexander. It is like you guys say, if I pass no parent (as in Alexander`s minimal example), the dialog stays open - with parent it is minimized along with its parent. My apologizes Wouter.

However, in my case it does not work like that. So I did not turn Wouter`s comment without checking or in bad intention. Now it is my duty to find out why. I suspect some utility classes to alter the dialog. I will report back here when I have found the root cause.

Ok, it is the windows flags. If the dialog is a tool window, it is always minimized, as normal window it depends on the parent.

Community
  • 1
  • 1
Horst Walter
  • 13,663
  • 32
  • 126
  • 228

1 Answers1

1

Try to create MyDialog without this(MainApplication) like parent and may be play with a second parameter of the constructor.

new MyDialog(0/*, ?*/);

Addition It is working code

MainWindow.cpp

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
    QScopedPointer<Dialog> dialog;
};

MainWindow.hpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    dialog(new Dialog(0))
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    dialog->setVisible(!dialog->isVisible());
}
Alexander Chernin
  • 446
  • 2
  • 8
  • 17