Results 1 to 6 of 6

Thread: Problem regaridng Type casting QVariant

  1. #1
    Join Date
    Aug 2008
    Posts
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Problem regaridng Type casting QVariant

    Hi everybody ,
    I got problem regarding typecasting . I have one structure called Disrict and I want to set the object of district as data to Qtreewidgetitem.

    Qt Code:
    1. QTreeWidgetItem *itemdist = new QTreeWidgetItem();
    2. District *dist;
    3. itemdist->setData(1, Qt::ToolTipRole, dist);
    4. QVariant item = itemdist->data(1, Qt::ToolTipRole);
    5. district *temp = static_cast<district*>(temp1);
    To copy to clipboard, switch view to plain text mode 

    But in line 5 it is showing error as

    Qt Code:
    1. error C2440: 'static_cast' : cannot convert from 'QVariant' to 'district *'
    To copy to clipboard, switch view to plain text mode 

    I tried by using reinterpret_cast ,but it is showing same error . so how can I cast the variant item to district type. The main thing I want to do is assing district object to Qtreewidgetitem object and I want to get back the district object using Qtreewidgetitem object.
    So please suggest me to solve this problem.

    Regards,
    Sudheer.

  2. #2
    Join Date
    Nov 2007
    Posts
    17
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem regaridng Type casting QVariant

    Try using QVariant::setValue(..) for example:

    // set
    QWidget* widget = ui.MyWidget->window();

    QVariant var;
    var.setValue(widget);
    listWidgetItem->setData(Qt::UserRole, var);

    // get
    QVariant var = item->data(Qt::UserRole);
    QWidget* widget= var.value<QWidget*>();


    I'm not completely sure, but so far I know you only can use this for classes inherited by QObject...

    Montuno

  3. #3
    Join Date
    Aug 2008
    Posts
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem regaridng Type casting QVariant

    Hi thanks for your kind reply. But here I am using a structure district which was not derived from any class.
    So what I have to do to solve my problem .
    Please help me to solve this problem.

    regards,
    sudheer.

  4. #4
    Join Date
    Nov 2007
    Posts
    17
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem regaridng Type casting QVariant

    Quote Originally Posted by sudheer168 View Post
    But here I am using a structure district which was not derived from any class.
    Anyway, did you try my suggestion?

  5. #5
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem regaridng Type casting QVariant

    Your structure, class, or pointer need not be derived from any other class. You can use QVariant to hold any kind of object. First you need to use the Q_DECLARE_METATYPE macro in a header file:
    Qt Code:
    1. Q_DECLARE_METATYPE(District *)
    To copy to clipboard, switch view to plain text mode 

    Then you can use the following to set the value in a QVariant instance:
    Qt Code:
    1. District *d = ...
    2. qVariantFromValue(d)
    To copy to clipboard, switch view to plain text mode 

    Then you can get the value from the QVariant instance:
    Qt Code:
    1. QVariant item = itemdist->data(1, Qt::ToolTipRole);
    2. District *d = item.value<District *>();
    To copy to clipboard, switch view to plain text mode 

    Also - look at the description of the QVariant class in the documentation - it should show you how to deal with custom structures, etc.

  6. #6
    Join Date
    Aug 2008
    Posts
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem regaridng Type casting QVariant

    Hi ,
    Thanks for all your replies. I got the solution.

    Regards,
    Sudheer.

Similar Threads

  1. Problem with type casting?
    By Jyoti.verma in forum Qt Programming
    Replies: 2
    Last Post: 25th August 2009, 05:24
  2. Compile 4.4.0
    By LordQt in forum Installation and Deployment
    Replies: 18
    Last Post: 29th May 2008, 13:43
  3. dummy question(Error)
    By Masih in forum Qt Programming
    Replies: 12
    Last Post: 19th July 2007, 23:38
  4. Problem at time compilation in traslation of language
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2007, 14:18
  5. Replies: 3
    Last Post: 15th April 2007, 19:16

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.