PDA

View Full Version : Problem using Q_INVOKABLE on private constructor



graeme
29th May 2010, 06:53
I have a factory class which I want to use to create all of my objects. This currently works using Q_INVOKABLE with the constructor and then with a QMetaObject of the class I first check the constructorCount() assuming this is greater than zero for the class I wish to instantiate I then use newInstance()

All of this works and I can create the objects that I want to. However I would like to restrict the creation of these object to just the factory method and so I tried to make the constructor private. As soon as I do that the constructorCount() becomes zero. I have tried adding friendship to the class but with no success. This is what I have tried, where Factory is my class.

friend class Factory;
friend class QObject;
friend class QMetaObject;
friend class QMetaMethod;


Is there another class that I should be using or is this just not possible? (Maybe there is a inner class?)

alexisdm
29th May 2010, 22:50
It seems you can't have an invokable private constructor (at least if you use the moc).

Look at the generated moc_youfile.cpp, you'll see that when the constructor is private the free static function YourClass_qt_static_metacall doesn't contain the constructor call anymore or doesn't exist if it was the only invokable static method or slot.