Results 1 to 7 of 7

Thread: How to store and retrieve a simple int array int/from QVariant.-

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How to store and retrieve a simple int array int/from QVariant.-

    I need to use Qvariant to store and retrieve a simple int array. I can't....
    I have declare an int * my_array;

    If I use vv.value<int*>();
    I have qt_metatype_id' is not a member of 'QMetaTypeId<int*>'

    Any help ? Thanks

  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: How to store and retrieve a simple int array int/from QVariant.-

    "int*" is "address of an int value". Do you really want to store a pointer in the variant? If so, then remember that an address is also a number and you can treat it as one and tell QVariant to treat it as one as well. Not an elegant solution but it will work.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to store and retrieve a simple int array int/from QVariant.-

    But int * my_array and my_array = new int[5] .....
    I'd can use a fixed or dynamc int array, simply I was testing how to get that the QVariant gives me the value...

    I'm unable to store and retrieve a simple int[5] . into/from a Qvariant .....
    Can you help me?
    Thanks

  4. #4
    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: How to store and retrieve a simple int array int/from QVariant.-

    Quote Originally Posted by tonnot View Post
    But int * my_array and my_array = new int[5] .....
    I don't really want to teach you C++ right now. Get a book and read it.

    Can you help me?
    I already told you, treat the address as an integer, QVariant can accept those.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    tonnot (21st September 2011)

  6. #5
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to store and retrieve a simple int array int/from QVariant.-

    Ok, I'm going to try it

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to store and retrieve a simple int array int/from QVariant.-

    QVariant will accept a void*, so if you are willing to discard type safety:
    Qt Code:
    1. int *my_array = new int[5];
    2.  
    3. QVariant v1 = QVariant::fromValue( static_cast<void *>(my_array) );
    4. int *p1 = static_cast<int*>( v1.value<void*>() );
    5.  
    6. delete[] my_array;
    To copy to clipboard, switch view to plain text mode 
    or, you can declare int* to the meta-type system:
    Qt Code:
    1. Q_DECLARE_METATYPE(int*)
    2.  
    3. // and then
    4. QVariant v2 = QVariant::fromValue(my_array);
    5. int *p2 = v2.value<int*>();
    To copy to clipboard, switch view to plain text mode 

    Before you run off passing the pointer... you are passing a pointer to a block of memory (that might not exist) without passing the size of the block. How is the receiver supposed to know how many elements (ints in this case) can be safely accessed through that pointer?

    Have you considered using QVector<int>?
    Qt Code:
    1. Q_DECLARE_METATYPE(QVector<int>)
    2.  
    3. QVector<int> my_array(5);
    4. QVariant v3 = QVariant::fromValue(a);
    5. QVector<int> a = v3.value<QVector<int> >();
    6. qDebug() << a.size();
    To copy to clipboard, switch view to plain text mode 
    This will copy the data if you modify the data at the receiving end but for read-only purposes is quite efficient.

  8. The following user says thank you to ChrisW67 for this useful post:

    tonnot (22nd September 2011)

  9. #7
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to store and retrieve a simple int array int/from QVariant.-

    Thank you very much !!!
    A very vell explanation !

Similar Threads

  1. I want to save and retrive a QGraphicsScene
    By c_srikanth1984 in forum Qt Programming
    Replies: 16
    Last Post: 7th January 2014, 14:19
  2. Replies: 11
    Last Post: 21st May 2012, 05:02
  3. read a .txt file and store it in a double array
    By fatecasino in forum Newbie
    Replies: 5
    Last Post: 3rd December 2010, 20:13
  4. Replies: 1
    Last Post: 1st December 2010, 06:13
  5. How I can retrive information abt file.
    By safknw in forum General Programming
    Replies: 3
    Last Post: 2nd December 2006, 13:35

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.