Results 1 to 2 of 2

Thread: Using enums in QtScript

  1. #1
    Join Date
    Aug 2007
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Using enums in QtScript

    Hi everybody,

    I want to make a QObject scriptable using QtScript which is available in Qt 4.3. The QObject has a slot which takes an enum as an argument. My problem is that I cannot invoke this slot using QtScript. Everytime I try to do so an exception is thrown which says "unknown type 'EnumName'". You can find an example below:

    Qt Code:
    1. // ##################
    2. // header file Test.h
    3. // ##################
    4. #ifndef TEST_H
    5. #define TEST_H
    6.  
    7. #include <QObject>
    8. #include <QDebug>
    9.  
    10. enum MyEnum
    11. {
    12. FirstValue,
    13. SecondValue
    14. };
    15.  
    16.  
    17. class Test : public QObject
    18. {
    19. Q_OBJECT
    20.  
    21. public:
    22. Test(QObject *parent=0);
    23. ~Test();
    24. public slots:
    25. void testSlot( MyEnum arg ) { qDebug() << "Received arg" << arg; }
    26.  
    27. private:
    28. };
    29.  
    30. #endif // TEST_H
    31.  
    32.  
    33. // ##################
    34. // Test.cpp contains empty constructor and destructor only
    35. // ##################
    36. #include "Test.h"
    37.  
    38. Test::Test(QObject *parent)
    39. : QObject(parent)
    40. {
    41.  
    42. }
    43.  
    44. Test::~Test()
    45. {
    46.  
    47. }
    48.  
    49. // ##################
    50. // main.cpp
    51. // ##################
    52.  
    53. #include <QtScript>
    54. #include <QObject>
    55. #include <QtCore/QCoreApplication>
    56.  
    57. #include "Test.h"
    58.  
    59.  
    60. int main(int argc, char *argv[])
    61. {
    62. QCoreApplication a(argc, argv);
    63. QScriptEngine engine;
    64.  
    65. // Create Test object
    66. Test t;
    67. // Create ScriptValue from Test object
    68. QScriptValue testObject = engine.newQObject(&t);
    69. // Make testObject globally available to engine
    70. engine.globalObject().setProperty("Test", testObject);
    71.  
    72. engine.evaluate("try { Test.testSlot(0);} catch(e) {print(e);}");
    73.  
    74. return a.exec();
    75. }
    To copy to clipboard, switch view to plain text mode 

    This will produce the output "TypeError: cannot call Test::testSlot(): unknown type `MyEnum'". Changing the arguments of testSlot to ints and casting inside the slot will work but I do not like this solution very much.

    My Question is: how can I make MyEnum available to the scripting engine? I tried to use Q_ENUMS, Q_DECLARE_METATYPE, Q_SCRIPT_DECLARE_QMETAOBJECT, qScriptRegisterMetaType(), etc. but I had no success so far. I guess there must be a solution but I can't find it so any help is greatly appreciated.

    Thanks in advance,
    Orphelic

  2. #2
    Join Date
    Nov 2007
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using enums in QtScript

    Hi Orphelic,

    I had a similar problem.

    I have patched MOC to generate meta-call-info for all public functions (not just slots as it is today), so I can call them from qscript, and I have set up some constructors for the classes I need to construct from qscript.

    The problem arised when I tried to call the function
    Qt Code:
    1. QDrag::exec( Qt::DropActions supportedActions = Qt::MoveAction)
    To copy to clipboard, switch view to plain text mode 
    from QScript.

    As it turns out, the Qt::enums are not made available to the script engine. Digging a bit further, i found out that the fix was simply calling
    Qt Code:
    1. qRegisterMetaType<Qt::DropAction>("Qt::DropAction");
    To copy to clipboard, switch view to plain text mode 
    first, so that Qt::DropAction was made known to the meta type system, and thus also to the script engine.

    Hope this helps out,

    Best regards
    orz

Similar Threads

  1. QtScript Q_ENUM problem
    By oc2k1 in forum Qt Programming
    Replies: 0
    Last Post: 14th May 2007, 16:07
  2. how to use enums
    By soul_rebel in forum General Programming
    Replies: 3
    Last Post: 23rd March 2006, 21:49

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.