Results 1 to 20 of 23

Thread: Return QObject undefined

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Return QObject undefined

    If I pass an object to a QML and that object in turn grabs another object I error out with the following:

    "code example/main.js:4: TypeError: Result of expression 'failedObject' [undefined] is not an object."

    Now if I change line 16 in object1.h (and of course its matching function in object1.cpp) to QObject the script will convert it fine and I will not have any errors. The problem with this is I then have to qobject_cast when using the function internally in the c++ code. This casting internally just to get it to be recognized correctly in the script feels wrong to me.

    Since my class inherits QObject I was under the impression I should not have to use qRegisterMetaType. It appears to be registered correctly since it knows how to cast it in the script when it is passed as a QObject.

    I am confused on why I must use QObject and let the script auto cast it and I cannot reference my class directly if it is already registered. Also not sure if casting my class internally will have a performance hit or is just bad form?

    Short Question: Why cant I reference a return object using my specific class identifier in a script?

    I attached a very small code example that will show specifically what the problem is.
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Return QObject undefined

    My impression is you need to call qmlRegisterType for qml to be aware of your object type.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Return QObject undefined

    That will work for QML code but I am trying to get the script code to reconize it. I need something like qScriptRegisterMetaType but I do not have a QScriptEngine since the script is being handled by the QML.

    My guess is it might not be possible right now or I am looking at it wrong.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Return QObject undefined

    Could you prepare an example that actually does something? When I run your code I only get a blank window and no messages to the console.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Return QObject undefined

    Could you prepare an example that actually does something?
    Just click anywhere in the box and you will see the error message in the console.


    Now if I change line 16 in object1.h (and of course its matching function in object1.cpp) to QObject the script will convert it fine and I will not have any errors. The problem with this is I then have to qobject_cast when using the function internally in the c++ code. This casting internally just to get it to be recognized correctly in the script feels wrong to me.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Return QObject undefined

    It works (no error, object name is printed) if I use qmlRegisterType. Why haven't you tried it after I have given this solution to you?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Return QObject undefined

    I still get the error, can you please post your code changes.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Return QObject undefined

    I added this to main().
    Qt Code:
    1. qmlRegisterType<Object2>("com.test.object", 1, 0, "Object2");
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Return QObject undefined

    I error with:

    In file included from object1.h:6,
    from main.cpp:7:
    object2.h:21: error: explicit specialization in non-namespace scope ‘class Object2’
    object2.h:21: error: ‘struct QMetaTypeId<Object2*>’ redeclared with different access
    object2.h:21: error: explicit specialization in non-namespace scope ‘class Object2’
    object2.h:21: error: ‘struct QMetaTypeId<QDeclarativeListProperty<Object2> >’ redeclared with different access
    make: *** [main.o] Error 1

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Return QObject undefined

    I don't know what you did exactly to get this error but the line I gave you is the only change I made to the whole program (apart including headers for your classes, of course).
    Qt Code:
    1. #include "object1.h"
    2. #include "object2.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QDeclarativeView view;
    9. qmlRegisterType<Object2>("com.test.object", 1, 0, "Object2");
    10. view.setResizeMode(QDeclarativeView::SizeRootObjectToView);
    11.  
    12. Object1 *object1 = new Object1();
    13.  
    14. view.rootContext()->setContextProperty("myObject1",object1);
    15.  
    16. view.setSource(QUrl::fromLocalFile("main.qml"));
    17. view.show();
    18.  
    19. return app.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Return QObject undefined

    Thank you Wysota, the problem I was having was experimenting with other possibilities and I did not back that code out before attempting the qmlRegisterType.

    I have noticed that QML behaves differently when these objects are passed to the script as a signal. I have added to the previous code example that reproduces this.

    The update adds a singleShot QTimer that passed the object1 twice to the script. Once declared class and the second as a QObject.

    When passed as a class it is casted as a QVariant and I will not have access to any properties or slots.

    When passed as a QObject I will have access to properties but is also casted differently then when it is passed using the doSomething() function. (Clickign anywhere in the window)

    I could not find anyway to cast it from a QVariant in the script so I could access it properly. Also a little confused why it is casting the same object type differently.
    Attached Files Attached Files

Similar Threads

  1. undefined reference to `QUiLoader::QUiLoader(QObject*)'
    By ashukla in forum Qt Programming
    Replies: 7
    Last Post: 7th October 2011, 18:05
  2. Return value to QList
    By lyucs in forum Newbie
    Replies: 7
    Last Post: 16th October 2009, 02:31
  3. undefined reference to QObject
    By hcerny in forum Qt Programming
    Replies: 2
    Last Post: 2nd May 2008, 16:55
  4. Replies: 1
    Last Post: 31st October 2007, 14:14
  5. Replies: 1
    Last Post: 18th June 2006, 10:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.