×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    13

    How to pass a user defined object as a function argument?

    Hi ,

    Please let me know how to pass a user defined object (i.e., getMyObject1) to the slot function (i.e., public slot: void foo(CMyClass1& myObject1)?

    Q_DECLARE_METATYPE(CMyClass1) does not compile. I suppose because it is Q_OBJECT.

    My java script is:

    myObject2.foo(myObject1);

    So far, I created:

    Q_PROPERTY(QScriptValue myObject1 READ getMyObject1)
    Q_PROPERTY(QScriptValue myObject2 READ getMyObject2)

    CMyClass1& m_myObject1;
    QScriptValue m_myObjectValue1;

    CMyClass2& m_myObject2;
    QScriptValue m_myObjectValue2;

    m_myObjectValue1 = m_pEngine->newQObject(&m_myObject1, QScriptEngine::QtOwnership, QScriptEngine::ExcludeSuperClassContents);
    m_myObjectValue1 = m_pEngine->newQObject(&m_myObject1, QScriptEngine::QtOwnership, QScriptEngine::ExcludeSuperClassContents);


    QScriptValue CScriptEnvironment:: getMyObject1()
    {
    return myObjectValue1;
    }
    QScriptValue CScriptEnvironment:: getMyObject2()
    {
    return myObjectValue2;
    }


    class CMyClass1 : public QObject, public QScriptClass
    {
    Q_OBJECT
    public:

    }

    class CMyClass2 : public QObject, public QScriptClass
    {
    Q_OBJECT
    public:

    public slots:
    void foo(CMyClass1& myObject1);
    }


    Thanks,
    Dusan Mudric.

  2. #2
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: How to pass a user defined object as a function argument?

    You don't need Q_DECLARE_METATYPE to pass a user-defined object in a slot.

    You DO need to jump through several hoops to have &-qualified instance variables or to use assignment on non-pointer objects.

    My guess is that you know Java and are trying to construct Qt code from a few examples, true? If so you'd be well-advised to spend some time studying first a good C++ textbook and then a guide to Qt.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    13

    Re: How to pass a user defined object as a function argument?

    I have C++ experiance only.

    True, I have only a few examples of Qt code and I am trying to use them to provide functionality that allows passing user-defined object in a slot.

    Can you tell me the steps to make it working?

  4. #4
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: How to pass a user defined object as a function argument?

    Quote Originally Posted by Dusan Mudric View Post
    Can you tell me the steps to make it working?
    Well, if you told us what you're trying to do and what error is occurring where we might be able to help you.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    13

    Re: How to pass a user defined object as a function argument?

    I am exposing my object (i.e., myObject1) that is used to set a number of attributes (i.e., var myObject1 = new MyClass1(a,b,c,d)). Then I need to pass that object as an argument via a function of another object (i.e., myObject2.foo(myObject1)).

    So far I was using some examples I found for QT where Q_DECLARE_METATYPE (MyClass1) is used. However, when I do:

    var myObject1 = new MyClass1();
    myObject2.foo(myObject1);


    while debugging I found that MyClass1::foo(void); is called, not MyClass1::foo(MyClass1 &).

    Here is what I am doing:

    var appScreen = new AppScreen();
    ui.display(appScreen);

    I see ui.display() being called.

    ======== Real Code ============

    class CScreen
    {
    public:
    CScreen();
    };
    // This macro makes the type CScreen known to QMetaType
    Q_DECLARE_METATYPE(CScreen)

    namespace UI
    {

    class CAppScreen : public QObject, public CScreen
    {
    Q_OBJECT

    public:
    CAppScreen();
    //CAppScreen(const CScreen& in);
    virtual ~CAppScreen();
    static CAppScreen& Instance(void);
    static void Destroy(void);

    public slots:
    protected:

    protected:
    static CAppScreen* m_pSelf;
    private:
    };

    class CUI : public QObject, public QscriptClass
    {
    Q_OBJECT

    public:
    public slots:
    void display(CScreen& appScreen);
    void display(CAppScreen& appScreen);
    void display(void);

    protected:

    protected:
    private:
    };


    QScriptValue AppScreenConstructor(QScriptContext *context, QScriptEngine *pEngine)
    {
    CScreen screen;

    return pEngine->toScriptValue(screen);
    }

    UI::CAppScreen& m_appScreen;
    QScriptValue m_appScreenValue;

    CUI* m_pUi;
    QScriptValue m_UiValue;

    // Bind "user.ui" property to Qt script engine.
    m_pUi = new CUI(m_pEngine);
    // Signals and slots, properties and children of object are available as properties of the created QScriptValue = m_userValue
    m_UiValue = m_pEngine->newQObject(m_pUi);
    m_userValue.setProperty("ui", m_UiValue);

    QScriptValue self = m_pEngine->newQObject(this, QScriptEngine::QtOwnership, QScriptEngine::ExcludeSuperClassContents);

    // create and register the appScreen prototype and constructor in the engine
    m_appScreenValue = m_pEngine->newQObject(&m_appScreen, QScriptEngine::QtOwnership, QScriptEngine::ExcludeSuperClassContents);
    int id = qMetaTypeId<CScreen>(); // Returns the meta type id of type CScreen at compile time. If the type was not declared with Q_DECLARE_METATYPE(), compilation will fail.
    m_pEngine->setDefaultPrototype(id, m_appScreenValue);
    QScriptValue screenCtor = m_pEngine->newFunction(AppScreenConstructor, m_appScreenValue);
    self.setProperty("AppScreen", screenCtor);

  6. #6
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: How to pass a user defined object as a function argument?

    Some hints:

    First, when you quote code, put it in CODE brackets so it formats well. (Go to Advanced editing and press the "#" icon.) It's very difficult to read code that's been mangled by the text formatter. (Hard enough before it's been mangled.)

    Second, delete all the Q_DECLARE_METATYPEs and the Q_PROPERTYs and the "slot" and most other Qt-specific stuff (other than, perhaps, the Q_OBJECT). I think it's confusing you and you don't need it until you're doing advanced stuff. Get a simple case working before you try the mess you've gotten yourself into. You need to understand what you're doing a bit more.

    You pass a user-defined object in a function call in Qt exactly like you do in C++. Qt **is** C++.

    Now, if you want to SIGNAL a SLOT rather than CALL a FUNCTION, you need to read up on signals and slots: http://doc.qt.nokia.com/4.6-snapshot...sandslots.html

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    13

    Re: How to pass a user defined object as a function argument?

    My understanding was that Qt supports JavaScript types and Qt defined meta types (http://doc.qt.nokia.com/4.6/qmetatype.html#Type-enum). A new type should be defined.

    Are you saying there is no need to define a new type? Just pass appScreen object in java script ui.display() function?

  8. #8
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: How to pass a user defined object as a function argument?

    I'm saying that that stuff has nothing to do with calling a function and passing a value. Crawl before you walk.

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    13

    Re: How to pass a user defined object as a function argument?

    Even that does not work properly. I created void CUserUI::display(int i) and called ui.display(6). It did call propper display(int) function but passed 0 value.

    I don't want to SIGNAL to SLOT. I want to call a slot function from java script file and pass an object that I defined. There should be a standard interface and easy to implement. So far examples at http://doc.qt.nokia.com/4.6//script-...rototypes.html , http://doc.qt.nokia.com/4.6/qscriptengine.html did not help.

    Anybody has the idea how to make it working?

  10. #10
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: How to pass a user defined object as a function argument?

    Are you doing the calls in C++ or JS? If it's C++ you're making some sort of very basic mistake. If it's JS you need to more clearly understand the call protocols between JS and C++.

  11. #11
    Registered User
    Join Date
    Jun 2010
    Posts
    13

    Re: How to pass a user defined object as a function argument?

    ui.display(appScreen); is JS call

    I need a working example showing how to register my object as Qt type, so C++ function display(CAppScreen&) gets invoked.

  12. #12
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: How to pass a user defined object as a function argument?

    Well, thanks for telling us that, in post #11.

    Look here for the root documentation on metatypes. Note in particular the requirement that your class have a public default constructor, a public copy constructor, and a public destructor. And check out QVariant here.

    Several script examples are here.

  13. #13
    Registered User
    Join Date
    Jun 2010
    Posts
    13

    Re: How to pass a user defined object as a function argument?

    I followed examples you sent. Created copy constructor and assignment operator for CAppScreen. Thanks.

    ui.display(appScreen) can not find CAppScreen data type.

    Here is the current code:
    qScriptRegisterMetaType<CAppScreen>(m_pEngine, CScriptEnvironment::AppScreenToScriptValue, CScriptEnvironment::AppScreenFromScriptValue);

    ctor = m_pEngine->newFunction(AppScreenConstructor);
    metaObject = m_pEngine->newQMetaObject(&CAppScreen::staticMetaObject, ctor);
    globalObject.setProperty("AppScreen", metaObject);

    QScriptValue AppScreenConstructor(QScriptContext *context, QScriptEngine *pEngine)
    {
    QObject* pObject = new CAppScreen();
    return pEngine->newQObject(pObject, QScriptEngine::ScriptOwnership);
    }

    QScriptValue CScriptEnvironment::AppScreenToScriptValue (QScriptEngine* pEngine, const CAppScreen& in)
    {
    QScriptValue data = pEngine->newVariant(qVariantFromValue(in));
    return pEngine->newObject(new CAppScreen(), data);
    }

    //////////////////////////////////////////////////////////////////////////

    void CScriptEnvironment::AppScreenFromScriptValue(const QScriptValue &object, CAppScreen& out)
    {
    out = *(CAppScreen*)(object.toQObject());
    }

  14. #14
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: How to pass a user defined object as a function argument?

    Please reread what I said about posting code.

  15. #15
    Registered User
    Join Date
    Jun 2010
    Posts
    13

    Re: How to pass a user defined object as a function argument?

    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);
    }

Similar Threads

  1. How to pass argument in console EXE function?
    By SymbianTH in forum Symbian
    Replies: 10
    Last Post: 2013-11-13, 08:28
  2. How to pass/read argument to/from an EXE?
    By danie78 in forum Symbian
    Replies: 6
    Last Post: 2009-03-17, 12:41
  3. Pass CConsoleBase * as a function argument
    By noahsark in forum Symbian
    Replies: 2
    Last Post: 2008-02-05, 10:33
  4. Pass TFixedArray as a Function Argument
    By symbianjunior in forum Symbian
    Replies: 1
    Last Post: 2007-11-02, 20:45
  5. How to pass HBufC as an argument to a function?
    By ashbhatia in forum Symbian User Interface
    Replies: 6
    Last Post: 2003-03-31, 10:32

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×