Results 1 to 4 of 4

Thread: How to convert QVariant to my custom object?

  1. #1
    Join Date
    Apr 2007
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to convert QVariant to my custom object?

    Hi!
    I have custom model with list of my custom objects - QList<TBook *> bookList;
    Also I have delegate in this model. In delegate I have index.model()->data(index, Qt::DisplayRole) - this is QVariant. How can I convert this to TBook? Here is definition of my custom class:
    Qt Code:
    1. class TBook
    2. {
    3. public:
    4. TBook(QString, QString, uint, QString, qreal);
    5. TBook(){};
    6. ~TBook();
    7. void setData(QString, QString, uint, QString, qreal);
    8. QString getTitle();
    9. QString getAuthor();
    10. uint getGenre();
    11. QString getPath();
    12. qreal getProgress();
    13. static QStringList getAllGenres();
    14. void setTitle(QString);
    15.  
    16. private:
    17. QString title;
    18. QString author;
    19. uint genre;
    20. QString path;
    21. qreal progress;
    22. };
    23. Q_DECLARE_METATYPE(TBook);
    To copy to clipboard, switch view to plain text mode 

    Please, help me

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to convert QVariant to my custom object?

    You can't really do that, not in a safe way, due to limitations of C++ unions -> limitations of QVariant.

    The most elegant way would be to create user roles for every relevant getter of TBook. For example, for TBook::getTitle you create the BookTitleRole, and so on.
    You should use these roles when requesting data from the model.

    Regards

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to convert QVariant to my custom object?

    Qt Code:
    1. TBook book = qvariant_cast<TBook>(variant);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  4. #4
    Join Date
    Apr 2007
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to convert QVariant to my custom object?

    Quote Originally Posted by jpn View Post
    Qt Code:
    1. TBook book = qvariant_cast<TBook>(variant);
    To copy to clipboard, switch view to plain text mode 
    Yes! It's working! Greate thanks!

Similar Threads

  1. QVariant types support for custom properties
    By Dusdan in forum Qt Tools
    Replies: 9
    Last Post: 11th January 2006, 09:55

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.