Hello there!

I just ran into a nasty problem, which prevents my scriptcode from running correctly.

After importing the qt.core extension, qobjects (or descendants) will not implicitely convert to a string anymore, when inside a string concatenation:

Qt Code:
  1. "A"+obj;
  2. "A"+obj.toString();
To copy to clipboard, switch view to plain text mode 

The first line will produce a type error, while the second one still works.

I built a minimal code example showing the problem:

Qt Code:
  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3.  
  4. #include <QObject>
  5.  
  6. class Test : public QObject
  7. { Q_OBJECT
  8. public:
  9. Test() {}
  10. public slots:
  11. QString toString() {return "AAAAH!";}
  12. };
  13.  
  14. #endif // WIDGET_H
  15.  
  16. #include <QtGui/QApplication>
  17. #include "Widget.h"
  18. #include <QtScript>
  19. #include <QDebug>
  20.  
  21. void TestEval(QScriptEngine* eng,QString varname)
  22. {
  23. QScriptValue ret;
  24. ret = eng->evaluate(varname);
  25. qDebug() << ret.toString();
  26. ret = eng->evaluate(varname+".toString();");
  27. qDebug() << ret.toString();
  28. ret = eng->evaluate("\"StrConcat \"+"+varname+";");
  29. qDebug() << ret.toString();
  30. ret = eng->evaluate("\"StrConcat \"+"+varname+".toString();");
  31. qDebug() << ret.toString();
  32. }
  33.  
  34. int main(int argc, char *argv[])
  35. {
  36. QApplication a(argc, argv);
  37.  
  38. QScriptEngine* eng = new QScriptEngine();
  39.  
  40. eng->globalObject().setProperty("t",eng->newQObject(new Test()));
  41. qDebug() << "Object created before importing the qt.core extension, works as expected";
  42. TestEval(eng,"t");
  43.  
  44. qDebug() << "IMPORTING qt.core EXTENSION!";
  45. QScriptValue ret = eng->importExtension("qt.core");
  46. if (ret.isError()) qWarning() << ret.toString();
  47.  
  48. eng->globalObject().setProperty("t2",eng->newQObject(new Test()));
  49. qDebug() << "Object created before loading the qt.core extension, works as expected even after loading the extension";
  50. TestEval(eng,"t");
  51. qDebug() << "Object created after loading the qt.core extension, fails to implicitly convert to string in a string concatenation";
  52. TestEval(eng,"t2");
  53.  
  54. return 0;
  55. }
To copy to clipboard, switch view to plain text mode 

Output is:

Qt Code:
  1. Object created before importing the qt.core extension, works as expected "AAAAH!"
  2. "AAAAH!"
  3. "StrConcat AAAAH!"
  4. "StrConcat AAAAH!"
  5. IMPORTING qt.core EXTENSION!
  6. Object created before loading the qt.core extension, works as expected even after loading the extension
  7. "AAAAH!"
  8. "AAAAH!"
  9. "StrConcat AAAAH!"
  10. "StrConcat AAAAH!"
  11. Object created after loading the qt.core extension, fails to implicitly convert to string in a string concatenation
  12. "AAAAH!"
  13. "AAAAH!"
  14. "TypeError: Type error"
  15. "StrConcat AAAAH!"
To copy to clipboard, switch view to plain text mode 

For this to work you need the qt.core plugin, built by the QtScriptBindingsGenerator.

I am using Qt4.6 (Dec01) on WinXP built with the bundled MinGW.

Did anybody experience the same problem? What could be the source of it?

Thx in advance!

Johannes