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