Results 1 to 4 of 4

Thread: Get custom data from qstandarditem?

  1. #1
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Get custom data from qstandarditem?

    I'm saving a custom type of data (in fact instance of a custom data) in a QStandardItem this way:

    item.setData(QVariant(my_data))

    Now I'm trying to get the data back this way:

    data = item.data()

    But print type(data) says that data is QVariant.

    How can I get my data back? Thanks

  2. #2
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Get custom data from qstandarditem?

    This is probably Python specific. In C++, you can do:
    Qt Code:
    1. MyType variable = variant.value<MyType>();
    2.  
    3. // or use one of the built in to* functions.
    4. QString str = variant.toString();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Get custom data from qstandarditem?

    In python you don't have to worry about types of data,

    a = 1234 will make a to be an int, and
    b = "abcd" will make b to be a string.

    So, the first part of your example would be:

    variable = ..........

    The second part is a little bit more confusing :S. QVariant doesn't have any "value" method. Maybe I understood you wrong?

  4. #4
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Get custom data from qstandarditem?

    Once again, I made it by myself =)

    First, I was storing the data in a bad way. The default QtRole in setData method is QtEditRole, and I needed VisibleRole. And second, once having the QVariant object, I needed to call the toPyObject() method, and I got the instance ^^

    Thanks anyway for reading/trying to help. Really, qtcentre rocks!

Similar Threads

  1. QAbstractTableModel , custom data
    By akon in forum Newbie
    Replies: 0
    Last Post: 17th April 2009, 16:03
  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. Best way to display lots of data fast
    By New2QT in forum Newbie
    Replies: 4
    Last Post: 16th October 2008, 22:46
  4. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 16:26
  5. QVariant::toString and custom data types
    By Vladimir in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2007, 15:36

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.