Results 1 to 3 of 3

Thread: how to set custom type data to a QTreeWidgetItem?

  1. #1
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question how to set custom type data to a QTreeWidgetItem?

    Hi,

    How can I set the data of a QTreeWidgetItem with a custom type?

    I can, for example, declare my type with:

    Qt Code:
    1. struct CntyData {
    2. QString Code;
    3. QString Desc;
    4. QString Desc2;
    5. QString Desc3;
    6. QString Desc4;
    7. QString Status;
    8. };
    9. typedef CntyData TCntyData;
    10. Q_DECLARE_METATYPE(TCntyData);
    To copy to clipboard, switch view to plain text mode 

    Then assign the custom data with:

    Qt Code:
    1. TCntyData s;
    2. QVariant var;
    3. var.setValue(s);
    4.  
    5. item->setData(0,Qt::UserRole,var);
    To copy to clipboard, switch view to plain text mode 

    But how can I do this without creating QVariant var and just passing s to setData.

    I tried:

    Qt Code:
    1. TCntyData s;
    2. item->setData(0,Qt::UserRole,QVariant(s));
    To copy to clipboard, switch view to plain text mode 

    But it get the error: no matching function for call to 'QVariant::QVariant(TCntyData&)'

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to set custom type data to a QTreeWidgetItem?

    Read here.

    You nedd to write:
    • a public default constructor,
    • a public copy constructor
    • a public destructor.
    A camel can go 14 days without drink,
    I can't!!!

  3. The following user says thank you to mcosta for this useful post:

    qlands (4th July 2011)

  4. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to set custom type data to a QTreeWidgetItem?

    You nedd to write:
    a public default constructor,
    a public copy constructor
    a public destructor.
    Structures have all mentioned above provided by default by the compiler.
    Your problem is that there is no constructor in QVariant class that takes your custom structure as an argument, you can use for example the template methods for conversions:
    Qt Code:
    1. struct struct_test{
    2. QString str;
    3. int val;
    4. // ...
    5. };
    6. Q_DECLARE_METATYPE(struct_test);
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. struct_test s; s.str = "hello";
    11. QMap<int,QVariant> map;
    12. map[0] = qVariantFromValue<struct_test>(s);
    13. qDebug() << qVariantValue<struct_test>(map[0]).str;
    14. }
    To copy to clipboard, switch view to plain text mode 
    read here for details and examples : custom type example

Similar Threads

  1. Using QVariant with custom data type
    By QAlex in forum Qt Programming
    Replies: 3
    Last Post: 4th December 2009, 12:04
  2. Convert between a custom data type wrapped in a QVariant
    By darkadept in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2009, 09:07
  3. QTreeWidgetItem mime type
    By themolecule in forum Qt Programming
    Replies: 6
    Last Post: 27th November 2007, 12:55
  4. Replies: 4
    Last Post: 19th October 2007, 19:47
  5. Get the type of a QTreeWidgetItem
    By aLiNuSh in forum Newbie
    Replies: 2
    Last Post: 24th March 2007, 19:44

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.