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();
"A"+obj;
"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:
#ifndef WIDGET_H
#define WIDGET_H
#include <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[])
{
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;
}
#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;
}
To copy to clipboard, switch view to plain text mode
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!"
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!"
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
Bookmarks