O'Reilly logo

Game Programming using Qt 5 Beginner's Guide - Second Edition by Lorenz Haas, Witold Wysota, Pavel Strakhov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – Using a button from JavaScript

Let's put all this together and build a complete example of a scriptable button:

int main(int argc, char *argv[]) { QApplication app(argc, argv); QJSEngine engine; engine.installExtensions(QJSEngine::ConsoleExtension); QPushButton button; engine.globalObject().setProperty("pushButton", engine.newQObject(&button)); QString script = "pushButton.text = 'My Scripted Button';\n" "pushButton.checkable = true;\n" "pushButton.setChecked(true);\n" "pushButton.toggled.connect(function(checked) {\n" " console.log('button toggled!', checked);\n" "});\n" "pushButton.show();"; engine.evaluate(script); QJSValue func = engine.evaluate( "function(checked) { console.log('button toggled 2!', checked); }"); QObject::connect(&button, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required