PDA

View Full Version : How to propagate from one class to another



mahe2310
16th March 2006, 10:04
hi,

I am in a big confussion now.

My application main.cpp is calling a class View defined in the file View.cpp. Later in progress of the program, It calls another class PB in the file PB.cpp. But i don't know how to exit from the previous class and proceed to the new class.

I tried it as



#include "screen.h" /* #in# Variables*/
#include "PB.h"
#include "View.h"

#include <qapplication.h>

int main( int argc, char **argv )
{
QApplication app( argc, argv );
while(true) {
if(inView == true)
{
View window;
window.setCaption( "View" );
app.setMainWidget( &window );
window.show();
return app.exec();
inView = false;
}
else if(inPB == true)
{
PB window;
window.setCaption( "PB" );
app.setMainWidget( &window );
window.show();
return app.exec();
inPB = false;
}
}

But I cant able to return from the View Class.

delete this; in View class creates Segmentation Fault.

Plz help

mahe2310

high_flyer
16th March 2006, 10:42
Your code doesn't make much sense.
Why do you have the while loop?
Also, app.exec() is a blocking function.
Try the below code, see if its any better for you.
If you explain what it is you want to acheave, then maybe we can offer a better design for your main().



#include "screen.h" /* #in# Variables*/
#include "PB.h"
#include "View.h"

#include <qapplication.h>

int main( int argc, char **argv )
{
QApplication app( argc, argv );
//while(true) {
if(inView == true)
{
View window;
window.setCaption( "View" );
app.setMainWidget( &window );
window.show();
//return app.exec();
inView = false;
}
else if(inPB == true)
{
PB window;
window.setCaption( "PB" );
app.setMainWidget( &window );
window.show();
//return app.exec();
inPB = false;
}
return app.exec();
}

mahe2310
16th March 2006, 12:01
Your code doesn't make much sense.
Why do you have the while loop?
Also, app.exec() is a blocking function.
Try the below code, see if its any better for you.
If you explain what it is you want to acheave, then maybe we can offer a better design for your main().



#include "screen.h" /* #in# Variables*/
#include "PB.h"
#include "View.h"

#include <qapplication.h>

int main( int argc, char **argv )
{
QApplication app( argc, argv );
//while(true) {
if(inView == true)
{
View window;
window.setCaption( "View" );
app.setMainWidget( &window );
window.show();
//return app.exec();
inView = false;
}
else if(inPB == true)
{
PB window;
window.setCaption( "PB" );
app.setMainWidget( &window );
window.show();
//return app.exec();
inPB = false;
}
return app.exec();
}


i tried with the code supplied. But i noticed the same error.

I will explain my scenario.

View is a window that displays 4 buttons.
On Return Key Press, the window will be closed and respective windows will be displayed.

mahe2310

high_flyer
16th March 2006, 12:07
Then your design is totaly wrong.
You need only one main widget, and from it you can open other widgets/dialogs.
You main should look like:



#include "screen.h" /* #in# Variables*/
#include "PB.h"
#include "View.h"

#include <qapplication.h>

int main( int argc, char **argv )
{
QApplication app( argc, argv );
View window;
window.setCaption( "View" );
app.setMainWidget( &window );
window.show();
return app.exec();
}

And in your main widget, you should connect a button clicked() signal with a slot, and in that slot call the PB::show() ;

mahe2310
16th March 2006, 12:43
Then your design is totaly wrong.
You need only one main widget, and from it you can open other widgets/dialogs.
You main should look like:



#include "screen.h" /* #in# Variables*/
#include "PB.h"
#include "View.h"

#include <qapplication.h>

int main( int argc, char **argv )
{
QApplication app( argc, argv );
View window;
window.setCaption( "View" );
app.setMainWidget( &window );
window.show();
return app.exec();
}

And in your main widget, you should connect a button clicked() signal with a slot, and in that slot call the PB::show() ;


I used this method.

I created an object for the window and call the window using show()
I called it as an object bcoz i am making use of keyPressEvent() function to handle the keyEvents.

PB window;
window.show();

But it is not displaying any window further. Only the first window.

mahes2310

high_flyer
16th March 2006, 13:07
i am making use of keyPressEvent() function to handle the keyEvents.

Why??


But it is not displaying any window further. Only the first window.

Hard to comment with out the code in question.

jacek
16th March 2006, 13:16
PB window;
window.show();

But it is not displaying any window further. Only the first window.
Because QWidget::show() is not blocking and that window variable goes out of scope too quickly.

http://www.qtcentre.org/forum/faq.php?faq=qt_general_category#faq_qt_designer_2f orms

mahe2310
16th March 2006, 15:02
Because QWidget::show() is not blocking and that window variable goes out of scope too quickly.

http://www.qtcentre.org/forum/faq.php?faq=qt_general_category#faq_qt_designer_2f orms


that works :) ...

Thank you Jacek

mahe2310
16th March 2006, 15:23
that works :) ...

Thank you Jacek


I am currently facing a problem like...

1. Window Main is opened.
2. Window Main calls window Sub1 on a key event.
3. Window Sub1 calls Window Main back on a key event.
4. Window Main calls another window Sub2 on a key event.
5. Window Sub2 calls Window Main back on a key event.
6. Window Main calls the Window Sub1 again.


***** The 6th step isnt happening. ******

Is it bcoz the Window Sub1 is already open???
How can i close that???

Mahe2310

zlatko
16th March 2006, 15:42
What you mean calls ? create...show..
give your code

mahe2310
16th March 2006, 16:06
What you mean calls ? create...show..
give your code

By Calls i mean....

static View *window = new View(this);
window->show();
window->setActiveWindow();
window->raise();

zlatko
16th March 2006, 16:31
your code make me confused :confused:
why are yo use static?

mahe2310
16th March 2006, 16:37
your code make me confused :confused:
why are yo use static?


thank you...

It worked correctly w/o static...
I used it as per the path jacek provided...

Chicken Blood Machine
17th March 2006, 23:34
your code make me confused :confused:
why are yo use static?

Static means the object only gets created once, regardless how many times his slot gets called.

An alternative (and often safer) design is to use a member variable.

mahe2310
19th March 2006, 07:52
will hide and show of the widget can play the role if w/o static?

Mahe2310

Chicken Blood Machine
20th March 2006, 01:27
will hide and show of the widget can play the role if w/o static?

Mahe2310

I don't understand the question. Can you re-phrase it?