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:
Code:
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
Code:
typedef QPair<MyBitArray, MyBitArray> MyBitArrayPair;
Q_DECLARE_METATYPE(MyBitArrayPair);
and
Code:
class MyBitArrayPair
{
public:
MyBitArrayPair() {}
MyBitArray first;
MyBitArray second;
};
Q_DECLARE_METATYPE(MyBitArrayPair);
The compiler does not complain anymore about
Code:
MyBitArrayPair pair = value.value<MyBitArrayPair>();
but still complains about
Code:
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
Re: Model/View: Use own class with QVariant
Quote:
Originally Posted by
No-Nonsense
but still complains about
Code:
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:
Code:
case Qt
::EditRole: return QVariant::fromValue(MyBitArrayPair
(x.
data(), x.
mask()));
Re: Model/View: Use own class with QVariant
Quote:
Originally Posted by
jpn
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:
Code:
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
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?
Re: Model/View: Use own class with QVariant
Quote:
Originally Posted by
wysota
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
Re: Model/View: Use own class with QVariant
Quote:
Originally Posted by
No-Nonsense
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.
Quote:
Does it cost more to use custom types with QVariant?
A little, maybe...