Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Beginners: how to show main widget after child exits



  • Hello guys,

    here is a beginner question. I made a widget X (QWidget) and when i click on a button, I create a new QWidget (y). However to save space on the screen, I hide the X window before I show y.

    I need to show the x window after y exits. Any Idea?

    thanks


  • Moderators

    If y is deleted, you can connect the y's "destroyed()" to X's "show()" slot



  • how can i do it?

    inside y I wrote this
    @
    #include "x.h"

    X t= new X;
    connect(this,this->close(), x, x.show());
    

    @
    could you please help me to formulate it?
    thanks

    [Edit: Added @ formatting tags -- mlong]


  • Moderators

    close() is a slot, not a signal. You want to connect this from outside y:
    @
    connect(y, SIGNAL(destroyed()), x, SLOT(show()));
    @

    ... but that will only work if y is deleted when it's closed. That means, you need to set this attribute: Qt::WA_DeleteOnClose ...read http://qt-project.org/doc/qt-4.8/qwidget.html#close for more details.

    Do you have experience with Qt signals and slots? If not, I recommend following a good beginner's tutorial, such as: http://doc.trolltech.com/4.3/tutorial.html This will help you learn much faster.



  • My friend, I am newby but I know signals and slots.

    Lets make the task simpler on two steps. When I click on a button in Y form,
    I can not show x from inside y.

    in y, I have the header #include "x.h"

    Should I create a new instance of X
    @
    x *t = new x
    x.show();
    @

    should I make parent child?



  • In show() method
    this->setVisible(true);



  • but this code will be in y and @this@ would refer to y.
    Am I right?

    To hide x before y loads, I used this.hide() in a x function.


  • Moderators

    Let's make it clearer by calling the classes ParentWidget and ChildWidget.

    [quote author="SherifOmran" date="1347214503"]Should I create a new instance of X[/quote]You can create a new instance of the ChildWidget, and destroy it when it closes.
    @
    ChildWidget *child = new ChildWidget();
    child->setAttribute(Qt::WA_DeleteOnClose);
    @

    [quote author="SherifOmran" date="1347214503"]should I make parent child?[/quote]This is a good idea. If you delete the parent, then Qt will automatically delete the child also. This makes memory-management easier, and helps avoid memory leaks.
    @
    // Constructor
    ChildWidget::ChildWidget(QWidget *parent)
    : QWidget(parent)
    {
    // ...
    }
    @

    @
    // Creation, inside ParentWidget
    ChildWidget *child = new ChildWidget(this); // Sets "this" as the parent
    @

    [quote author="SherifOmran" date="1347219741"]but this code will be in y and @this@ would refer to y.
    Am I right?

    To hide x before y loads, I used this.hide() in a x function.[/quote] You are right -- if your code is in y, "this" refers to y.

    However, I would put the code in the parent, not the child. That's because the parent should manage the child; the child should not manage the parent.

    I would put this code in ParentWidget:

    @
    void ParentWidget::createChild()
    {
    ChildWidget *child = new ChildWidget(this);
    child->setAttribute(Qt::WA_DeleteOnClose);

    connect(child, SIGNAL(destroyed()), this, SLOT(show()));
    
    child->show();
    this->hide();
    

    }
    @



  • Thank you my friend, i will test and let you know.



  • It does not work my friend. When I close the child, the parent is hidden and the software exists.



  • I made a slot called reshow and connected the signal child destroyed to it. What i get is that

    1- after closing the child window, it comes to the parent reshow slot, ignores the this.show command
    2- goes to the parent destroyed function and destroy the parent

    may be we need to drop event destroyed of the parent




Log in to reply