How to propagate from one class to another
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
Code:
#include "screen.h" /* #in# Variables*/
#include "PB.h"
#include "View.h"
#include <qapplication.h>
int main( int argc, char **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
Re: How to propagate from one class to another
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().
Code:
#include "screen.h" /* #in# Variables*/
#include "PB.h"
#include "View.h"
#include <qapplication.h>
int main( int argc, char **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();
}
Re: How to propagate from one class to another
Quote:
Originally Posted by high_flyer
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().
Code:
#include "screen.h" /* #in# Variables*/
#include "PB.h"
#include "View.h"
#include <qapplication.h>
int main( int argc, char **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
Re: How to propagate from one class to another
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:
Code:
#include "screen.h" /* #in# Variables*/
#include "PB.h"
#include "View.h"
#include <qapplication.h>
int main( int argc, char **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() ;
Re: How to propagate from one class to another
Quote:
Originally Posted by high_flyer
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:
Code:
#include "screen.h" /* #in# Variables*/
#include "PB.h"
#include "View.h"
#include <qapplication.h>
int main( int argc, char **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
Re: How to propagate from one class to another
Quote:
i am making use of keyPressEvent() function to handle the keyEvents.
Why??
Quote:
But it is not displaying any window further. Only the first window.
Hard to comment with out the code in question.
Re: How to propagate from one class to another
Quote:
Originally Posted by mahe2310
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.ph...esigner_2forms
Re: How to propagate from one class to another
Quote:
Originally Posted by jacek
that works :) ...
Thank you Jacek
Re: How to propagate from one class to another
Quote:
Originally Posted by mahe2310
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
Re: How to propagate from one class to another
What you mean calls ? create...show..
give your code
Re: How to propagate from one class to another
Quote:
Originally Posted by zlatko
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();
Re: How to propagate from one class to another
your code make me confused :confused:
why are yo use static?
Re: How to propagate from one class to another
Quote:
Originally Posted by zlatko
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...
Re: How to propagate from one class to another
Quote:
Originally Posted by zlatko
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.
Re: How to propagate from one class to another
will hide and show of the widget can play the role if w/o static?
Mahe2310
Re: How to propagate from one class to another
Quote:
Originally Posted by mahe2310
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?