c++ - QT - 如何通过按下按钮从子部件返回到父部件

标签 c++ qt

如何在按下按钮的情况下从子窗口小部件 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/

相关文章:

c++ - 在 Clang++ 的 C++17 中不能禁用 RVO/NRVO?

c++ - 从 QVariant 参数转换时如何选择方法重载?

python - PySide2 与 qml 作为 ui

qt - QScopedPointer 与 boost::scoped_ptr [选择哪一个]

c++ - 带有 if 条件的编译器错误

c++ - qwt 图表示例

c++ - 提升 Python 和 shared_ptr 的 vector

c++ - 我可以使用 Qt 将字节写入音频输出吗?

c++ - Qt 向导后退按钮 Ui 问题

c++ - 为什么我需要将reinterpret_cast从 `long &`到 `int &`,其中两者都是32位(Windows LLP64)?