Results 1 to 20 of 22

Thread: static function/object accessibility

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: static function/object accessibility

    non-Qt parts: a Coin3d scenegraph. My current task: as the user rotates the model (thus moving the camera), I want to send the new camera X position to a QLabel's setNum() method. I haven't any trouble sending Qt instructions/parameters to the Coin3d class, but going the other way around--from Coin3d to Qt--is where I'm currently stuck.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: static function/object accessibility

    Just make the label visible outside the window class.
    Add a method getLabel in the window that returns the label. You can use it to call setText or connect signals to it.

    You don't need the label to be static. It will work with a singleton.
    Something like:
    Qt Code:
    1. Window::getInstance()->getLabel()->setText("x");
    To copy to clipboard, switch view to plain text mode 


    Regards

  3. #3
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: static function/object accessibility

    program compiles, but crashes when run. I've got:

    Qt Code:
    1. // window.h
    2. class Window : public QWidget {
    3.  
    4. Q_OBJECT
    5.  
    6. public:
    7. Window();
    8. static Window* getInstance();
    9. QLabel *getLabel();
    10. QLabel *myLabel;
    11.  
    12. // window.cpp
    13. myLabel = new QLabel;
    14. Window* Window::getInstance() {
    15. static Window *win=0;
    16. if(!win) {
    17. win = new Window();
    18. }
    19. return win;
    20. }
    21.  
    22. QLabel *Window::getLabel() {
    23. return myLabel;
    24. }
    25.  
    26. // model.h ...Coin3d scenegraph
    27. #include <Inventor/*/*.h> //many
    28. #include <QLabel> //was surprised I had to include this
    29. #include "window.h"
    30.  
    31. // model.cpp
    32.  
    33. Window::getInstance()->getLabel()->setText("x");
    34. // Window::getInstance(); //also crashes
    To copy to clipboard, switch view to plain text mode 

    based on the last commented line: looks like I'm not employing the Singleton correctly.

    Another question though: the call to update the QLabel value will happen ~25 times a second as the camera is moved. Is this still a good way to connect the camera X-value to the QLabel?
    Attached Images Attached Images

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: static function/object accessibility

    What do you do in the constructor of Window?
    It should work, although, you should add a static private Window membner, and take it out of get instance.

  5. #5
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: static function/object accessibility

    Quote Originally Posted by marcel View Post
    It should work, although, you should add a static private Window membner, and take it out of get instance.
    ok, did that.

    Quote Originally Posted by marcel View Post
    What do you do in the constructor of Window?
    almost everything. Instantiate many of my widget objects, call functions to intantiate others, do connects, etc...


    edit: NOTE my constructor is public.

    i should also add: the application hangs for about 3-4 seconds, then crashes.
    Last edited by vonCZ; 19th August 2007 at 11:15.

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: static function/object accessibility

    edit: NOTE my constructor is public.
    Does not really matter, but it is not singleton if the constructor is not private.
    If it is public it means that you can create other instances outside the class.

    I am almost sure that something happens in the constructor.

    Regards

  7. #7
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: static function/object accessibility

    Quote Originally Posted by marcel View Post
    Does not really matter, but it is not singleton if the constructor is not private.
    If it is public it means that you can create other instances outside the class.

    I am almost sure that something happens in the constructor.

    Regards
    well here's the code, when you or someone else has a moment.

    "public it means that you can create other instances outside the class"- perhaps that's my problem. I'm calling:

    Qt Code:
    1. Window::getInstance()->getLabel()->setText("x");
    To copy to clipboard, switch view to plain text mode 

    from another class, Model. Isn't this call, then, creating a 2nd instance of Window? ...thus the hang/crash? I'll try making the constructor private, also I suspect doing so will cause all sorts of other problems...
    Attached Files Attached Files

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: static function/object accessibility

    from another class, Model. Isn't this call, then, creating a 2nd instance of Window? ...thus the hang/crash? I'll try making the constructor private, also I suspect doing so will cause all sorts of other problems...
    It won't create another instance because getInstance tests if there already is an instance available. That is the whole point of singletons.

    Keeping the constructor public allows something like this from another class:
    Qt Code:
    1. Window *w = new Window();
    To copy to clipboard, switch view to plain text mode 
    therefore creating another instance, other than the static one.

    So you should make the constructor private to prevent it from being accessed from any other class than Window.

    well here's the code, when you or someone else has a moment.
    I can't compile it, bu I'll take a look.

    Regards

  9. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: static function/object accessibility

    Who initializes the members of Window::dafo?
    Qt Code:
    1. for(int a=0; a<dafo.get_compNUM_TOT(); a++)
    2. {
    3. mkLyBuLayout(a); // comp Number
    4. }
    To copy to clipboard, switch view to plain text mode 
    This code could cause the seg fault if get_compNUM_TOT returns an uninitialized int.

    Regards

  10. The following user says thank you to marcel for this useful post:

    vonCZ (19th August 2007)

  11. #10
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: static function/object accessibility

    Quote Originally Posted by marcel View Post
    Who initializes the members of Window::dafo?
    Qt Code:
    1. for(int a=0; a<dafo.get_compNUM_TOT(); a++)
    2. {
    3. mkLyBuLayout(a); // comp Number
    4. }
    To copy to clipboard, switch view to plain text mode 
    This code could cause the seg fault if get_compNUM_TOT returns an uninitialized int.

    Regards
    dafo's members are all static, initialized in dafo.cpp.

  12. #11
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: static function/object accessibility

    Well, it is pretty hard to see anything without debugging.
    I recommend doing so in the constructor.

    BTW: "EARTH Earth, the 3rd planet from the sun, is approximately four and a half billion years old.". I think it is older than that, but I'm sure this isn't causing the crash . The planet itself should be as old as the solar system.

    EDIT: Oh, I read only half a billion years , missed the "four".
    Anyway, I see you are on Windows, so debugging should be a lot easier. A step by step debugging should reveal the error.
    Did it work before, when you were not using a singleton?


    Regards
    Last edited by marcel; 19th August 2007 at 12:01.

  13. #12
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: static function/object accessibility

    Quote Originally Posted by marcel View Post
    EDIT: Oh, I read only half a billion years , missed the "four".
    though i live in CZ, I work for a geologic research institute in Texas... a state in which half the population believes the earth is only 6,000 years old. I've heard Jesus turned water into wine... I guess he turned rocks into oil, too.

  14. #13
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: static function/object accessibility

    Quote Originally Posted by marcel View Post
    So you should make the constructor private to prevent it from being accessed from any other class than Window.
    oops- i didn't see this at first! Anyway, you were right on: I made the constructor private and changed changed a couple other things accordingly, i.e.

    Qt Code:
    1. //-- main.cpp
    2.  
    3. // Window window; //changed to
    4. Window* window = Window::getInstance();
    To copy to clipboard, switch view to plain text mode 

    and it runs without crashing. For some reason

    Window::getInstance()->getLabel()->setText("x")

    still doesn't update the text, but i'll figure it out.

    Thanks to all for your help...

  15. #14
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: static function/object accessibility

    Quote Originally Posted by vonCZ View Post
    oops- i didn't see this at first! Anyway, you were right on: I made the constructor private and changed changed a couple other things accordingly, i.e.

    Qt Code:
    1. //-- main.cpp
    2.  
    3. // Window window; //changed to
    4. Window* window = Window::getInstance();
    To copy to clipboard, switch view to plain text mode 
    and it runs without crashing. For some reason

    Window::getInstance()->getLabel()->setText("x")

    still doesn't update the text, but i'll figure it out.

    Thanks to all for your help...
    This is is because you create 5 labels for each layer ( in mkZXag ). So, probably, the label you return is not the one which is visible.
    The label you return in Window is the 7th.
    Which layer do you have visible?

  16. #15
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: static function/object accessibility

    right again! ...this was just a test: the label "vuXLocLabel" will exist somewhere else--not on the zXag QStack or any other Stack--but I put it there just to test this out for convenience... by mistake (putting it on a stack).

    huh- this was the cause of my original error, before I got into the Singleton stuff, i bet. Nevertheless, I'm glad ya'll helped me thru it: I still don't understand fully why, but I gather from this and other threads global variables--apparently even the kind I had coded in my original post--are a bad idea. I'll have to go back and re-do dafo.h/dafo.cpp using this singleton approach, I guess.

    I'm still curious how this will work after I make the proper connections: for this test the QLabel is updated just once with a function call, but I'm going to created 3 QLabels that are updated by movement of the camera... ~25/sec. I'll be interested to see how making 3 getLabel()->setText("x/y/z") calls, 25/sec, works out.

  17. #16
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: static function/object accessibility

    I'm still curious how this will work after I make the proper connections: for this test the QLabel is updated just once with a function call, but I'm going to created 3 QLabels that are updated by movement of the camera... ~25/sec. I'll be interested to see how making 3 getLabel()->setText("x/y/z") calls, 25/sec, works out.
    I wouldn't worry about that. There will be 25 function calls + signals/slots overhead.
    There shouldn't be any problem with calling a function 25 times/second.

    Eventually you could skip some of those values since no one can really see 25 coordinates/second anyway.

    Regards

  18. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: static function/object accessibility

    Do you by any chance use multiple threads in your application?

  19. #18
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: static function/object accessibility

    Quote Originally Posted by wysota View Post
    Do you by any chance use multiple threads in your application?
    not intentionally ...i mean, no.

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 10:12
  2. Accessing to a static variable from the same class
    By xgoan in forum General Programming
    Replies: 6
    Last Post: 5th March 2007, 10:50

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
  •  
Qt is a trademark of The Qt Company.