QT-如何通过按下按钮从子窗口小部件返回到父窗口小部件

QT - how to go back from child widget to parent with button press

本文关键字:小部 窗口 返回 何通过 按钮 QT-      更新时间:2023-10-16

如何在按下PushButton的情况下从子窗口小部件视图返回到父窗口小部件?

Parent.cpp的一部分,带有子部件初始化:

void Menu::on_pushButton_phone_numbers_clicked()
{
Child child_window(this);
child_window.setModal(true);
child_window.exec();        //child_window.show() only pops out and close program
this->hide();
}

Child.cpp的一部分,我试图回到Parent Widget:

void Child::on_pushButton_parent_clicked()
{
parentWidget()->setHidden(false); // also tried with parentWidget().show()
this->close();       //that results with closing whole program
}

我应该考虑在Parent.cpp中使用connect((吗?还是我应该走另一条路?

或者有没有任何文档可以让我找到如何正确操作的答案?

编辑:主要问题是当调用parent.hide((时-即使动态分配Child-每次尝试隐藏或关闭子窗口小部件时都会引发n_pushButton_pparent_clickd((,这将导致父窗口弹出并关闭整个程序

我会做一些类似的事情

void Menu::on_pushButton_phone_numbers_clicked()
{
Child child_window(this);
connect(child_window, &Child::sig_show, this, [this]{this->show();});
child_window.setModal(true);
child_window.exec();
this->hide();
}
void Child::on_pushButton_parent_clicked()
{
emit sig_show();
this->close();
}

我还没有测试过,但这是我会使用的逻辑。