Frage

I have created a simple MDI parent-child application, basing on a tutorial found on the web. The QMainWindow has a pointer to child window. The following code is bundled with appropriate connect function.

main_window.h file

public slots:
    void openChildWindow();
private:
    Ui::MainWindow *ui;
    ChildWindow *childWindow;

main_window.cpp file

void MainWindow::openChildWindow()
{
   childWindow = new ChildWindow(ui->mdiArea); // Be sure to destroy you window somewhere
   childWindow->resize(400, 320);
   childWindow->show();
}

My question is what is the best way to store mdi child windows inside mdi parent? Should it be a list of pointers to child windows?

With current implementation I can both create and close many mdi children at the same time, but sometimes they leave a shade after they're closed (e.g. when I minimalize and maximize a child window). Could it be that I forget to call mdi child destructor somewhere? Currently it's only:

ChildWindow::~ChildWindow()
{
    delete ui;
    std::cout << "I\'m dead!" << std::endl;
}

but I do not call it anywhere manually. When the whole Qt program is terminated I can see "I'm dead" in the Qt Creator console (but it should appear when I close the child window)

War es hilfreich?

Lösung

There is no need to store a list of pointers to the MDI subwindows; this functionality is already provided by QMdiArea::subWindowList.

Regarding the other problem, I suggest setting the attribute WA_DeleteOnClose for the subwindows (subWindow->setAttribute(WA_DeleteOnClose);) which will ensure that the subwindow's destructor is called when it is closed. If that doesn't solve the problem, I suggest creating a separate question for that.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top