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);
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
typedef QPair<MyBitArray, MyBitArray> MyBitArrayPair;
Q_DECLARE_METATYPE(MyBitArrayPair);
typedef QPair<MyBitArray, MyBitArray> MyBitArrayPair;
Q_DECLARE_METATYPE(MyBitArrayPair);
To copy to clipboard, switch view to plain text mode
and
class MyBitArrayPair
{
public:
MyBitArrayPair() {}
MyBitArray first;
MyBitArray second;
};
Q_DECLARE_METATYPE(MyBitArrayPair);
class MyBitArrayPair
{
public:
MyBitArrayPair() {}
MyBitArray first;
MyBitArray second;
};
Q_DECLARE_METATYPE(MyBitArrayPair);
To copy to clipboard, switch view to plain text mode
The compiler does not complain anymore about
MyBitArrayPair pair = value.value<MyBitArrayPair>();
MyBitArrayPair pair = value.value<MyBitArrayPair>();
To copy to clipboard, switch view to plain text mode
but still complains about
case Qt::EditRole: return MyBitArrayPair(x.data(), x.mask());
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
Bookmarks