如何在按下按钮的情况下从子窗口小部件 View 返回到父窗口小部件 View ?
带有子控件初始化的 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 的一部分:
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() 时 - 即使动态分配子项 - 当每次尝试隐藏或关闭子小部件时都会引发_pushButton_parent_clicked() ,这将导致父项弹出并关闭整个子项程序
最佳答案
我会做类似的事情
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();
}
我还没有测试过,但这是我会使用的逻辑。
关于c++ - QT - 如何通过按下按钮从子部件返回到父部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58867155/