SomeRandomNick
SomeRandomNick

Reputation: 31

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

How can I go back from Child widget view to Parent widget view with PushButton pressed?

part of Parent.cpp with Child Widget Init:

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();
    }

part of Child.cpp where I try to go back to Parent Widget:

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

Should I consider using connect() in Parent.cpp? Or should I go other way?

Or is there any documentation where I can find answer how to do it properly?

Edit: the main problem is when parent.hide() is called - even with dynamic allocation of Child - When_pushButton_parent_clicked() is raised every attempt to hide or close child widget , it will result with parent pop out and close of whole program

Upvotes: 0

Views: 480

Answers (1)

Nox
Nox

Reputation: 924

I would have done something like

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();
}

I haven't tested it, but this is the logic I would have used.

Upvotes: 1

Related Questions