As we all know, connecting to the signals of a QObject from QtScript code is trivial; for example
bossButton.clicked.connect(function() { desktop.showFakeSpreadSheet(); employee.grin(); } );
However, what if you don’t intend to expose your QObject to script code? Let’s say you got this crazy idea, that you only intend for scripters to be able to code some script function (AKA “slot”) that you bring into your application, then you want to handle the actual signal-to-slot connections yourself, in C++ (god forbid!). What do you do then? Scream out in agony?
Well, if you’re using the daily Qt snapshots, there’s actually no need to do so; a couple of days ago we added the necessary functionality. Let’s have a look at the obligatory, completely useless example. First, we have a fair amount of bland code (sorry about that) that sets up a widget with two buttons:
#include <QtGui>
#include <QtScript>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QWidget window;
QVBoxLayout *vbox = new QVBoxLayout(&window);
QPushButton *awakenButton = new QPushButton("Rise From Your Watery Grave");
vbox->addWidget(awakenButton);
QPushButton *exitButton = new QPushButton("Exit");
vbox->addWidget(exitButton);
Then we create our script engine, and a script object, and give the object a suitable name:
QScriptEngine engine;
QScriptValue object = engine.newObject();
object.setProperty("name", QScriptValue(&engine, "Megatron"));
We read our “slot” script function from a file:
QFile file(":/slot.qs");
file.open(QIODevice::ReadOnly);
QScriptValue slot = engine.evaluate(file.readAll());
file.close();
Nothing out of the ordinary so far. But now we’re actually ready to do a signal-to-slot connection:
qScriptConnect(awakenButton, SIGNAL(clicked()), object, slot);
Wow. Just… Wow. How much easier, consistent and intuitive can it get? Both syntactically and semantically, it’s very similar to QObject::connect(). The main difference, of course, is that the last two arguments to qScriptConnect(), the receiver and the slot, are QtScript objects (one can be any type of object, the other must be a function object). When the QtScript “slot” function is executed in response to the signal, the receiver that you specified will act as the this-object. If you specify an invalid QScriptValue as the receiver, the this-object will be the interpreter’s Global Object; this even makes qScriptConnect() consistent with ECMAScript Function.prototype.call() and Function.prototype.apply(), for the purists who care about that stuff (hey, don’t look at me). There is of course also a qScriptDisconnect() function, which destroys a connection.
Finally, demonstrating the principle of “Why do it the easy way when you can do it the hard way”, let’s round off main() by using qScriptConnect() to connect to QApplication::quit() as well:
QScriptValue scriptApp = engine.newQObject(&app);
qScriptConnect(exitButton, SIGNAL(clicked()), scriptApp, scriptApp.property("quit"));
window.show();
return app.exec();
}
Mmm, beautiful. Last, but not least, the QtScript function loaded from slot.qs, which is executed when the first button is clicked:
return function() {
var sender = __qt_sender__;
sender.text = "YES! " + this.name + " is now alive and kicking!";
sender.styleSheet = "background: lime";
sender["clicked()"].disconnect(this, arguments.callee);
sender.checkable = true;
sender.toggled.connect(sender.hide);
}
First we use a QtScript extension to ECMAScript, __qt_sender__, to grab hold of the button that sent the signal (in general a dubious practice, if you ask me, whether it be script code or C++; but sometimes it’s just too tempting). We change the button’s text, give it a nice background color, then we break the connection that we established in C++ (arguments.callee to the rescue, as usual). Finally we do some more silly stuff, just to show that we can and aren’t afraid to do so.
Oh yeah, in case you were wondering, you can also do a connect from C++ with the QtScript 4.3 API, by doing this:
QScriptValue scriptButton = engine.newQObject(awakenButton);
QScriptValue scriptSignal = scriptButton.property("clicked()");
QScriptValueList args;
args << object << slot;
scriptSignal.property("connect").call(scriptSignal, args);
It’s simple to create a convenience function based on this code, having arguments identical to qScriptConnect(). Way to go 4.3, counterattack!
3 Responses to “Connecting Signals to QtScript Functions”
Nice! That last example solves the remaining piece of the puzzle in how to do a kross plugin that offers the same functionality as the kjsembed one. ![]()
I cannot understand your obsession to build your scripting language when a lot of very good languages exists and are also very popular with a lot of existing libraries (like Python).
You already make one time the mistake with QSA, how many times you need to make the same mistake to learn something from the past and open your world to Python and/or other scripting languages.
I have impression that you lose your time and the time of your users.
Qt is (was) “great” C++ graphical library, but growing on “inconsistent” way, instead of cover and anticipate (be proactive) all the graphical library requests, the Qt is moving on Java, “Green phone” and scripting language “again”.
Example of missing “basic” graphical library features (inside the waiting list from several years): custom page print, missing a lot new generation widgets like: auto-hide dock windows, QPicture export functionality (SVG, CGM, WMF…) etc.
Probably same users will be not agreeing with me: working on Java/Phone or working on applications that don’t need all the graphical library needs.
@MontCook: the QtScript technology is not only nice but it’s ECMA-262 standard, tightly integrated with Qt, very very performant and is a key component to allow anybody to expand his/her app with no limit. Trust me, we are already surprising our customers by showing them QtScript. If you want Python, find the autogenerated bindings somewhere. If you want a really cool scripting host environment, take QtScript, expose your Object and you’re ready to rock!
Long life QtScript!