MyScript.js
function doSomethingFromScriptFile()
{
for (var i=0; i < grabQObject.maxQObjects; i++)
{
var myQObject = grabQObject.objectAt(i);
myQObject.doMore();
}
}
function doSomethingFromScriptFile()
{
for (var i=0; i < grabQObject.maxQObjects; i++)
{
var myQObject = grabQObject.objectAt(i);
myQObject.doMore();
}
}
To copy to clipboard, switch view to plain text mode
This code is using a known QObject passed to the QML file by view.rootContext()->setContextProperty("grabQObject",grabQObject); to grab and return a new QObject each time through the loop so the javascript can process it.
MyQML.qml
import Qt 4.7
import "MyScript.js" as MyScript
Rectangle {
id: container
width: 600; height: 480
color: "lightgray"
Button {
text: "RunScript"
onClicked: {
MyScript.doSomethingFromScriptFile();
}
}
}
import Qt 4.7
import "MyScript.js" as MyScript
Rectangle {
id: container
width: 600; height: 480
color: "lightgray"
Button {
text: "RunScript"
onClicked: {
MyScript.doSomethingFromScriptFile();
}
}
}
To copy to clipboard, switch view to plain text mode
QML code with a simple button and mouse click area to run doSomethingFromScriptFile() from MyScript.js
main.cpp
#include <qdeclarative.h>
#include <QDeclarativeView>
#include <qdeclarativecontext.h>
int main(int argc, char *argv[])
{
QDeclarativeView view;
view.rootContext()->setContextProperty("grabQObject",grabQObject);
view.
setSource(QUrl::fromLocalFile("MyQML.qml"));
view.show();
}
#include <qdeclarative.h>
#include <QDeclarativeView>
#include <qdeclarativecontext.h>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDeclarativeView view;
view.rootContext()->setContextProperty("grabQObject",grabQObject);
view.setSource(QUrl::fromLocalFile("MyQML.qml"));
view.show();
}
To copy to clipboard, switch view to plain text mode
This is just enough c++ code to get us to execute the QML file and pass it our QObject grabQObject.
Now when I was using QtScript before I could just subclass QScriptable and do somethinglike this.
grabQObject.h
class GrabQObject
: public QObject,
protected QScriptable
{
Q_OBJECT
public:
public slots:
QScriptValue objectAt(int index);
int maxQObjects() { return m_count; }
private:
QList <QObject
*> availableObjects;
int m_count;
}
class GrabQObject : public QObject, protected QScriptable
{
Q_OBJECT
public:
GrabQObject(QObject *parent = 0);
public slots:
QScriptValue objectAt(int index);
int maxQObjects() { return m_count; }
private:
QList <QObject *> availableObjects;
int m_count;
}
To copy to clipboard, switch view to plain text mode
grabQObject.cpp
QScriptValue grabQObject::objectAt(int index)
{
if (index > availableObjects.size())
return engine()->nullValue();
else
return engine()->newQObject(availableObjects.at(index));
}
QScriptValue grabQObject::objectAt(int index)
{
if (index > availableObjects.size())
return engine()->nullValue();
else
return engine()->newQObject(availableObjects.at(index));
}
To copy to clipboard, switch view to plain text mode
This would use the calling QScriptEngine to make a returnable QObject formated as a QScriptValue and all was good.
My problem now is being able to access each new QObject as I loop through them in the script. When I call myQObject.doMore(); I get.
TypeError: Result of expression 'myQObject' [undefined] is not an object.
Not sure what is going wrong here and still learning QML and how it executes javascript, any help would be greatly appreciated.
Bob
Bookmarks