I am attempting to expose a Q_PROPERTY that references a Qt container to QtScript, that allows for both reading from the container and writing to the container. According to all the Qt documentation, this should be possible. I have spent a week trying everything I can think of, and searching this forum and the whole of the Google index to no avail. There are several similar threads on this forum, but none answer the question, really.
Everything I have tried allows the script to see the values in the container, but not alter the values in that container. Even the QtScript Debugger sees the values, but cannot modify them interactively.
Minimal, compilable code follows:
myclass.h (inlined for ease of posting)
{
Q_OBJECT
Q_PROPERTY(QStringList strings READ getStrings WRITE setStrings
);
public:
MyClass() { strings << "abc" << "def" << "ghi" << "jkl"; };
~MyClass() {};
public slots:
void setStrings(const QStringList& list) { strings = list; };
private:
};
class MyClass : public QObject
{
Q_OBJECT
Q_PROPERTY(QStringList strings READ getStrings WRITE setStrings);
public:
MyClass() { strings << "abc" << "def" << "ghi" << "jkl"; };
~MyClass() {};
QStringList getStrings() const { return strings; };
public slots:
void setStrings(const QStringList& list) { strings = list; };
private:
QStringList strings;
};
To copy to clipboard, switch view to plain text mode
main.cpp
#include "myclass.h"
int main(int argc, char *argv[])
{
QScriptEngine *scriptEngine = new QScriptEngine;
qScriptRegisterSequenceMetaType<QStringList>(scriptEngine);
MyClass *myclass = new MyClass();
QScriptValue value = scriptEngine->newQObject(myclass);
scriptEngine->globalObject().setProperty("AAA", value);
qDebug("Before: " + myclass->getStrings().join("~").toAscii());
scriptEngine->evaluate("AAA.strings[1] = 'xxx';");
qDebug("After : " + myclass->getStrings().join("~").toAscii());
}
#include "myclass.h"
Q_DECLARE_METATYPE(QStringList);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QScriptEngine *scriptEngine = new QScriptEngine;
qScriptRegisterSequenceMetaType<QStringList>(scriptEngine);
MyClass *myclass = new MyClass();
QScriptValue value = scriptEngine->newQObject(myclass);
scriptEngine->globalObject().setProperty("AAA", value);
qDebug("Before: " + myclass->getStrings().join("~").toAscii());
scriptEngine->evaluate("AAA.strings[1] = 'xxx';");
qDebug("After : " + myclass->getStrings().join("~").toAscii());
}
To copy to clipboard, switch view to plain text mode
Output:
Before: abc~def~ghi~jkl
After : abc~def~ghi~jkl
Before: abc~def~ghi~jkl
After : abc~def~ghi~jkl
To copy to clipboard, switch view to plain text mode
There is something obvious I am missing, but for the life of me, I can't figure it out. This is not limited to the QStringList container, I cannot WRITE to QVector, QList, QMap, or any other Qt containers.
Another hint: A breakpoint that is put on the "setStrings" method is never triggered, indicating to me that something is wrong with my syntax, in that the script engine has not been instructed properly to bind with my WRITE method.
I have also tried using the "qScriptRegisterMetaType" approach, and defining my own methods for converting the QStringList to and from QScriptValue, but that has not been successful either.
Representative threads (there are many) where this same topic has gone unanswered:
http://www.qtcentre.org/threads/6540...signer-plugins
http://www.qtcentre.org/threads/2522...Y-of-type-QMap
Any pointers are appreciated.
Bookmarks