Results 1 to 10 of 10

Thread: QScriptValue with simple typedef types

  1. #1
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QScriptValue with simple typedef types

    Hi there!

    I can't get a simple typedefed type (like typedef CDB_ID quint32;) to work correctly as a QScriptValue / QVariant.

    When I try to call a member function returning a CDB_ID from a qtscript, it always returns zero. Can anybody point me in the right direction?

    Qt Code:
    1. typedef int CDB_ID;
    2.  
    3. Q_DECLARE_METATYPE(CDB_ID)
    4.  
    5. class SClass : public QObject
    6. { Q_OBJECT
    7. public slots:
    8. int TestA() {return 5;}
    9. CDB_ID TestB() {return 8;}
    10. };
    11.  
    12. QScriptValue CDB_IDToScriptValue(QScriptEngine *engine,const CDB_ID &in)
    13. {
    14. return engine->newVariant(QVariant::fromValue(in));
    15. }
    16.  
    17. void CDB_IDFromScriptValue(const QScriptValue &object, CDB_ID &out)
    18. {
    19. out = object.toVariant().value<CDB_ID>();
    20. }
    21.  
    22. ..
    23.  
    24. QScriptEngine eng;
    25. qRegisterMetaType<CDB_ID>("CDB_ID");
    26. qScriptRegisterMetaType<CDB_ID>(&eng, CDB_IDToScriptValue, CDB_IDFromScriptValue);
    27.  
    28. SClass* s = new SClass();
    29. QScriptValue obj = eng.newQObject(s);
    30.  
    31. // Return value as CDB_ID .. always returns 0!
    32. fun = eng.evaluate("function() {return this.TestB();}");
    33. res = fun.call(obj,QScriptValueList());
    34. CDB_ID cdbid = 4;
    35. CDB_IDFromScriptValue(res,cdbid);
    36. qDebug() << "B: " << res.toString() << res.isVariant() << cdbid << " error: " << eng.uncaughtException().toString();
    To copy to clipboard, switch view to plain text mode 

    Attached you will find a working demo of my problem!

    Thx a lot!

    Johannes
    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: QScriptValue with simple typedef types

    Declaring a metatype for a typedef to int doesn't make much sense. CDB_ID is just an alias for int - you can mix the two freely so you can use CDB_ID just like if it was an int (because it IS an int). For example you can cast a variant to CDB_ID by using QVariant::toInt().
    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
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScriptValue with simple typedef types

    Hi there!

    It makes sense to me, not because of type-safety, but because I want to be able to globally change the type later if required. Change to 64bit or something like that.. Over the years, I found it to be good practice to define those dedicated types..

    A workaround could have been a #define, but then I found out, that moc ignores them.

    Of course there is the option to use a struct, but thats overkill here.

    For now.. I gave up and just used the built in types (quint32, ..) directly.. but that's not a nice solution..

    Thx for your reply anyway.

    Johannes

  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: QScriptValue with simple typedef types

    But you don't need any workaround. Ints just work, so use them. You can't declare a meta-object for a type that already has one and probably you can't declare a meta-object for something that is not a class.
    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
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScriptValue with simple typedef types

    Hi!

    You didn't get me. I want to use a typedef for certain variables like IDs or TypeIDs to be able to change the type programmwide at one spot. Thats the basic idea of typedefs btw.. But as you said, thats probably not possible for non class/struct-types in combination with qtscript.

    Cheers

    Joh

  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: QScriptValue with simple typedef types

    I understand what you want. I just said that if you typedef to int, you shouldn't try to register its metatype. At some point you are making the typedef. At the same spot you can decide whether to register the metatype or not.
    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
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScriptValue with simple typedef types

    Ok. But if I don't register the metatype, I can't call methods with that return type or parameter from within qscript. Thats my problem. And as I see it, even with a #define instead of a typedef there is no way convincing moc to allow me to do that. Or is there?

  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: QScriptValue with simple typedef types

    I've been experimenting but I can't get it to work. But it's not moc that is to be blamed but QtScript. So it should just be a matter of convincing QtScript that it knows the typedefed type. I think it should be possible to do it if you implement a prototype for CDB_ID in the script engine.
    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. The following user says thank you to wysota for this useful post:

    JohannesMunk (15th May 2009)

  10. #9
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: QScriptValue with simple typedef types

    Uha. And I thought.. the solution would be so obvious, that I posted in the Newbie forum

    In what way will a prototype help me here? If I understand the concept correctly, prototypes of a given value are used to resolve properties not found in the value itself. Which allows to implement some kind of inheritance. Do you suggest putting my CDB_ID-value into a property instead of the value directly? But each property is a QScriptValue again. How does that help me?

    Thx for your efforts!

    Johannes

  11. #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: QScriptValue with simple typedef types

    A prototype in QtScript is an equivalent of class in C++. So creating a prototype for "CDB_ID" is an equivalent of creating a class in C++ code. In other words it should make QtScript familiar with CDB_ID. Then outside the script environment you'll be able to operate on CDB_ID script values depending on the actual type(def) of CDB_ID.
    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.


  12. The following user says thank you to wysota for this useful post:

    JohannesMunk (15th May 2009)

Tags for this Thread

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.