Results 1 to 3 of 3

Thread: QtScript Problem with implicit QObject.toString and qt.core extension

  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

    Angry QtScript Problem with implicit QObject.toString and qt.core extension

    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

  2. #2
    Join Date
    Dec 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: QtScript Problem with implicit QObject.toString and qt.core extension

    We encountered a similar problem in our application, and I could also reproduce the problem with your example.

    Did you solve it somehow? Does anyone have a clue of what is going on?

    Our case is quite different because for us it happens so that the similar autoconversion made by PythonQt of Qt internal types (like Qt::Key) stops working when importExtension qt.core is made with a QtScriptEngine in the same application. So might be the same issue.

    ~Toni

  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: QtScript Problem with implicit QObject.toString and qt.core extension

    Hi Toni!

    No, I couldn't figure that one out, sorry. I ended up converting everything explicitly. Can't you do that, too?

    If not, you could comment parts of the bindings sourcecode out and check if the problem persists.

    Good luck,

    Johannes

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.