PDA

View Full Version : QtScript Problem with implicit QObject.toString and qt.core extension



JohannesMunk
8th December 2009, 03:33
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:



"A"+obj;
"A"+obj.toString();


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

I built a minimal code example showing the problem:



#ifndef WIDGET_H
#define WIDGET_H

#include <QObject>

class Test : public QObject
{ Q_OBJECT
public:
Test() {}
public slots:
QString toString() {return "AAAAH!";}
};

#endif // WIDGET_H

#include <QtGui/QApplication>
#include "Widget.h"
#include <QtScript>
#include <QDebug>

void TestEval(QScriptEngine* eng,QString varname)
{
QScriptValue ret;
ret = eng->evaluate(varname);
qDebug() << ret.toString();
ret = eng->evaluate(varname+".toString();");
qDebug() << ret.toString();
ret = eng->evaluate("\"StrConcat \"+"+varname+";");
qDebug() << ret.toString();
ret = eng->evaluate("\"StrConcat \"+"+varname+".toString();");
qDebug() << ret.toString();
}

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QScriptEngine* eng = new QScriptEngine();

eng->globalObject().setProperty("t",eng->newQObject(new Test()));
qDebug() << "Object created before importing the qt.core extension, works as expected";
TestEval(eng,"t");

qDebug() << "IMPORTING qt.core EXTENSION!";
QScriptValue ret = eng->importExtension("qt.core");
if (ret.isError()) qWarning() << ret.toString();

eng->globalObject().setProperty("t2",eng->newQObject(new Test()));
qDebug() << "Object created before loading the qt.core extension, works as expected even after loading the extension";
TestEval(eng,"t");
qDebug() << "Object created after loading the qt.core extension, fails to implicitly convert to string in a string concatenation";
TestEval(eng,"t2");

return 0;
}


Output is:



Object created before importing the qt.core extension, works as expected "AAAAH!"
"AAAAH!"
"StrConcat AAAAH!"
"StrConcat AAAAH!"
IMPORTING qt.core EXTENSION!
Object created before loading the qt.core extension, works as expected even after loading the extension
"AAAAH!"
"AAAAH!"
"StrConcat AAAAH!"
"StrConcat AAAAH!"
Object created after loading the qt.core extension, fails to implicitly convert to string in a string concatenation
"AAAAH!"
"AAAAH!"
"TypeError: Type error"
"StrConcat AAAAH!"


For this to work you need the qt.core plugin, built by the QtScriptBindingsGenerator (http://qt.gitorious.org/qt-labs/qtscriptgenerator/commits/4.6).

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

antont
29th December 2010, 23:49
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

JohannesMunk
30th December 2010, 11:25
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