PDA

View Full Version : The execution get teminate while accessing variable declared in another class



sudheer168
23rd December 2008, 09:05
Hi ,I am using QT 4.4.0 .
I here attached a sample code where I got a problem. This application consists of two buttons named "Dialog1" and "Dialog2". If we click on "Dialog1" button, a dialog1 will open which consists of one button and If we click on "Dialog2" button, a dialog2 will open which consists of one button .In dialog2 if click I click the button named "Dialog2_Push" I need to get a pop up a message displaying the variable which I had declared in dialog1.h class. I wrote a piece of code for that but the execution stops when I click a button named "Dialog2_Push" which is on dialog2 class dialog.
So help me or suggest me to access the variable declared in one dialog in another dialog.
Please go through the attached file.

With regards,
Sudheer.

jpn
23rd December 2008, 09:18
The parent of dialog2 is not dialog1 but testpro so the following code crashes because "omans" is a null pointer:


void dialog2::on_pushButton_clicked()
{
dialog1 *omans = qobject_cast<dialog1 *>(parent());
int dat = omans->datacount;
QMessageBox::information(this,"",""+ QString::number(dat) +"");
}

sudheer168
23rd December 2008, 09:41
Hi JPN,
Thanks fro your kind reply.What you have said is right.But I dont know how to over come this problem,I mean I need to access the variable .
So please suggest me to solve this problem.

With Regards,
Sudheer.

jpn
23rd December 2008, 10:03
You have to change your design. In your current code there is no instance of dialog1 at all when you execute dialog2.

sudheer168
23rd December 2008, 10:57
Hi JPN,
thanks for your quick and kind reply.



You have to change your design. In your current code there is no instance of dialog1 at all when you execute dialog2.



I didn't get you .If you don't mind please modify the code what I have attached and reply back to me .

With regards and Thanks,
Sudheer .

jpn
23rd December 2008, 11:13
When "pushButton" is clicked

an instance of dialog1 is created
the event loop of that dialog1 is executed
the execution returns to testpro::on_pushButton_clicked() after the dialog1 event loop exits
dialog1 instance goes out of scope and is automatically destructed according to normal C++ rules

When "pushButton" is clicked, there is dialog1 but no dialog2.

And the same goes for "pushButton_2". You create an instance of dialog2 but at that point there is no dialog1.

sudheer168
25th December 2008, 05:58
Hi JPN ,
thanks for your kind reply.I understood what you have told .Thanks for your explanation ,But how can I over come this problem to implement the same concept.
So please help me to solve the problem .