Results 1 to 4 of 4

Thread: QList<double> => QVariant => QList<double>

  1. #1
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QList<double> => QVariant => QList<double>

    Hi!

    I'm trying to store my QList to QVariant and than convert it back to QList.

    This is minimal example of what I'm trying to do. Real application would be more complex, but on same basis.

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QVariant>
    3. #include <QList>
    4. #include <iostream>
    5.  
    6. Q_DECLARE_METATYPE(QList<double>)
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QCoreApplication a(argc, argv);
    11.  
    12. qRegisterMetaType<QList<double> >("QList<double>");
    13.  
    14. QList<double> x1;
    15. for(int i=0; i<30; i++)
    16. x1.append((double)i);
    17.  
    18. x2.fromValue<QList<double> >(x1);
    19.  
    20. QList<double> x3 = x2.value<QList<double> >();
    21. for(int i=0; i<x3.size(); i++)
    22. std::cout << x3[i] << "\n";
    23.  
    24. std::cout << "***";
    25.  
    26. return a.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 

    Above code compiles without any warnings but running the code does not produce expected results.

    Can somebody tell me what am I doing wrong?
    Thanx!

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QList<double> => QVariant => QList<double>

    Does this look simpler
    Qt Code:
    1. QList<QVariant> x1;
    2. for(int i=0; i<30; i++)
    3. x1.append((double)i);
    4.  
    5. QVariant x2 = x1;
    6.  
    7. QList<QVariant> x3 = x2.toList();
    8. for(int i=0; i<x3.size(); i++)
    9. std::cout << x3.at(i).toDouble() << "\n";
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following 2 users say thank you to Santosh Reddy for this useful post:

    Cupidvogel (22nd March 2015), stefan (12th October 2013)

  4. #3
    Join Date
    May 2008
    Location
    Rijeka, Croatia
    Posts
    85
    Thanks
    10
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList<double> => QVariant => QList<double>

    Yes, this could solve my issue!
    Thank you!

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QList<double> => QVariant => QList<double>

    You problem is here:

    Quote Originally Posted by stefan View Post
    Qt Code:
    1. x2.fromValue<QList<double> >(x1);
    To copy to clipboard, switch view to plain text mode 
    QVariant::fromValue() returns a QVariant. It is a static method, it does not (cannot) change x2.

    Qt Code:
    1. QVariant x2 = QVariant::fromValue(x1);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. sorting QList<double>
    By timmu in forum Qt Programming
    Replies: 7
    Last Post: 24th August 2012, 17:35
  2. Replies: 2
    Last Post: 23rd February 2012, 02:11
  3. Program not finding an existing double in a QList<double>
    By aarelovich in forum Qt Programming
    Replies: 2
    Last Post: 9th May 2011, 21:59
  4. Replies: 4
    Last Post: 20th August 2010, 14:54
  5. Wrong converting QVariant to double
    By estanisgeyer in forum Newbie
    Replies: 4
    Last Post: 15th July 2009, 23:19

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.