PDA

View Full Version : Q_DECLARE_METATYPE() questions.



hickscorp
29th June 2010, 15:40
Hello :)

i have a question related to QVariant's user types. As i may have already said in a previous post, i'm implementing a derived class from "QItemEditorFactory" so it can handle my own types, and also for some built-in types (QRect/F, QPoint/F, etc).

In my factory, i use a switch to compare the given type (in createEditor(QVariant::Type, QWidget *parent)) to types built-into QVariant (QRect/F, QPoint/F).

But i don't understand how i am supposed to compare the given argument (Type) to know wether it's one of my own types because it's always UserType in the case of a specific user type item (But no detail on what user type)... i'm a bit lost into the QVariant typing thingy.

As far as i understand (And since i'm calling myself createEditor on the default installed item editor factory, i may want to pass my very specific custom type (UserType+1, UserType+2), or something?

Thanks,
Pierre.

hickscorp
29th June 2010, 16:04
Just some precisions.... For now, to allow my editor factory to work, i'm doing like this:
QItemEditorFactory const *f = QItemEditorFactory::defaultFactory();
_widget = f->createEditor((QVariant::Type)_param.value.userType (), this);
QByteArray vpName = f->valuePropertyName((QVariant::Type)_param.value.use rType());

Then, in my QItemEditorFactory subclass:
QWidget* CustomItemEditorFactory::createEditor (QVariant::Type type, QWidget* parent) const {
QWidget *ed;
// Custom boolean editor.
if (type==QVariant::Bool)
ed = new WgtBoolEditor(parent);
// Custom QPoint editor.
else if (type==QVariant::Point)
ed = new WgtPointEditor(false, INT_MIN, INT_MAX, parent);
// Custom QPointF editor.
else if (type==QVariant::PointF)
ed = new WgtPointEditor(true, LONG_MIN, LONG_MAX, parent);

// Custom RelativeRect editor.
else if (type==qMetaTypeId<REA::RelativeRect>())
ed = new WgtRelativeRectEditor(false, INT_MIN, INT_MAX, parent);
// Custom RelativeRectF editor.
else if (type==qMetaTypeId<REA::RelativeRectF>())
ed = new WgtRelativeRectEditor(true, INT_MIN, INT_MAX, parent);

else
ed = _standardFactory->createEditor(type, parent);
return ed;
}

QByteArray CustomItemEditorFactory::valuePropertyName (QVariant::Type type) const {
if (type==QVariant::Bool || type==QVariant::Point || type==QVariant::PointF)
return "value";
else
return _standardFactory->valuePropertyName(type);
}

Is there any better way to do that (Without casting some ints into QVariant::Type)? Actually, this works pretty good... But i'm not sure if it's the only way to achieve correctly what i'm trying to do... Without being able to get my custom types detailed as QVariant::Type from my custom factory, i really don't see the point of registering custom types.

Thanks,
Pierre.

And for the records, here is my custom types declaration:
#ifndef REATypes_h
#define REATypes_h

#include <QRect>
#include <QRectF>

namespace REA {

// Unsigned integers list.
typedef QList<unsigned int> UIntList;
// Signed integers list.
typedef QList<int> IntList;
// Color to unsigned int hash.
typedef QHash<QRgb,unsigned int> ColorToUIntHash;

// Relative rect class.
class RelativeRect : public QRect {};
// Relative float rect class.
class RelativeRectF : public QRectF {};

};

// Declare meta-types.
Q_DECLARE_METATYPE(REA::UIntList);
Q_DECLARE_METATYPE(REA::IntList);
Q_DECLARE_METATYPE(REA::ColorToUIntHash);
Q_DECLARE_METATYPE(REA::RelativeRect);
Q_DECLARE_METATYPE(REA::RelativeRectF);

#endif