Results 1 to 16 of 16

Thread: How to propagate from one class to another

  1. #1
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default 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
    Qt Code:
    1. #include "screen.h" /* #in# Variables*/
    2. #include "PB.h"
    3. #include "View.h"
    4.  
    5. #include <qapplication.h>
    6.  
    7. int main( int argc, char **argv )
    8. {
    9. QApplication app( argc, argv );
    10. while(true) {
    11. if(inView == true)
    12. {
    13. View window;
    14. window.setCaption( "View" );
    15. app.setMainWidget( &window );
    16. window.show();
    17. return app.exec();
    18. inView = false;
    19. }
    20. else if(inPB == true)
    21. {
    22. PB window;
    23. window.setCaption( "PB" );
    24. app.setMainWidget( &window );
    25. window.show();
    26. return app.exec();
    27. inPB = false;
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    But I cant able to return from the View Class.

    delete this; in View class creates Segmentation Fault.

    Plz help

    mahe2310

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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().
    Qt Code:
    1. #include "screen.h" /* #in# Variables*/
    2. #include "PB.h"
    3. #include "View.h"
    4.  
    5. #include <qapplication.h>
    6.  
    7. int main( int argc, char **argv )
    8. {
    9. QApplication app( argc, argv );
    10. //while(true) {
    11. if(inView == true)
    12. {
    13. View window;
    14. window.setCaption( "View" );
    15. app.setMainWidget( &window );
    16. window.show();
    17. //return app.exec();
    18. inView = false;
    19. }
    20. else if(inPB == true)
    21. {
    22. PB window;
    23. window.setCaption( "PB" );
    24. app.setMainWidget( &window );
    25. window.show();
    26. //return app.exec();
    27. inPB = false;
    28. }
    29. return app.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default 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().
    Qt Code:
    1. #include "screen.h" /* #in# Variables*/
    2. #include "PB.h"
    3. #include "View.h"
    4.  
    5. #include <qapplication.h>
    6.  
    7. int main( int argc, char **argv )
    8. {
    9. QApplication app( argc, argv );
    10. //while(true) {
    11. if(inView == true)
    12. {
    13. View window;
    14. window.setCaption( "View" );
    15. app.setMainWidget( &window );
    16. window.show();
    17. //return app.exec();
    18. inView = false;
    19. }
    20. else if(inPB == true)
    21. {
    22. PB window;
    23. window.setCaption( "PB" );
    24. app.setMainWidget( &window );
    25. window.show();
    26. //return app.exec();
    27. inPB = false;
    28. }
    29. return app.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. #include "screen.h" /* #in# Variables*/
    2. #include "PB.h"
    3. #include "View.h"
    4.  
    5. #include <qapplication.h>
    6.  
    7. int main( int argc, char **argv )
    8. {
    9. QApplication app( argc, argv );
    10. View window;
    11. window.setCaption( "View" );
    12. app.setMainWidget( &window );
    13. window.show();
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

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

  5. #5
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default 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:
    Qt Code:
    1. #include "screen.h" /* #in# Variables*/
    2. #include "PB.h"
    3. #include "View.h"
    4.  
    5. #include <qapplication.h>
    6.  
    7. int main( int argc, char **argv )
    8. {
    9. QApplication app( argc, argv );
    10. View window;
    11. window.setCaption( "View" );
    12. app.setMainWidget( &window );
    13. window.show();
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    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

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to propagate from one class to another

    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  8. #8
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to propagate from one class to another

    Quote Originally Posted by jacek
    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

    that works ...

    Thank you Jacek

  9. #9
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default 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

  10. #10
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: How to propagate from one class to another

    What you mean calls ? create...show..
    give your code
    a life without programming is like an empty bottle

  11. #11
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default 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();

  12. #12
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: How to propagate from one class to another

    your code make me confused
    why are yo use static?
    a life without programming is like an empty bottle

  13. #13
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to propagate from one class to another

    Quote Originally Posted by zlatko
    your code make me confused
    why are yo use static?

    thank you...

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

  14. #14
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to propagate from one class to another

    Quote Originally Posted by zlatko
    your code make me 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.
    Save yourself some pain. Learn C++ before learning Qt.

  15. #15
    Join Date
    Jan 2006
    Posts
    80
    Thanks
    1
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default 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

  16. #16
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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?
    Save yourself some pain. Learn C++ before learning Qt.

Similar Threads

  1. Replies: 3
    Last Post: 27th December 2008, 19:34
  2. class in the class
    By baray98 in forum General Programming
    Replies: 2
    Last Post: 23rd July 2008, 07:01
  3. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 07:57
  4. How to use Signal through direct connection
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th December 2007, 07:07
  5. Replies: 2
    Last Post: 4th May 2006, 19:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.