PDA

View Full Version : Model/View: Use own class with QVariant



No-Nonsense
18th December 2006, 09:58
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:

Q_DECLARE_METATYPE(MyBitArray);

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

typedef QPair<MyBitArray, MyBitArray> MyBitArrayPair;
Q_DECLARE_METATYPE(MyBitArrayPair);
and

class MyBitArrayPair
{
public:
MyBitArrayPair() {}
MyBitArray first;
MyBitArray second;
};
Q_DECLARE_METATYPE(MyBitArrayPair);

The compiler does not complain anymore about

MyBitArrayPair pair = value.value<MyBitArrayPair>();
but still complains about

case Qt::EditRole: return MyBitArrayPair(x.data(), x.mask());
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

jpn
18th December 2006, 10:05
but still complains about

case Qt::EditRole: return MyBitArrayPair(x.data(), x.mask());
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:

case Qt::EditRole: return QVariant::fromValue(MyBitArrayPair(x.data(), x.mask()));

No-Nonsense
18th December 2006, 10:12
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:

case Qt::EditRole: return QVariant::fromValue(MyBitArrayPair(x.data(), x.mask()));

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

wysota
18th December 2006, 12:40
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?


QVariantList list << QByteArray(...) << QByteArray(...);
QVariant var = QVariant(list);

No-Nonsense
18th December 2006, 14:42
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

wysota
18th December 2006, 17:21
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...