Results 1 to 6 of 6

Thread: Model/View: Use own class with QVariant

  1. #1
    Join Date
    Nov 2006
    Posts
    35
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Model/View: Use own class with QVariant

    The Model/View framework uses QVariant to pass data between the Model and the View. This works fine for Qt's own data types, but I want to pass my own data type to the view (the delegate knows how to handle my custom data).

    I have subclassed QBitArray and registering MyBitArray using the following code. It just works fine:
    Qt Code:
    1. Q_DECLARE_METATYPE(MyBitArray);
    To copy to clipboard, switch view to plain text mode 

    Now I want to pass two of these custom BitArrays without using two columns (ModelIndeces), as they get edited together (data and mask), so I tried
    Qt Code:
    1. typedef QPair<MyBitArray, MyBitArray> MyBitArrayPair;
    2. Q_DECLARE_METATYPE(MyBitArrayPair);
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. class MyBitArrayPair
    2. {
    3. public:
    4. MyBitArrayPair() {}
    5. MyBitArray first;
    6. MyBitArray second;
    7. };
    8. Q_DECLARE_METATYPE(MyBitArrayPair);
    To copy to clipboard, switch view to plain text mode 

    The compiler does not complain anymore about
    Qt Code:
    1. MyBitArrayPair pair = value.value<MyBitArrayPair>();
    To copy to clipboard, switch view to plain text mode 
    but still complains about
    Qt Code:
    1. case Qt::EditRole: return MyBitArrayPair(x.data(), x.mask());
    To copy to clipboard, switch view to plain text mode 
    with the following error message:
    "error: conversion from 'MyBitArrayPair' to non-scalar type 'QVariant' requested"
    There is a constructor for MyBitArrayPair(first, second) that I omitted here.

    What am I doing wrong? How can I pass custom classes using QVariant?

    Thanks in advance,
    -Jens

  2. #2
    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: Model/View: Use own class with QVariant

    Quote Originally Posted by No-Nonsense View Post
    but still complains about
    Qt Code:
    1. case Qt::EditRole: return MyBitArrayPair(x.data(), x.mask());
    To copy to clipboard, switch view to plain text mode 
    with the following error message:
    "error: conversion from 'MyBitArrayPair' to non-scalar type 'QVariant' requested"
    I think the compiler is unable to convert MyBitArrayPair to a QVariant because QVariant has no correcponding constructor. How about if you modify it like this:
    Qt Code:
    1. case Qt::EditRole: return QVariant::fromValue(MyBitArrayPair(x.data(), x.mask()));
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    No-Nonsense (18th December 2006)

  4. #3
    Join Date
    Nov 2006
    Posts
    35
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: Model/View: Use own class with QVariant

    Quote Originally Posted by jpn View Post
    I think the compiler is unable to convert MyBitArrayPair to a QVariant because QVariant has no correcponding constructor. How about if you modify it like this:
    Qt Code:
    1. case Qt::EditRole: return QVariant::fromValue(MyBitArrayPair(x.data(), x.mask()));
    To copy to clipboard, switch view to plain text mode 
    Ah - thank you very much - this works! But why did it work with the custom BitArray subclass? Did it use QVariant(... QBitArray) to construct the variant?

    Thanks in advance,
    -Jens

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Model/View: Use own class with QVariant

    Maybe you could use QVariants after all?

    I don't know how exactly your bit array works, but maybe you can fit it into a QByteArray which is castable to QVariant and construct a list or map of such variants which is also castable to QVariant?

    Qt Code:
    1. QVariantList list << QByteArray(...) << QByteArray(...);
    2. QVariant var = QVariant(list);
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Nov 2006
    Posts
    35
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Model/View: Use own class with QVariant

    Quote Originally Posted by wysota View Post
    Maybe you could use QVariants after all?

    I don't know how exactly your bit array works, but maybe you can fit it into a QByteArray which is castable to QVariant and construct a list or map of such variants which is also castable to QVariant?
    Why would I want to do this? Does it cost more to use custom types with QVariant?

    Actually I have a list of a custom class that has these two bit arrays and some integers (later maybe IdObjects) and a map of values that can be accessed by a key (QString, later some IdObject). For this datastructure I created a table model to edit the contents execpt the map. Passing the custom bit array directly to the editor widget (delegate) seems the easyest way to me. The map will be edited by an editor widget that is created from an XML file as I do not know the map key names and value types at compile time.

    -Jens

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Model/View: Use own class with QVariant

    Quote Originally Posted by No-Nonsense View Post
    Why would I want to do this?
    Because it would work out of the box. But it's just a suggestion, you don't have to use it.

    Does it cost more to use custom types with QVariant?
    A little, maybe...

  8. The following user says thank you to wysota for this useful post:

    No-Nonsense (20th December 2006)

Similar Threads

  1. Crash caused by QVariant (mis)use
    By mclark in forum Newbie
    Replies: 2
    Last Post: 31st October 2006, 15:05
  2. How can i prevent class redefinition problem ?
    By ankurjain in forum General Programming
    Replies: 5
    Last Post: 26th May 2006, 02:35
  3. Replies: 2
    Last Post: 4th May 2006, 19:17
  4. How to propagate from one class to another
    By mahe2310 in forum Qt Programming
    Replies: 15
    Last Post: 20th March 2006, 01:27
  5. Replies: 5
    Last Post: 15th March 2006, 07:33

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.