[Development] Storing is-a QObject fact with metatypes

Stephen Kelly stephen.kelly at kdab.com
Fri Dec 2 10:57:07 CET 2011


Hi,

There is a lot of work going into the Qt metatype system at the moment, and I 
would like to put my oar in too.

I would like to store the fact of whether a type inherits QObject with the 
qmetatype information.

This would allow things like this:

QVariant v = QVariant::fromValue(new QAction); // Any QObject subclass

// Somewhere else:

if (v.canConvert<QObject*>() {
    QObject *obj = v.value<QObject*>();
    // ...
}

// Now use the properties on obj, or connect to its signals and slots.


Implementing this just needs a SFINAE template, and isn't unduly complex.

The problem is that it introduces a slight source incompatibility. The SFINAE 
template requires that the full type be declared instead of just forward 
declared. That is, things like this, which are possible with Qt4 would not 
work anymore:

class MyClass;
Q_DECLARE_METATYPE(MyClass*)

void foo(MyClass *aFoo)
{
  QVariant v = QVariant::fromValue(aFoo);
}

void choo(const QVariant &aChoo)
{
  MyClass *c = aChoo.value<MyClass*>();
}

// MyClass still just forward declared!

Similarly, qRegisterMetaType<MyClass*>() would require MyClass to be fully 
defined, whereas currently it does not. Being able to use these APIs when the 
tpr is not fully defined makes sense inside QtCore where types in QtGui need 
to be forward declared for example, but I don't think there's a great need to 
allow forward declared types outside of that case.devl

To add a migration path I would add QVariant::fromForwardDeclared<T>() and 
qRegisterForwardDeclaredMetaType<T*>() (along with helper template methods) 
which don't use the SFINAE and therefore behave in Qt4 compatibility and 
Q_FORWARD_DECLARE_METATYPE() macro which would not use template methods which 
rely on SFINAE.

It is very uncommon to use forward declared type like is currently allowed, so 
this would be optimizing for the more common case. I think this would be 
beneficial to QML related APIs.

What do you think of the proposed change?

Thanks,

-- 
Stephen Kelly <stephen at kdab.com> | Software Engineer
KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company
www.kdab.com || Germany +49-30-521325470 || Sweden (HQ) +46-563-540090
KDAB - Qt Experts - Platform-Independent Software Solutions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20111202/2128baf0/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3636 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/development/attachments/20111202/2128baf0/attachment.bin>


More information about the Development mailing list