Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: static function/object accessibility

  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 static function/object accessibility

    I'm trying to set up a static function and QLabel so that non-Qt parts of my application can call this function and change the setNum() value of the QLabel. I've done it like so:

    Qt Code:
    1. //--window.h
    2. class Window : public QWidget
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. Window();
    8. static void setText(int value);
    9. // etc...
    10.  
    11.  
    12. //--window.cpp
    13. #include <QtGui>
    14. #include "window.h"
    15.  
    16. static QLabel *vuXLocLabel=0;
    17.  
    18.  
    19. vuXLocLabel = new QLabel;
    20. vuXLocLabel->setText("hello");
    21.  
    22.  
    23.  
    24. void Window::setText(int value)
    25. {
    26. vuXLocLabel->setNum(value);
    27. //vuXLocLabel->setText("goodbye");
    28. qDebug(" HERE %d", value);
    29. }
    To copy to clipboard, switch view to plain text mode 

    The QLabel is displayed correctly initially: "hello". But when I call

    Window::setText(value);

    from the non-Qt part of the program, the value is sent and qDebug("HERE %d") is called (with correct value), but the QLabel isn't changed (using either setNum() or setText().)

    I also tried setting up the vuXLocLabel like so:
    Qt Code:
    1. //--window.h
    2.  
    3. static QLabel *vuXLocLabel;
    4.  
    5. //--window.cpp
    6.  
    7. QLabel *Window::vuXLocLabel=0;
    To copy to clipboard, switch view to plain text mode 

    but got the same result: code compiled, program didn't crash, but setNum/setText didn't work. Any insights greatly appreciated. (of course )

  2. #2
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: static function/object accessibility

    hi,

    you have here a global variable - which isn't a good thing - you could use the singleton-pattern like this:
    Qt Code:
    1. class Window : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Window();
    7. static void setText(int value);
    8. static Window* getInstance();
    9. // etc...
    10.  
    11. static Window* Window::getInstance()
    12. {
    13. static Window* win = 0;
    14. if (!win) {
    15. win = new Window();
    16. }
    17. return win;
    18. }
    19.  
    20. void Window::setText(int value)
    21. {
    22. Window* label = Window::getInstance();
    23. label->setNum(value);
    24. }
    To copy to clipboard, switch view to plain text mode 

    you can then access the window using:
    Qt Code:
    1. Window* win = Window::getInstance();
    2. win->show();
    To copy to clipboard, switch view to plain text mode 
    (in your main for example)

  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

    thanks Niko. I knew it was just a matter of time before someone would sling a Singleton solution at me. I tried incorporating your solution but get a
    setNum is not a member of 'Window'
    error. But let me step back a bit first:

    1. my "class Window" is my primary/main application window class with references to 100+ top-level widgets, etc. Confusing, perhaps, since it derives from QWidget and not QMainWindow; I may change this later. Anyway, given this fact, perhaps I need to go about this a different way, because...

    2. from the Wiki I see that a primary use of Singletons is "to encapsulate global variables", which is why I would want to use it here, of course. Here's where I'm confused: the Wiki example provides an example of a Singleton class, including things like a your getInstance() method, and a private constructor (which you left public). In *your* example, however: are you simply saying that I can create a class, Window, that is normal in most respects, yet has some singleton-pattern methods to enable access to a specific QLabel? If true, then I think this might be the best way for me to do it, since I will only have 5 or so QWidgets in Window that need to be accessed by non-Qt parts of the program. But if *not* true:

    3. Perhaps the best thing to do: create a separate Singleton class (like in the Wiki) and put all my global variables there... so that they can be accessed by both Window and the non-Qt parts of my application?

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

    Default Re: static function/object accessibility

    What do you mean by "non-Qt parts"? Can't you just call setText() on the object directly?

  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

    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.

  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

    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

  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

    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

  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

    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.

  9. #9
    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.

  10. #10
    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

  11. #11
    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

  12. #12
    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

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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?

  14. #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

    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

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

    vonCZ (19th August 2007)

  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

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

  17. #16
    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.

  18. #17
    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.

  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 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...

  20. #19
    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.

  21. #20
    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?

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.