Results 1 to 3 of 3

Thread: Link QVariant to global variable

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2007
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Link QVariant to global variable

    I have list of properties. Each property has a name and a QVariant value. I can access each property over the name(like QSettings).
    I want to connect some property to a concrete global variable. If the value of property is changed the global variable should also be changed.
    Here is some code to to show what i mean.

    Qt Code:
    1. // global variable
    2. double xy; // could be any other type (e.g int,QPoint,QString etc.)
    3.  
    4. class Prop
    5. {
    6. private:
    7. QString mName;
    8. QVariant mValue;
    9. QVariant /*or void?? */ *mPointer; // should point to xy
    10.  
    11. public:
    12. void SetPointer(/*Pointer to xy*/)
    13. {
    14. mPointer = /*Pointer to xy*/;
    15. }
    16.  
    17. void SetGlobalVariabel()
    18. {
    19. // should set xy indirect with mPointer
    20. */*Pointer to xy*/ = mValue.value();
    21. }
    22. };
    To copy to clipboard, switch view to plain text mode 

    Is this possible? If yes, how can i do this.
    There are other ways to change the global variable. But also this way should work.
    Last edited by wysota; 7th May 2007 at 10:44. Reason: missing [code] tags

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

    Default Re: Link QVariant to global variable

    If the global variable was also of type QVariant then this would be easy, but if you want to allow a generic type without QVariant, you have to provide some kind of "handlers" for different types and associate them with each global var. It'd be much easier if you used QVariant

  3. #3
    Join Date
    Feb 2007
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Link QVariant to global variable

    Now i have solved the problem with a handler:

    Qt Code:
    1. // global variable
    2. double xy; // could be any other type (e.g int,QPoint,QString etc.)
    3.  
    4. class Prop
    5. {
    6. private:
    7. QString mName;
    8. QVariant mValue;
    9. void *mPointer; // should point to xy
    10.  
    11. public:
    12. void SetPointer(void *pointer)
    13. {
    14. mPointer = pointer;
    15. }
    16.  
    17. void SetGlobalVariabel() // should set xy indirect with mPointer
    18. {
    19. if (!mPointer) return;
    20.  
    21. switch (mValue.type())
    22. {
    23. case (QVariant::Double): {
    24. *(static_cast<double*>(mPointer)) = mValue.value<double>();
    25. break;
    26. }
    27. case (QVariant::Int): {
    28. *(static_cast<int*>(mPointer)) = mValue.value<int>();
    29. break;
    30. }
    31. default:
    32. ;
    33. }
    34. }
    35. };
    To copy to clipboard, switch view to plain text mode 
    SetPointer(&xy) links the QVariant variable to the global variable.
    SetGlobalVariable() sets the global variable.

    This is not exactly what i wanted but since i only need some types of QVariant it is the fastest way to solve the problem.

    A more common solution should be possible if i would know the address(pointer) where QVariant stores the value of the variable.

    But at the moment the problem is solved for me.

    Thanks for the help.

Similar Threads

  1. global variable
    By Shuchi Agrawal in forum General Programming
    Replies: 10
    Last Post: 15th February 2007, 04:19

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.