Results 1 to 2 of 2

Thread: Q_DECLARE_METATYPE and qVariantCanConvert

  1. #1
    Join Date
    May 2007
    Posts
    106
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Q_DECLARE_METATYPE and qVariantCanConvert

    Hi,

    I have the following class in a static lib:

    Qt Code:
    1. class ITGraphic : public QPixmap
    2. {
    3. ....
    4. ....
    5. };
    6.  
    7. Q_DECLARE_METATYPE(ITGraphic);
    To copy to clipboard, switch view to plain text mode 

    Thereafter I am returning ITGraphic object in another static library from model class in data function:

    Qt Code:
    1. QVariant ITPictureList::data(const QModelIndex &index, int role) const
    2. {
    3. if (!index.isValid())
    4. return QVariant();
    5.  
    6. if (role == Qt::DecorationRole)
    7. {
    8. ITGraphic *graphic = GetBitmap(index.row());
    9. if(graphic != NULL)
    10. {
    11. QPixmap tmpPixmap = graphic->scaled(iconSize,Qt::KeepAspectRatio);
    12. return QIcon(tmpPixmap);
    13. }
    14. else
    15. return QVariant();
    16. }
    17. else if (role == Qt::UserRole)
    18. {
    19. return *(GetBitmap(index.row()));
    20. }
    21.  
    22. return QVariant();
    23. }
    To copy to clipboard, switch view to plain text mode 

    I am trying to get this object like this:

    QVariant value = picList->data(index,Qt::UserRole);

    Qt Code:
    1. res = qVariantCanConvert<ITGraphic>(value);
    2. ITGraphic pGraphic = qvariant_cast<ITGraphic>(value);
    To copy to clipboard, switch view to plain text mode 

    qVariantCanConvert returns false here and I am not able to get the proper object here. But if I try the following, it works:

    Qt Code:
    1. QPixmap pGraphic = qVariantValue<QPixmap>(picList->data(index, Qt::UserRole));
    To copy to clipboard, switch view to plain text mode 


    Can someone help me, how to get the ITGraphic object here?

    Regards,
    Manoj
    Last edited by wysota; 31st March 2008 at 09:59. Reason: reformatted to look better

  2. #2
    Join Date
    May 2007
    Posts
    106
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Cool Re: Q_DECLARE_METATYPE and qVariantCanConvert

    I finally found the solution to this myself. There are two possible solutions to this:

    (1) Implementing a copy constructor for ITGraphic class and then it should start work.

    (2) I did it other way round and changed the data function as follows:
    Qt Code:
    1. QVariant ITPictureList::data(const QModelIndex &index, int role) const
    2. {
    3. if (!index.isValid())
    4. return QVariant();
    5.  
    6. if (role == Qt::DecorationRole)
    7. {
    8. ITGraphic *graphic = GetBitmap(index.row());
    9. if(graphic != NULL)
    10. {
    11. QPixmap tmpPixmap = graphic->scaled(iconSize,Qt::KeepAspectRatio);
    12. return QIcon(tmpPixmap);
    13. }
    14. else
    15. return QVariant();
    16. }
    17. else if(role == Qt::DisplayRole)
    18. return GetFileName(index.row());
    19. else if (role == Qt::UserRole)
    20. {
    21. ITGraphic *graphic = GetBitmap(index.row());
    22. qVariantSetValue(v,graphic);
    23. return v;
    24. }
    25. else if (role == Qt::ToolTipRole)
    26. return GetFilePathName(index.row());
    27.  
    28. return QVariant();
    29. }
    To copy to clipboard, switch view to plain text mode 

    Then getting the data like this:

    Qt Code:
    1. ITGraphic *pGraphic = qVariantValue<ITGraphic*>(picList->data(index,Qt::UserRole));
    To copy to clipboard, switch view to plain text mode 

    It works!! :-)

    Regards,
    Manoj
    Last edited by jpn; 1st April 2008 at 06:21. Reason: changed [qtclass] to [code] tags

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.