[Interest] [SPAM] Is it safe to call qRegisterMetaType() before main()?

Thiago Macieira thiago.macieira at intel.com
Tue Jul 23 03:53:39 CEST 2019


On Monday, 22 July 2019 16:15:05 PDT Hamish Moffatt wrote:
> On 22/7/19 10:56 pm, Nikos Chantziaras wrote:
> > In order to avoid calling qRegisterMetaType() in main() (with the
> > possibility of forgetting to do so,) I instead call it before the
> > program enters main(), in the implementation file of whoever is the
> > owner of the type. There's several ways to do this, I just use a lambda:
> > 
> > namespace {
> > char unused = [] {
> >     qRegisterMetaType<MyClass::MyEnum>();
> >     return 0;
> > }();
> > }
> > 
> > It "seems to work fine," but is this safe though?

Actually, yes, so long as you're not using static builds. The metatype system 
depends on only the base containers (QVector, QByteArray), so this should 
work.

Still, it's ugly. For classes, I recommend putting the registration in the 
constructor. An enum is a different story.

> Why not put them in a static function in each file, and use
> Q_COREAPP_STARTUP_FUNCTION to have Qt call it for you at the right time?

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel System Software Products






More information about the Interest mailing list