How to Open & Close a Widget ?!!
I've created an app that contains a Widget . I've put all the ctrls I need into it .
I've created my own Slots by creating a (Header + cpp) files to implement the functionality I want .
Everything ,so far , goes fine .
Now I've created another Widget . when I try to display the 2nd widget it just appears for a second !!
-I've created 2 HeaderFiles +2 .CPP files ; I mean each widget has its .cpp + .h .
My Questions are :
1- I need when the user press a button @ the 1st widget
, the 2nd widget shows .
2- How Could I close a specific Widget ?!!
3 - Do I 've to create @ my app a "Main Window"
4- Finally , Do I 've to create a Header + .cpp file to each Widget I've ?!!
My Main function's Code :
Code:
#include <QApplication>
#include <QDialog>
#include"CustomSlot.cpp"
#include "ui_Test.h"
int main(int argc, char *argv[])
{
CustomSlot widget;
widget.show();
return app.exec();
}
-------------------------------------------------------------------------------------------
First CustomSlot.cpp 's code :
Code:
#include"CustomSlot.h"
#include "ui_Test.h"
#include "ui_Form2.h"
#include"CustomSlot2.cpp"
{
ui.setupUi(this);
}
void CustomSlot:: OpenNxtWindow()
{
CustomSlot2 widget;
widget.show();
}
-----------------------------------------------------------------
Second CustomSlot.cpp 's code :
Code:
#include"CustomSlot2.h"
#include "ui_Form2.h"
{
formTwo.setupUi(this);
}
Thanks.
Re: How to Open & Close a Widget ?!!
The variable goes out of scope according to normal C++ rules and gets immediately destructed after the OpenNxtWindow() function ends. To make it remain alive you should allocate it on the heap with operator "new":
Code:
void CustomSlot:: OpenNxtWindow()
{
CustomSlot2* widget = new CustomSlot2;
widget->setAttribute(Qt::WA_DeleteOnClose);
widget->show();
}
or if it's a dialog you can use QDialog::exec() to produce a modal dialog:
Code:
void CustomSlot:: OpenNxtWindow()
{
CustomSlot2 widget(this);
widget.exec();
}
Re: How to Open & Close a Widget ?!!
As usual , Jbn , Your answeris accurate .
So, I'll need a header + cpp files to each form I've .
Thanks .
Re: How to Open & Close a Widget ?!!
Well , I've another inquiry .
- After I've opened a new widget , I 've put Button + QLineEdit @ the new open Widget just to test it .
- What I 've found out is that ctrls @ the new open widget don't call the slots I implement ?!!
- @ the new widget it's supposed when the user press the button , a text 'll be printed into QLineEdit .
- When I Put the code that print some text into QLineEdit into Constructor , it Works .
- Also when I use the built in slots (e.g. showMaximized() ) , it works .
- My Question : Why don't the ctrls @ the new open Widget call the slots I implement ?!!
N.B. : I've a Header + .cpp file to each form I've in order to implement my own slots .
The code which I use to open my new widget that's located @ the first .cpp which I implement into it the slots of the 1st widget :
Code:
CustomSlot2* widget = new CustomSlot2;
widget->setAttribute(Qt::WA_DeleteOnClose);
widget->show();
----------------------------------------------------------------------
My .cpp file 's code , which I implement the slots of ctrls of 2nd new open widget :
Code:
#include"CustomSlot2.h"
#include "ui_Form2.h"
{
formTwo.setupUi(this);
}
void CustomSlot2::PrintTxt
{
formTwo.txtData->setText("Alooooooo") ;
}
Thanks
Re: How to Open & Close a Widget ?!!
Did you remember to add required Q_OBJECT macro to CustomSlot2 class? Did you remember to declare CustomSlot2::PrintTxt() as slot?
Re: How to Open & Close a Widget ?!!
Mmmmm , actually it was a SILLY error ; I forgot to write the keyword "slot" beside the access modifier @ header file .
By the way , what's the use of :
widget->setAttribute(Qt::WA_DeleteOnClose);
As when I 've commented it , nothing is changed !!
Thanks JPN
Re: How to Open & Close a Widget ?!!
Quote:
Originally Posted by
Fatla
By the way , what's the use of :
widget->setAttribute(Qt::WA_DeleteOnClose);
As when I 've commented it , nothing is changed !!
It ensures that the widget gets deleted once closed. You know, closing a window doesn't delete it by default. And there is no parent to clean it up either because it has no parent, it's a top level widget aka window.
You're welcome. :)