How to close parent UI window when child UI window

2019-03-02 08:13发布

问题:

I have multiple UI windows in my QT project. When a new UI window opens, the previous UI window must be closed, that is, at every point of time only one UI window must be open. How can this be done?

回答1:

I did that before and i suggest you to not close(delete) UI.

just hide it and when you need it show it again.

check this code:

when user click to see second UI:

void MainApp::on_btnSettings_clicked()
{
    this->hide();
    settingsManager = new SettingsManager(); // put this line in constructor
    settingsManager->show();
}

on second UI on closing form(or back button) emit a signal:

void SettingsManager::closeEvent(QCloseEvent *event)
{
    emit settingsBackToMainApp();
}

on main hide second class and show main:

void MainApp::settingsBackToMainApp()
{
    settingsManager->hide();
    this->show();
}

connect signal to slot:

connect(settingsManager,&SettingsManager::settingsBackToMainApp,this,&MainApp::settingsBackToMainApp); // put this line in constructor


标签: c++ qt qt5