PDA

View Full Version : How to Open & Close a Widget ?!!



Fatla
11th June 2008, 12:57
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 :



#include <QApplication>
#include <QDialog>
#include"CustomSlot.cpp"
#include "ui_Test.h"



int main(int argc, char *argv[])
{
QApplication app(argc, argv);

CustomSlot widget;
widget.show();
return app.exec();
}



-------------------------------------------------------------------------------------------
First CustomSlot.cpp 's code :



#include"CustomSlot.h"
#include "ui_Test.h"

#include "ui_Form2.h"
#include"CustomSlot2.cpp"






CustomSlot::CustomSlot(QWidget *parent): QWidget(parent)
{
ui.setupUi(this);
}


void CustomSlot:: OpenNxtWindow()
{

CustomSlot2 widget;
widget.show();

}

-----------------------------------------------------------------
Second CustomSlot.cpp 's code :



#include"CustomSlot2.h"

#include "ui_Form2.h"



CustomSlot2::CustomSlot2(QWidget *parent): QWidget(parent)
{
formTwo.setupUi(this);
}




Thanks.

jpn
11th June 2008, 13:02
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":


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:


void CustomSlot:: OpenNxtWindow()
{
CustomSlot2 widget(this);
widget.exec();
}

Fatla
12th June 2008, 17:24
As usual , Jbn , Your answeris accurate .

So, I'll need a header + cpp files to each form I've .

Thanks .

Fatla
13th June 2008, 19:43
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 :



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 :


#include"CustomSlot2.h"

#include "ui_Form2.h"

CustomSlot2::CustomSlot2(QWidget *parent): QWidget(parent)
{
formTwo.setupUi(this);

}

void CustomSlot2::PrintTxt
{
formTwo.txtData->setText("Alooooooo") ;

}


Thanks

jpn
13th June 2008, 19:48
Did you remember to add required Q_OBJECT macro to CustomSlot2 class? Did you remember to declare CustomSlot2::PrintTxt() as slot?

Fatla
13th June 2008, 20:30
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

jpn
13th June 2008, 20:39
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.


Thanks JPN
You're welcome. :)