What is missing in my code? Why
Code:
qRegisterMetaType<CAppScreen>("CAppScreen")
does not help resolving this JS code?
Code:
var appScreen1 = new AppScreen();
user.ui.display(appScreen1);
Based on this site http://doc.qt.nokia.com/4.6-snapshot...CLARE_METATYPE
Note that if you intend to use the type in queued signal and slot connections or in QObject's property system, you also have to call qRegisterMetaType() since the names are resolved at runtime.
, using qRegisterMetaType() should resolve the problem.
What is missing in my code?
Code:
...
m_pEngine = new QScriptEngine(this);
qScriptRegisterMetaType<CAppScreen>(m_pEngine, CScriptEnvironment::AppScreenToScriptValue, CScriptEnvironment::AppScreenFromScriptValue);
//To use the type T in QVariant, using Q_DECLARE_METATYPE() is sufficient. To use the type T in queued signal and slot connections, qRegisterMetaType<T>() must be called before the first connection is established.
qRegisterMetaType<CAppScreen>("CAppScreen");
...
m_pUserUi = new CUserUI(m_pEngine);
m_userUiValue = m_pEngine->newQObject(m_pUserUi);
m_userValue.setProperty("ui", m_userUiValue); // "ui" is a property of "user"
m_appScreenValue = m_pEngine->newQObject(&m_appScreen, QScriptEngine::QtOwnership, QScriptEngine::ExcludeSuperClassContents);
// get existing appScreen object for appScreen Q_PROPERTY
QScriptValue CScriptEnvironment::appScreen()
{
return m_appScreenValue;
}
// constructor function called for JS "new AppScreen"
QScriptValue AppScreenConstructor(QScriptContext *context, QScriptEngine *pEngine)
{
QObject* pObject = new CAppScreen();
return pEngine->newQObject(pObject, QScriptEngine::ScriptOwnership);
}
QScriptValue ctor = m_pEngine->newFunction(AppScreenConstructor);
QScriptValue metaObject = m_pEngine->newQMetaObject(&CAppScreen::staticMetaObject, ctor);
QScriptValue self = m_pEngine->newQObject(this, QScriptEngine::QtOwnership, QScriptEngine::ExcludeSuperClassContents);
self.setProperty("AppScreen", metaObject);
class CAppScreen : public QObject, public QScriptClass
{
Q_OBJECT
public:
CAppScreen();
CAppScreen(const CAppScreen&); // copy constructor
CAppScreen & operator=(const CAppScreen &rhs);
virtual ~CAppScreen();
};
Q_DECLARE_METATYPE(CAppScreen)
Q_DECLARE_METATYPE(CAppScreen*)
class CUserUI : public QObject, public QScriptClass
{
public slots:
void display(CAppScreen& appScreen);
}