Results 1 to 20 of 23

Thread: Problems accessing static member variable from static member function

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problems accessing static member variable from static member function

    Quote Originally Posted by spirit View Post
    this looks ok, but when you define QComboBox like static class member you didn't set a parent. so when you start application you will see this combobox like separate widget. so, you must reparent combobox in this case. anyway, I suggest you refuse this approach.
    no, my code is working fine. I have a separate combo box member variable which is actually displayed on the GUI. The static variable is just used as i require to pass the address of the variable to a different class through a static function. Since a static member function can only access static member variables, so i had to use an extra variable...

    in the class constructor, i just assign the normal combo box pointer to the static combo box variable...in the GUI, the normal combo box is displayed.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems accessing static member variable from static member function

    I guess it works fine, because you use layouts.

  3. #3
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problems accessing static member variable from static member function

    yes i am using layouts but as i said the combo box being shown on the GUI is a normal member variable.
    The static variable just stores the same address as the normal variable...so it's just a copy...

    Thanks for your help!

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems accessing static member variable from static member function

    in the class constructor, i just assign the normal combo box pointer to the static combo box variable....
    this can lead to memory leak, because when you init static variable you create a new comboboex and then (according to your words) you reset variable with a new object, but don't destroy old.

  5. #5
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problems accessing static member variable from static member function

    i am not allocating new memory for the static combo box, i am just assigning the same address to the static variable, so there is no extra memory allocation. Here's my code:

    Qt Code:
    1. sm_pTestCombo = m_pTestCombo;
    To copy to clipboard, switch view to plain text mode 

    here, sm_pTestCombo is the static member variable and m_pTestCombo is the normal member variable.

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems accessing static member variable from static member function

    what about this
    Qt Code:
    1. QComboBox *Test::m_comboBox = new QComboBox();
    To copy to clipboard, switch view to plain text mode 
    ?

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems accessing static member variable from static member function

    if you use code like this
    Qt Code:
    1. QComboBox *Test::m_comboBox = 0;
    To copy to clipboard, switch view to plain text mode 
    then ok.

  8. #8
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problems accessing static member variable from static member function

    i think that code was posted by you. I am not allocating any new memory

  9. #9
    Join Date
    Feb 2008
    Posts
    51
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems accessing static member variable from static member function

    Hi,

    You can try a different solution.
    Let's see it is easy and safe also.

    class GetComboInstance
    {
    public:
    static GetComboInstance* createInstance();
    updateCombo();
    private:
    QComboBox *m_pComboBox;
    static GetComboInstance *m_pInstance;
    };

    In Cpp file:

    static GetComboInstance *GetComboInstance::m_pInstance = NULL;
    static GetComboInstance* GetComboInstance::createInstance()
    {
    if(m_pInstance == NULL)
    {
    m_pInstance = new GetComboInstance();
    }
    return m_pInstance;
    }

    now you can call updateCombo method on your instance of GetComboInstance class from any class. It is better and safe way.

  10. #10
    Join Date
    Feb 2008
    Posts
    51
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems accessing static member variable from static member function

    Please let me know what do you think
    Last edited by Sandip; 8th October 2008 at 13:47. Reason: updated contents

  11. #11
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: Problems accessing static member variable from static member function

    I think that when you describe the singleton pattern, you should do it right, e.g. make constructors protected or private. ;-)

    http://www.inquiry.com/techtips/cpp_.../10min0200.asp

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 14:22
  2. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 12:57
  3. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 17:33
  4. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  5. I got two problems when I used static compiled library of QT4
    By qintm in forum Installation and Deployment
    Replies: 8
    Last Post: 20th April 2006, 08:52

Tags for this Thread

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.