QT: Show child window from parent, vice versa


In due course of time, once you start getting comfortable with c++ you will get ambitious and want to build a multiple window application as in visual studio or other languages. It happened to me too.. The major problem I faced (which many may find silly), was how to show the child window from parent window on button click and how to show the parent window when the child window is closed.

This small tut is the result of that confusion and search.

The below diagram is what I am trying to accomplish, (The “Close Button” on the child window means the “X” button used to close the window)

What I am trying to accomplish

The folder structure/file structure I am using is as below,

Root Folder (Project)










In, childform.h

#include "parentform.h"





   ParentForm *mainParentForm;

In your parentwindow.cpp, declare an instance of the child window after importing it. like,

ChildForm *childForm;

ParentForm::ParentForm(QWidget *parent) : QMainWindow(parent),ui(new Ui::ParentForm)




Now, comes the part of wiring your event. In the button click in the Main parent window, (parentwindow.cpp), add the following code,

void ParentForm::on_btnOpenChildWindow_clicked()


  if(!childForm) {childForm = new ChildForm();}



    childForm->mainParentForm = this;





The above part of code should effectively hide the parent window and show your child window. Now, to the second part of closing the child window and opening the parent window.

In your childForm.h, add the following after or before public: declaration, the file should look like, (Add the code in BOLD)







<strong>&nbsp;&nbsp;&nbsp; void closeEvent(QCloseEvent *event);</strong>



This closeEvent will be triggered once you close the child window clicking the close button.

Now, in childform.cpp, add the following,

void ChildForm::closeEvent(QCloseEvent *event)


&nbsp;&nbsp;&nbsp; this->mainParentForm->show();

&nbsp;&nbsp;&nbsp; event->accept();


Remember to import –>

#include "QCloseEvent"

That is all, now, run the application and it should work as in the diagram. I will upload a sample code soon..

Share this:
Share this page via Email Share this page via Stumble Upon Share this page via Digg this Share this page via Facebook Share this page via Twitter

Warning: Missing argument 1 for cwppos_show_review(), called in /home/thedaz5/public_html/source/wp-content/themes/flat/content-single.php on line 29 and defined in /home/thedaz5/public_html/source/wp-content/plugins/wp-product-review/includes/legacy.php on line 18

1 comment

Leave a Reply

Your email address will not be published. Required fields are marked *

CommentLuv badge