我想創建一個鏈中多個窗口:窗口1窗口2的父,窗口2窗口3等。當我關閉一個窗口,我想它的所有兒童,關閉以及母公司。目前,如果我關閉頂層窗口,所有其他人關閉,如希望的,但關閉,例如窗口2,只關閉窗口2,而不是窗口3等。我應該怎麼做?謝謝你的幫助!Qt:父/子鏈中的多個窗口,父母不關閉孩子?
main_window.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
QPushButton* button = new QPushButton("Open 1", this);
connect(button, SIGNAL(clicked()), this, SLOT(on_button_clicked()));
}
void MainWindow::on_button_clicked() {
window1 *w = new window1(this);
w->show();
}
window1.cpp
window1::window1(QWidget *parent) : QWidget(parent)
{
this->setWindowFlags(Qt::Window); // in order to have a free-standing window
QPushButton* button = new QPushButton("Open 2", this);
connect(button, SIGNAL(clicked()), this, SLOT(on_button_clicked()));
}
void window1::on_button_clicked() {
window2 *w = new window2(this);
w->show();
}
window2.cpp
window2::window2(QWidget *parent) : QWidget(parent)
{
this->setWindowFlags(Qt::Window);
QLabel* label = new QLabel("Window 2", this);
}
這樣做伎倆!添加「w-> setAttribute(Qt :: WA_DeleteOnClose);」在上面的mainwindow.cpp中工作。很高興我不必重新實施closeEvent。謝謝! – ishmael 2010-12-16 21:33:54
我不知道這個小部件在默認情況下並沒有關閉。這很有幫助。不知何故,爲我的PySide窗口設置窗口屬性爲WA_DeleteOnClose解決了分段故障問題。現在我只需要弄清楚爲什麼... – lightalchemist 2012-04-26 06:22:46