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

Thread: Problems accessing static member variable from static member function

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

    Question Problems accessing static member variable from static member function

    I have a class with a static variable of type QComboBox *. I have created a static member function from which i want to return this static variable. Here is the sample code:

    Header code
    Qt Code:
    1. Class TestPage : public QWidget
    2. {
    3. ---
    4. ---
    5. ---
    6. public:
    7. static QComboBox *getTestCombo ();
    8.  
    9. private:
    10. static QComboBox *sm_pTestCombo;
    11. };
    To copy to clipboard, switch view to plain text mode 

    CPP code
    Qt Code:
    1. QComboBox * TestPage::getTestCombo ()
    2. {
    3. return sm_pTestCombo;
    4. }
    To copy to clipboard, switch view to plain text mode 


    I am getting the following linking error:
    undefined reference to 'TestPage::sm_pTestCombo'

    Any ideas?

  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

    you must init static variable
    Qt Code:
    1. QComboBox *Test::m_comboBox = new QComboBox();
    To copy to clipboard, switch view to plain text mode 

    why do you need this method of storing/getting of QComboBox?
    Last edited by spirit; 7th October 2008 at 12:51. Reason: spelling error

  3. The following user says thank you to spirit for this useful post:

    montylee (7th October 2008)

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

    got it working...
    searched the internet and found that i have to define the static member variable again in the CPP file at file scope.

    So i have added this on top of the CPP file:
    Qt Code:
    1. QComboBox* TestPage::sm_pTestCombo;
    To copy to clipboard, switch view to plain text mode 

    But that brings me to a questions that is it worth it?
    Now the static variable can be accessed by anyone using the scope resolution operator. Same thing can be done by simply using a global variable in the CPP file, so just take a global variable in the CPP file and be happy. Why should i use the static variable then?
    Last edited by montylee; 7th October 2008 at 13:05.

  5. #4
    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
    you must init static variable
    Qt Code:
    1. QComboBox *Test::m_comboBox = new QComboBox();
    To copy to clipboard, switch view to plain text mode 

    why do you need this method of storing/getting of QComboBox?
    i already got my solution... i have posted it...

    I need to get the combo box pointer as i need to add items to it from a different class.

    If i use a normal member function to return the combo box pointer it won't work as as i have to first create an instance of this class in the other class to invoke the member function. As soon as i create an instance, the member combo box variable is allocated a new memory and i am not able to get the original combo box pointer.

    So , i have to call it directly without using an object, so i have to use either statiic or a global variable...

    pls read my earlier post and suggest if i should go for global or static member variable.

  6. #5
    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

    this variable is private, you can't get access from another class.

  7. #6
    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 have made it public and my code is working. But i am not sure whether to use static member variable or use a global variable...
    both approaches r working...

    pls suggest which one is better.

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

    I suggest you to add method in you class which will add item to a combobox and refuse using of global pointer to the combobox or using static variable.

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

    Quote Originally Posted by spirit View Post
    I suggest you to add method in you class which will add item to a combobox and refuse using of global pointer to the combobox or using static variable.
    The problem is:

    Suppose the combo box is part of class A:
    Qt Code:
    1. class A {
    2. private:
    3. QComboBox *m_pTestCombo;
    4.  
    5. public:
    6. void updateCombo ();
    7. }
    To copy to clipboard, switch view to plain text mode 

    Now, suppose i want to update the combo box from a different class B. For that, i have to call updateCombo () function of class A from class B.
    For calling the updateCombo () function of class A, i have to first create an object of class A in class B. For e.g.

    Qt Code:
    1. void B::someFunction ()
    2. {
    3. A obj;
    4. obj.updateCombo;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Now, as soon as i create a new object of class A in class B, calling updateCombo function will have no effect on the GUI because when i create a new object of class A, new memory is allocated to m_pTestCombo variable. So, even if the code to add items is executed this way, i am unable to see the updated combo box in the GUI as the GUI is still showing the original combo box.

    So, basically i have to get the same instance of the combo box which is being displayed on the GUI. Getting the existing instance of the pointer is only possible if i use a static member variable and call the updateCombo () function without creating a new object.
    I hope i have explained my problem well...

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

    you must provide interface in a class which keep combobox to setting/getting information, but don't allow to use members of this class to another classes. this is bad idea.

  11. #10
    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 you case, I suggest you add method to a class, which updates combobox, which will return QStringList with new items, and in a class, which keeps combox, method which will set new items (QStringList).

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

    ok, i got a bit confused with my own code. Here's what i am doing now:

    Header code:
    Qt Code:
    1. Class TestPage : public QWidget
    2. {
    3. ---
    4. ---
    5. ---
    6. public:
    7. static QComboBox *getTestCombo ();
    8.  
    9. private:
    10. static QComboBox *sm_pTestCombo;
    11. };
    To copy to clipboard, switch view to plain text mode 

    CPP code
    Qt Code:
    1. QComboBox* TestPage::sm_pTestCombo;
    2. QComboBox * TestPage::getTestCombo ()
    3. {
    4. return sm_pTestCombo;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Now, sm_pTestCombo is a private variable so it can't be accessed from the other class directly.

    From the other class, i am calling getTestCombo() to get the combo box pointer.

    Qt Code:
    1. QComboBox *testCombo = TestPage::getTestComboBox ();
    To copy to clipboard, switch view to plain text mode 


    Does this approach look ok? The member variable is now private and other classes have to use the public member function to get the variable's address.

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

    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.

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

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

  16. #15
    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!

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

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

  19. #18
    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 
    ?

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

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

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