[Development] constexpr and qMetaTypeId<>()

achartier at fastmail.fm achartier at fastmail.fm
Thu Sep 12 18:38:16 CEST 2013


> Message: 1
> Date: Wed, 11 Sep 2013 19:12:26 -0700
> From: Thiago Macieira <thiago.macieira at intel.com>
> Subject: Re: [Development] constexpr and qMetaTypeId<>()
> To: development at qt-project.org
> Message-ID: <3962396.DaoebYs1tu at tjmaciei-mobl2>
> Content-Type: text/plain; charset="us-ascii"
> 
> On quarta-feira, 11 de setembro de 2013 14:29:44, achartier at fastmail.fm
> wrote:
> > Hello,
> > 
> > I'd like to use qMetaTypeId<>() in a constexpr expression. 
> 
> You can't. The types are registered at runtime.
> 
> > Any help is greatly appreciated.
> 
> -- 
> Thiago Macieira - thiago.macieira (AT) intel.com
>   Software Architect - Intel Open Source Technology Center

> Message: 5
> Date: Thu, 12 Sep 2013 09:47:26 +0200
> From: Stephen Kelly <stephen.kelly at kdab.com>
> Subject: Re: [Development] constexpr and qMetaTypeId<>()
> To: development at qt-project.org
> Message-ID: <6202252.jE7UbQaEne at hal>
> Content-Type: text/plain; charset="us-ascii"
> 
> On Wednesday, September 11, 2013 14:29:44 achartier at fastmail.fm wrote:
> > Hello,
> > 
> > I'd like to use qMetaTypeId<>() in a constexpr expression. I have my own
> > custom type declared as such:
> > 
> > class MyCustomClass : public QObject
> > {
> >      Q_OBJECT
> > 
> >      // ...
> > };
> > 
> > Q_DECLARE_METATYPE(MyCustomClass*)
> 
> Unless you need to stay Qt 4 compatible, you can remove this line.
> Pointers to 
> QObject derived types are automatically treated as metatypes if needed.
> 
> > 
> > I then try the following:
> > 
> > constexpr qint32 metaTypeId = qMetaTypeId<MyCustomClass*>();
> 
> This only works for built-in metatypes which have an id value defined in
> the 
> QMetaType::Type enum.
> 
> Thanks,
> 
> -- 
> Join us in October at Qt Developer Days 2013 - https://devdays.kdab.com
> 
> Stephen Kelly <stephen.kelly 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

Thank you Thiago and Stephen for the information. I am, however, still
confused by both of your responses.

Thiago - The qMetaTypeId documentation
(http://qt-project.org/doc/qt-5.1/qtcore/qmetatype.html#qMetaTypeId)
specifically states that the type registration using this function is
resolved at compile-time, not run-time.

Stephen - The qMetaTypeId documentation goes on to provide a code
snippet where a custom type (MyStruct) is registered, so it would seem
this should work for non-built-in types as well.

As such, the documentation leads one to believe that qMetaTypeId can be
used to register one's custom type at compile-time, as I tried to do in
the code snippet in my original query.

Is the documentation completely wrong or is a compile-time type
registration still possible in some way? I am very interested in this,
as the system I am working on would greatly benefit from this
functionality.

Thanks again,

Alfonso



More information about the Development mailing list