I am researching possibilities of QtScript. I understand that it is possible to create QObject
in C++ and then pass it into QScriptEngine
:
QObject *someObject = new WindowWithText;
QScriptValue objectValue = engine.newQObject(someObject);
engine.globalObject().setProperty("window", objectValue);
This works - I was able to call my methods defined in C++:
WindowWithText
declaration:
#include <QWidget>
namespace Ui {
class WindowWithText;
}
class WindowWithText : public QWidget
{
Q_OBJECT
public:
explicit WindowWithText(QWidget *parent = 0);
~WindowWithText();
public slots:
void setHeading(const QString&);
void setContents(const QString&);
QString getHeading() const;
private:
Ui::WindowWithText *ui;
};
But I would like to instantiate windows from qtscript itself, like this:
var window = new WindowWithText();
I understand I will probably have to write some proxy between constructor and QtCcript, but how to do it?
So far, I just created static
method newInstance
that creates the object, but that's no new
:
QScriptValue WindowWithText::newInstance(QScriptContext *context, QScriptEngine *engine)
{
QObject *someObject = new WindowWithText;
QScriptValue objectValue = engine->newQObject(someObject);
return objectValue;
}
I exported it to the engine as follows:
engine.globalObject().setProperty("WindowWithText", engine.newFunction(WindowWithText::newInstance));
This does not use new
though and isn't true javascript pseudoclass:
Following code will fail:
function Subclass() {
this.setHeading("bla bla");
}
Subclass.prototype = Object.create(WindowWithText.prototype);
var window = new dd();
window.show();
Error caused by the fact that WindowWithText.prototype
doesn't have anything to do with WindowWithText
:
TypeError: Result of expression 'this.setHeading' [undefined] is not a function.
Is there more reliable and less tedious way of exporting C++ classes to my engine?