SlideShare a Scribd company logo
Scripting Your Qt Application
                                09/25/09
About Me (Kent Hansen)

• Working on Qt since 2005
• QtScript
• Qt State Machine framework
• Plays harmonica and Irish whistle




                                      2
Goals For This Talk

• Give overview of QtScript and things related to it
• Show how to embed QtScript into your application




                                                       3
Agenda

• What is application scripting?
• QtScript tour (the essentials)
• Debugger
• Misc.




                                   4
“Scripting Your Application”: What?

• Premise: Core functionality implemented in C++
• Interface(s) exported to scripting environment
• Plugin-based, interactive (console), ...




                                                   5
Golden Example: The Web Browser

• Script loaded as part of web page
• Browser exports tree-based API to page (DOM)
• Script manipulates the page




                                                 6
What is QtScript?

• A Qt module
• A scripting language
• An “engine” for evaluating scripts
• An API for embedding into your application




                                               7
The QtScript Language

• Based on ECMA-262 standard
   –“JavaScript minus DOM”
• Object-oriented, dynamically typed
• Java-like syntax




                                       8
Disclaimer

This is not a JavaScript tutorial!




                                     9
The QtScript Engine

• Executes scripts
• Performs C++ <--> script bridging
• It's re-entrant




                                      10
The QtScript API

• Embed QtScript engine in your application
• Expose C++ objects to script environment
• Work with script values from C++




                                              11
Why should you use QtScript?

• Easy to embed into any Qt application
• Tight integration with rest of Qt
• Easy-to-use API




                                          12
Agenda

• What is application scripting?
• QtScript tour (the essentials)
• Debugger
• Misc.




                                   13
QtScript tour (the essentials)

• Basic embedding (example)
• Working with QScriptValues
• Configuring the script environment
• Qt meta-object system integration




                                       14
Example: Meaning of Life Calculator



   “Our objective: Write an application that uses
     QtScript to calculate the meaning of life”




                                                    15
QScriptEngine object

QScriptEngine engine;

• “Vanilla” environment
• Built-in ECMA objects
   –Array, Math, RegExp, Error, ...
   –parseInt(), parseFloat(), ...
• No Qt-specific script APIs




                                      16
Mutating the Environment (I)




                               17
Mutating the Environment (II)

• Evaluate scripts
• Operate on QtScript values in C++




                                      18
QtScript tour (the essentials)

• Basic embedding (example)
• Working with QScriptValues
• Configuring the script environment
• Qt meta-object system integration




                                       19
QScriptValue class

• Represents a QtScript (JavaScript) value
     • Undefined
     • Null
     • Boolean
     • Number
     • String
     • Object
         –Functions, arrays, regexps, errors, ...
     • Invalid (no value)



                                                    20
QScriptValue type checks & conversion

• isXXX(): Tests if value is of a certain type
• toXXX(): Converts to Qt/C++ type
• qscriptvalue_cast(): Converts to C++ type




                                                 21
QScriptValue construction

• Constructors for standard Qt/C++ types
• qScriptValueFromValue()
      • Counterpart to qscriptvalue_cast()
• QScriptEngine::newXXX()
      • Object, QObject, Array, RegExp




                                             22
QScriptValue property access


 JS:   myObject.foo = 123;

       myObject['foo'] = 123;


 C++: myObject.setProperty(“foo”, 123);




                                          23
QScriptValue holds reference to object (I)
QScriptValue object = engine.evaluate(
“{
   foo: 123,
   toString: function() {
     return 'MyObject(foo=' + this.foo + ')';
   }
}”);




                                             24
QScriptValue holds reference to object (II)

QScriptValue same = object;
object.setProperty(“foo”, 456);
qDebug() << same.toString();




                                              25
QScriptValue called as function

 JS: myArray.sort(); or
    myArray['sort'].call(myArray);


 C++: myArray.property(“sort”)
     .call(myArray);




                                     26
Calling a function with arguments (I)

JS:   myObject.myFunction(123, “foo”);


C++: myFunction.call(
      myObject,
      QScriptValueList()
      << 123 << “foo”);



                                         27
Calling a function with arguments (II)

QScriptValue adder = engine.evaluate(
 “(function(a, b) { return a+b; })”);


QScriptValue result = adder.call(
  /*thisObject=*/QScriptValue(),
  QScriptValueList() << 41 << 1);



                                         28
Calling a function as constructor (I)

JS: function Point(x, y) {
          this.x = x;
          this.y = y;
    }
    ...
    new Point(10, 20);



                                        29
Calling a function as constructor (II)

C++:
QScriptValue p = Point.construct(
       QScriptValueList() << 10 << 20);


qDebug() << p.property(“x”).toNumber();




                                          30
QScriptValueIterator class


 QScriptValueIterator it(myObject);
 while (it.hasNext()) {
     it.next(); qDebug() << it.name();
 }




                                         31
Working with JavaScript Arrays (I)

 JS:

 var a = [10, 20, 30];

 print(a[0], a['0']); // same property

 a.foo = 123;

 a.push(40);

 a[a.length] = 'appended';




                                         32
Working with JavaScript Arrays (II)
 C++:

 QScriptValue a = engine.evaluate(

        “[10, 20, 30]”); // array literal

 qDebug() << a.property(“length”).toInt32();

 qDebug() << a.property(“0”).toNumber();

 // for convenience + speed

 qDebug() << a.property(0).toNumber();




                                               33
Error Handling (I)

 // Uh-oh, this script will throw an error!

 QScriptValue result = engine.evaluate(

   “noSuchVariable”);




                                              34
Error Handling (II)

 if (result.isError()) {

     QString message = result.toString();

     // Notify user about the problem ...

 }




                                            35
QScriptValue API Summary

• isXXX(), toXXX()
• property() and setProperty()
• call(), construct()
• QScriptValueIterator for introspection




                                           36
QtScript tour (the essentials)

• Basic embedding (example)
• Working with QScriptValues
• Configuring the script environment
• Qt meta-object system integration




                                       37
The Global Object

• A built-in script object
• Every QScriptEngine has one
      • QScriptEngine::globalObject()
• Initially contains properties defined by ECMA-262




                                                      38
Tailoring Scripting to Your Application

• Design “contract” of C++ <--> script interaction
• Set properties of Global Object
• Evaluate scripts that use your API
• (Optional: Call back into script objects as
 appropriate)




                                                     39
Example: Meaning of Life Environment

C++:
QScriptEngine engine;
QScriptValue global = engine.globalObject();
global.setProperty(“meaningOfLife”, 42,
                   QScriptValue::ReadOnly);


JS:
var myMeaningOfLife = Math.sqrt(meaningOfLife);




                                                  40
Example: Fake Navigator Object

C++:

QScriptValue navigator = engine.newObject();
navigator.setProperty(“appVersion”, 1);
navigator.setProperty(“appMinorVersion”, 1);
navigator.setProperty(“cookieEnabled”, false);
navigator.setProperty(“browserLanguage”, “en_US”);
navigator.setProperty(“platform”, “No Such OS”);
navigator.setProperty(“userAgent”, “007”);

global.setProperty(“navigator”, navigator);



                                                     41
QtScript tour (the essentials)

• Basic embedding (example)
• Working with QScriptValues
• Configuring the script environment
• Qt meta-object system integration




                                       42
Qt Meta-Object System

• In Qt, QObject-based classes can be introspected
 at runtime
      • Properties
      • Signals & slots
• QObject::connect()
• Qt Designer property editor
• ...




                                                     43
Relax! Leave it to magic




                           44
QtScript <--> QObject Integration (I)

• QScriptEngine::newQObject(QObject*)
• Returns a script object that acts as proxy
• Proxy delegates property access to
 QObject::property() and QObject::setProperty()




                                                  45
QtScript <--> QObject Integration (II)

• Proxy provides signals and slots as properties
• Proxy provides named child objects as properties




                                                     46
QtScript <--> QObject Integration (III)

• Ownership control
• Customize proxy behavior
      • Don't expose child objects
      • Don't expose super class contents




                                            47
Example: Scripting a QLineEdit




                                 48
qScriptConnect() function

• Connect a signal to a script function
• Let scripts define signal handlers
• Application does the “plumbing”




                                          49
Summary of “API essentials”

• QScriptValue
• QScriptEngine
     • evaluate()
     • globalObject()
     • newQObject()
• qScriptConnect()




                              50
Agenda

• What is application scripting?
• QtScript tour (the essentials)
• Debugger
• Misc.




                                   51
The QtScript Debugger

• Graphical debugger
• Introduced in Qt 4.5
• To make the API available in your application:
      QT += scripttools
      #include <QtScriptTools>




                                                   52
Debugger Demo

Look at the pretty little demo!




                                  53
The QtScript Debugger API (I)

One class in public API: QScriptEngineDebugger

 QScriptEngine engine;
 QScriptEngineDebugger debugger;
 debugger.attachTo(&engine);


 // evaluate scripts ...




                                                 54
The QtScript Debugger API (II)

• Debugger actions can be programmatically
 triggered
• Individual debugger widgets are accessible




                                               55
Invoking the Debugger from a Script

• Use the debugger keyword
• Becomes no-op if no debugger is attached

    JS:

    var a = Math.random();
    debugger; // synchronously run debugger
    var b = Math.random();
    debugger;
    return a + b;


                                              56
Agenda

• What is application scripting?
• QtScript tour (the essentials)
• Debugger
• Misc.




                                   57
Prototypes...?




                 58
Prototype-based Inheritance (I)




                                  59
Prototype-based Inheritance: Why care?

• Not every QObject member is a property / signal /
 slot
• Not everything is a QObject
• Not everything should/needs to be a QObject
• JS developers are familiar with prototypes




                                                      60
Prototype-based Inheritance: How?

• Create a prototype object for the C++ type
• Register the type with Qt's meta-type system
• Associate prototype object with meta-type ID
• Create QScriptValues from the C++ type
 (qScriptValueFromValue())




                                                 61
Bindings for Qt's APIs (I)

• Available as stand-alone project
• qtscriptgenerator on Qt Labs
 (http://labs.qt.nokia.com)




                                     62
Bindings for Qt's APIs (II)

• Creates plugins that can be imported into a script
 engine
• Provides bindings for most of Qt's classes
• Makes it possible to write pure script applications




                                                        63
Summary

• It's easy to make your Qt application scriptable
 with QtScript
• There's a QtScript debugger (also part of Qt)
• You can achieve a lot by mastering a small API




                                                     64
Thank You!

Questions?




             65

More Related Content

What's hot

Best Practices in Qt Quick/QML - Part I
Best Practices in Qt Quick/QML - Part IBest Practices in Qt Quick/QML - Part I
Best Practices in Qt Quick/QML - Part I
ICS
 
Qt for beginners part 1 overview and key concepts
Qt for beginners part 1   overview and key conceptsQt for beginners part 1   overview and key concepts
Qt for beginners part 1 overview and key concepts
ICS
 
Hello, QML
Hello, QMLHello, QML
Hello, QML
Jack Yang
 
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with QtConvert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
ICS
 
Qt State Machine Framework
Qt State Machine FrameworkQt State Machine Framework
Qt State Machine Framework
account inactive
 
Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4
ICS
 
Qt 5 - C++ and Widgets
Qt 5 - C++ and WidgetsQt 5 - C++ and Widgets
Qt 5 - C++ and Widgets
Juha Peltomäki
 
UI Programming with Qt-Quick and QML
UI Programming with Qt-Quick and QMLUI Programming with Qt-Quick and QML
UI Programming with Qt-Quick and QML
Emertxe Information Technologies Pvt Ltd
 
Introduction to the Qt State Machine Framework using Qt 6
Introduction to the Qt State Machine Framework using Qt 6Introduction to the Qt State Machine Framework using Qt 6
Introduction to the Qt State Machine Framework using Qt 6
ICS
 
本当のオブジェクト指向は可読性を上げる
本当のオブジェクト指向は可読性を上げる本当のオブジェクト指向は可読性を上げる
本当のオブジェクト指向は可読性を上げる
Wataru Terada
 
Qt and QML performance tips & tricks for Qt 4.7
Qt and QML performance tips & tricks for Qt 4.7Qt and QML performance tips & tricks for Qt 4.7
Qt and QML performance tips & tricks for Qt 4.7
Pasi Kellokoski
 
QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong? QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong?
ICS
 
02 - Basics of Qt
02 - Basics of Qt02 - Basics of Qt
02 - Basics of Qt
Andreas Jakl
 
セキュリティ未経験だったけど入社1年目から Bug Bounty Program 運営に参加してみた
セキュリティ未経験だったけど入社1年目から Bug Bounty Program 運営に参加してみたセキュリティ未経験だったけど入社1年目から Bug Bounty Program 運営に参加してみた
セキュリティ未経験だったけど入社1年目から Bug Bounty Program 運営に参加してみた
LINE Corporation
 
用 C# 與 .NET 也能打造機器學習模型:你所不知道的 ML.NET 初體驗
用 C# 與 .NET 也能打造機器學習模型:你所不知道的 ML.NET 初體驗用 C# 與 .NET 也能打造機器學習模型:你所不知道的 ML.NET 初體驗
用 C# 與 .NET 也能打造機器學習模型:你所不知道的 ML.NET 初體驗
Ko Ko
 
객체지향 개념 (쫌 아는체 하기)
객체지향 개념 (쫌 아는체 하기)객체지향 개념 (쫌 아는체 하기)
객체지향 개념 (쫌 아는체 하기)
Seung-June Lee
 
Eos - Efficient Private Delegation of zkSNARK provers
Eos  - Efficient Private Delegation of zkSNARK proversEos  - Efficient Private Delegation of zkSNARK provers
Eos - Efficient Private Delegation of zkSNARK provers
Alex Pruden
 
Qt programming-using-cpp
Qt programming-using-cppQt programming-using-cpp
Qt Framework Events Signals Threads
Qt Framework Events Signals ThreadsQt Framework Events Signals Threads
Qt Framework Events Signals Threads
Neera Mital
 

What's hot (20)

Best Practices in Qt Quick/QML - Part I
Best Practices in Qt Quick/QML - Part IBest Practices in Qt Quick/QML - Part I
Best Practices in Qt Quick/QML - Part I
 
Qt for beginners part 1 overview and key concepts
Qt for beginners part 1   overview and key conceptsQt for beginners part 1   overview and key concepts
Qt for beginners part 1 overview and key concepts
 
Hello, QML
Hello, QMLHello, QML
Hello, QML
 
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with QtConvert Your Legacy OpenGL Code to Modern OpenGL with Qt
Convert Your Legacy OpenGL Code to Modern OpenGL with Qt
 
Qt State Machine Framework
Qt State Machine FrameworkQt State Machine Framework
Qt State Machine Framework
 
Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4Best Practices in Qt Quick/QML - Part 1 of 4
Best Practices in Qt Quick/QML - Part 1 of 4
 
Qt 5 - C++ and Widgets
Qt 5 - C++ and WidgetsQt 5 - C++ and Widgets
Qt 5 - C++ and Widgets
 
UI Programming with Qt-Quick and QML
UI Programming with Qt-Quick and QMLUI Programming with Qt-Quick and QML
UI Programming with Qt-Quick and QML
 
Introduction to the Qt State Machine Framework using Qt 6
Introduction to the Qt State Machine Framework using Qt 6Introduction to the Qt State Machine Framework using Qt 6
Introduction to the Qt State Machine Framework using Qt 6
 
本当のオブジェクト指向は可読性を上げる
本当のオブジェクト指向は可読性を上げる本当のオブジェクト指向は可読性を上げる
本当のオブジェクト指向は可読性を上げる
 
Qt and QML performance tips & tricks for Qt 4.7
Qt and QML performance tips & tricks for Qt 4.7Qt and QML performance tips & tricks for Qt 4.7
Qt and QML performance tips & tricks for Qt 4.7
 
Qt Qml
Qt QmlQt Qml
Qt Qml
 
QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong? QThreads: Are You Using Them Wrong?
QThreads: Are You Using Them Wrong?
 
02 - Basics of Qt
02 - Basics of Qt02 - Basics of Qt
02 - Basics of Qt
 
セキュリティ未経験だったけど入社1年目から Bug Bounty Program 運営に参加してみた
セキュリティ未経験だったけど入社1年目から Bug Bounty Program 運営に参加してみたセキュリティ未経験だったけど入社1年目から Bug Bounty Program 運営に参加してみた
セキュリティ未経験だったけど入社1年目から Bug Bounty Program 運営に参加してみた
 
用 C# 與 .NET 也能打造機器學習模型:你所不知道的 ML.NET 初體驗
用 C# 與 .NET 也能打造機器學習模型:你所不知道的 ML.NET 初體驗用 C# 與 .NET 也能打造機器學習模型:你所不知道的 ML.NET 初體驗
用 C# 與 .NET 也能打造機器學習模型:你所不知道的 ML.NET 初體驗
 
객체지향 개념 (쫌 아는체 하기)
객체지향 개념 (쫌 아는체 하기)객체지향 개념 (쫌 아는체 하기)
객체지향 개념 (쫌 아는체 하기)
 
Eos - Efficient Private Delegation of zkSNARK provers
Eos  - Efficient Private Delegation of zkSNARK proversEos  - Efficient Private Delegation of zkSNARK provers
Eos - Efficient Private Delegation of zkSNARK provers
 
Qt programming-using-cpp
Qt programming-using-cppQt programming-using-cpp
Qt programming-using-cpp
 
Qt Framework Events Signals Threads
Qt Framework Events Signals ThreadsQt Framework Events Signals Threads
Qt Framework Events Signals Threads
 

Viewers also liked

Meet Qt
Meet QtMeet Qt
Qt Graphics View Framework (Qt Developers Meetup Isreal)
Qt Graphics View Framework (Qt Developers Meetup Isreal)Qt Graphics View Framework (Qt Developers Meetup Isreal)
Qt Graphics View Framework (Qt Developers Meetup Isreal)
vitalipe
 
Practical Model View Programming
Practical Model View ProgrammingPractical Model View Programming
Practical Model View ProgrammingMarius Bugge Monsen
 
GUI_using_QT_Designer_PyQT4
GUI_using_QT_Designer_PyQT4GUI_using_QT_Designer_PyQT4
GUI_using_QT_Designer_PyQT4zenonas
 
Python Tricks That You Can't Live Without
Python Tricks That You Can't Live WithoutPython Tricks That You Can't Live Without
Python Tricks That You Can't Live Without
Audrey Roy
 
Object-oriented Programming in Python
Object-oriented Programming in PythonObject-oriented Programming in Python
Object-oriented Programming in Python
Juan-Manuel Gimeno
 
Python Advanced – Building on the foundation
Python Advanced – Building on the foundationPython Advanced – Building on the foundation
Python Advanced – Building on the foundation
Kevlin Henney
 
Python Worst Practices
Python Worst PracticesPython Worst Practices
Python Worst Practices
Daniel Greenfeld
 

Viewers also liked (8)

Meet Qt
Meet QtMeet Qt
Meet Qt
 
Qt Graphics View Framework (Qt Developers Meetup Isreal)
Qt Graphics View Framework (Qt Developers Meetup Isreal)Qt Graphics View Framework (Qt Developers Meetup Isreal)
Qt Graphics View Framework (Qt Developers Meetup Isreal)
 
Practical Model View Programming
Practical Model View ProgrammingPractical Model View Programming
Practical Model View Programming
 
GUI_using_QT_Designer_PyQT4
GUI_using_QT_Designer_PyQT4GUI_using_QT_Designer_PyQT4
GUI_using_QT_Designer_PyQT4
 
Python Tricks That You Can't Live Without
Python Tricks That You Can't Live WithoutPython Tricks That You Can't Live Without
Python Tricks That You Can't Live Without
 
Object-oriented Programming in Python
Object-oriented Programming in PythonObject-oriented Programming in Python
Object-oriented Programming in Python
 
Python Advanced – Building on the foundation
Python Advanced – Building on the foundationPython Advanced – Building on the foundation
Python Advanced – Building on the foundation
 
Python Worst Practices
Python Worst PracticesPython Worst Practices
Python Worst Practices
 

Similar to Scripting Your Qt Application

Qt for beginners
Qt for beginnersQt for beginners
Qt for beginners
Sergio Shevchenko
 
Hybrid Apps (Native + Web) via QtWebKit
Hybrid Apps (Native + Web) via QtWebKitHybrid Apps (Native + Web) via QtWebKit
Hybrid Apps (Native + Web) via QtWebKitAriya Hidayat
 
Hybrid Apps (Native + Web) using WebKit
Hybrid Apps (Native + Web) using WebKitHybrid Apps (Native + Web) using WebKit
Hybrid Apps (Native + Web) using WebKit
Ariya Hidayat
 
Hybrid Apps (Native + Web) using WebKit
Hybrid Apps (Native + Web) using WebKitHybrid Apps (Native + Web) using WebKit
Hybrid Apps (Native + Web) using WebKit
Ariya Hidayat
 
Building Web Apps Sanely - EclipseCon 2010
Building Web Apps Sanely - EclipseCon 2010Building Web Apps Sanely - EclipseCon 2010
Building Web Apps Sanely - EclipseCon 2010
Chris Ramsdale
 
下午3 intel fenghaitao_mee_go api and application development
下午3 intel fenghaitao_mee_go api and application development下午3 intel fenghaitao_mee_go api and application development
下午3 intel fenghaitao_mee_go api and application developmentcsdnmobile
 
Google Developer Fest 2010
Google Developer Fest 2010Google Developer Fest 2010
Google Developer Fest 2010Chris Ramsdale
 
What is the ServiceStack?
What is the ServiceStack?What is the ServiceStack?
What is the ServiceStack?
Demis Bellot
 
Google io bootcamp_2010
Google io bootcamp_2010Google io bootcamp_2010
Google io bootcamp_2010Chris Ramsdale
 
Qt & Webkit
Qt & WebkitQt & Webkit
Qt & Webkit
QT-day
 
Advanced Visualization with OpenGL in Oil & Gas
Advanced Visualization with OpenGL in Oil & GasAdvanced Visualization with OpenGL in Oil & Gas
Advanced Visualization with OpenGL in Oil & Gas
account inactive
 
Aplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & JetpackAplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & Jetpack
Nelson Glauber Leal
 
Petri Niemi Qt Advanced Part 1
Petri Niemi Qt Advanced Part 1Petri Niemi Qt Advanced Part 1
Petri Niemi Qt Advanced Part 1
NokiaAppForum
 
Auto cad 2006_api_overview
Auto cad 2006_api_overviewAuto cad 2006_api_overview
Auto cad 2006_api_overviewscdhruv5
 
Raffaele Rialdi
Raffaele RialdiRaffaele Rialdi
Raffaele Rialdi
CodeFest
 
[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless
KatyShimizu
 
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
KatyShimizu
 
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...
DataStax Academy
 
Qt for beginners part 4 doing more
Qt for beginners part 4   doing moreQt for beginners part 4   doing more
Qt for beginners part 4 doing more
ICS
 

Similar to Scripting Your Qt Application (20)

Qt for beginners
Qt for beginnersQt for beginners
Qt for beginners
 
Hybrid Apps (Native + Web) via QtWebKit
Hybrid Apps (Native + Web) via QtWebKitHybrid Apps (Native + Web) via QtWebKit
Hybrid Apps (Native + Web) via QtWebKit
 
Hybrid Apps (Native + Web) using WebKit
Hybrid Apps (Native + Web) using WebKitHybrid Apps (Native + Web) using WebKit
Hybrid Apps (Native + Web) using WebKit
 
Hybrid Apps (Native + Web) using WebKit
Hybrid Apps (Native + Web) using WebKitHybrid Apps (Native + Web) using WebKit
Hybrid Apps (Native + Web) using WebKit
 
Building Web Apps Sanely - EclipseCon 2010
Building Web Apps Sanely - EclipseCon 2010Building Web Apps Sanely - EclipseCon 2010
Building Web Apps Sanely - EclipseCon 2010
 
下午3 intel fenghaitao_mee_go api and application development
下午3 intel fenghaitao_mee_go api and application development下午3 intel fenghaitao_mee_go api and application development
下午3 intel fenghaitao_mee_go api and application development
 
Google Developer Fest 2010
Google Developer Fest 2010Google Developer Fest 2010
Google Developer Fest 2010
 
JBoss World 2010
JBoss World 2010JBoss World 2010
JBoss World 2010
 
What is the ServiceStack?
What is the ServiceStack?What is the ServiceStack?
What is the ServiceStack?
 
Google io bootcamp_2010
Google io bootcamp_2010Google io bootcamp_2010
Google io bootcamp_2010
 
Qt & Webkit
Qt & WebkitQt & Webkit
Qt & Webkit
 
Advanced Visualization with OpenGL in Oil & Gas
Advanced Visualization with OpenGL in Oil & GasAdvanced Visualization with OpenGL in Oil & Gas
Advanced Visualization with OpenGL in Oil & Gas
 
Aplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & JetpackAplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & Jetpack
 
Petri Niemi Qt Advanced Part 1
Petri Niemi Qt Advanced Part 1Petri Niemi Qt Advanced Part 1
Petri Niemi Qt Advanced Part 1
 
Auto cad 2006_api_overview
Auto cad 2006_api_overviewAuto cad 2006_api_overview
Auto cad 2006_api_overview
 
Raffaele Rialdi
Raffaele RialdiRaffaele Rialdi
Raffaele Rialdi
 
[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless
 
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
 
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...
 
Qt for beginners part 4 doing more
Qt for beginners part 4   doing moreQt for beginners part 4   doing more
Qt for beginners part 4 doing more
 

More from account inactive

KDE Plasma for Mobile Phones
KDE Plasma for Mobile PhonesKDE Plasma for Mobile Phones
KDE Plasma for Mobile Phones
account inactive
 
Shipping Mobile Applications Using Qt for Symbian
Shipping Mobile Applications Using Qt for SymbianShipping Mobile Applications Using Qt for Symbian
Shipping Mobile Applications Using Qt for Symbian
account inactive
 
The Future of Qt Widgets
The Future of Qt WidgetsThe Future of Qt Widgets
The Future of Qt Widgets
account inactive
 
Special Effects with Qt Graphics View
Special Effects with Qt Graphics ViewSpecial Effects with Qt Graphics View
Special Effects with Qt Graphics View
account inactive
 
Developments in The Qt WebKit Integration
Developments in The Qt WebKit IntegrationDevelopments in The Qt WebKit Integration
Developments in The Qt WebKit Integration
account inactive
 
Qt Kwan-Do
Qt Kwan-DoQt Kwan-Do
Qt Kwan-Do
account inactive
 
Qt on Real Time Operating Systems
Qt on Real Time Operating SystemsQt on Real Time Operating Systems
Qt on Real Time Operating Systems
account inactive
 
Development with Qt for Windows CE
Development with Qt for Windows CEDevelopment with Qt for Windows CE
Development with Qt for Windows CE
account inactive
 
Translating Qt Applications
Translating Qt ApplicationsTranslating Qt Applications
Translating Qt Applications
account inactive
 
Qt Creator Bootcamp
Qt Creator BootcampQt Creator Bootcamp
Qt Creator Bootcamp
account inactive
 
Qt Widget In-Depth
Qt Widget In-DepthQt Widget In-Depth
Qt Widget In-Depth
account inactive
 
Mobile Development with Qt for Symbian
Mobile Development with Qt for SymbianMobile Development with Qt for Symbian
Mobile Development with Qt for Symbian
account inactive
 
How to Make Your Qt App Look Native
How to Make Your Qt App Look NativeHow to Make Your Qt App Look Native
How to Make Your Qt App Look Native
account inactive
 
Animation Framework: A Step Towards Modern UIs
Animation Framework: A Step Towards Modern UIsAnimation Framework: A Step Towards Modern UIs
Animation Framework: A Step Towards Modern UIs
account inactive
 
Using Multi-Touch and Gestures with Qt
Using Multi-Touch and Gestures with QtUsing Multi-Touch and Gestures with Qt
Using Multi-Touch and Gestures with Qt
account inactive
 
Debugging Qt, Fixing and Contributing a Bug Report (Using Gitorious)
Debugging Qt, Fixing and Contributing a Bug Report (Using Gitorious)Debugging Qt, Fixing and Contributing a Bug Report (Using Gitorious)
Debugging Qt, Fixing and Contributing a Bug Report (Using Gitorious)
account inactive
 
The Mobility Project
The Mobility ProjectThe Mobility Project
The Mobility Project
account inactive
 
Copy Your Favourite Nokia App with Qt
Copy Your Favourite Nokia App with QtCopy Your Favourite Nokia App with Qt
Copy Your Favourite Nokia App with Qt
account inactive
 
The Next Generation Qt Item Views
The Next Generation Qt Item ViewsThe Next Generation Qt Item Views
The Next Generation Qt Item Views
account inactive
 
Optimizing Performance in Qt-Based Applications
Optimizing Performance in Qt-Based ApplicationsOptimizing Performance in Qt-Based Applications
Optimizing Performance in Qt-Based Applications
account inactive
 

More from account inactive (20)

KDE Plasma for Mobile Phones
KDE Plasma for Mobile PhonesKDE Plasma for Mobile Phones
KDE Plasma for Mobile Phones
 
Shipping Mobile Applications Using Qt for Symbian
Shipping Mobile Applications Using Qt for SymbianShipping Mobile Applications Using Qt for Symbian
Shipping Mobile Applications Using Qt for Symbian
 
The Future of Qt Widgets
The Future of Qt WidgetsThe Future of Qt Widgets
The Future of Qt Widgets
 
Special Effects with Qt Graphics View
Special Effects with Qt Graphics ViewSpecial Effects with Qt Graphics View
Special Effects with Qt Graphics View
 
Developments in The Qt WebKit Integration
Developments in The Qt WebKit IntegrationDevelopments in The Qt WebKit Integration
Developments in The Qt WebKit Integration
 
Qt Kwan-Do
Qt Kwan-DoQt Kwan-Do
Qt Kwan-Do
 
Qt on Real Time Operating Systems
Qt on Real Time Operating SystemsQt on Real Time Operating Systems
Qt on Real Time Operating Systems
 
Development with Qt for Windows CE
Development with Qt for Windows CEDevelopment with Qt for Windows CE
Development with Qt for Windows CE
 
Translating Qt Applications
Translating Qt ApplicationsTranslating Qt Applications
Translating Qt Applications
 
Qt Creator Bootcamp
Qt Creator BootcampQt Creator Bootcamp
Qt Creator Bootcamp
 
Qt Widget In-Depth
Qt Widget In-DepthQt Widget In-Depth
Qt Widget In-Depth
 
Mobile Development with Qt for Symbian
Mobile Development with Qt for SymbianMobile Development with Qt for Symbian
Mobile Development with Qt for Symbian
 
How to Make Your Qt App Look Native
How to Make Your Qt App Look NativeHow to Make Your Qt App Look Native
How to Make Your Qt App Look Native
 
Animation Framework: A Step Towards Modern UIs
Animation Framework: A Step Towards Modern UIsAnimation Framework: A Step Towards Modern UIs
Animation Framework: A Step Towards Modern UIs
 
Using Multi-Touch and Gestures with Qt
Using Multi-Touch and Gestures with QtUsing Multi-Touch and Gestures with Qt
Using Multi-Touch and Gestures with Qt
 
Debugging Qt, Fixing and Contributing a Bug Report (Using Gitorious)
Debugging Qt, Fixing and Contributing a Bug Report (Using Gitorious)Debugging Qt, Fixing and Contributing a Bug Report (Using Gitorious)
Debugging Qt, Fixing and Contributing a Bug Report (Using Gitorious)
 
The Mobility Project
The Mobility ProjectThe Mobility Project
The Mobility Project
 
Copy Your Favourite Nokia App with Qt
Copy Your Favourite Nokia App with QtCopy Your Favourite Nokia App with Qt
Copy Your Favourite Nokia App with Qt
 
The Next Generation Qt Item Views
The Next Generation Qt Item ViewsThe Next Generation Qt Item Views
The Next Generation Qt Item Views
 
Optimizing Performance in Qt-Based Applications
Optimizing Performance in Qt-Based ApplicationsOptimizing Performance in Qt-Based Applications
Optimizing Performance in Qt-Based Applications
 

Recently uploaded

Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
Abida Shariff
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 

Recently uploaded (20)

Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 

Scripting Your Qt Application

  • 1. Scripting Your Qt Application 09/25/09
  • 2. About Me (Kent Hansen) • Working on Qt since 2005 • QtScript • Qt State Machine framework • Plays harmonica and Irish whistle 2
  • 3. Goals For This Talk • Give overview of QtScript and things related to it • Show how to embed QtScript into your application 3
  • 4. Agenda • What is application scripting? • QtScript tour (the essentials) • Debugger • Misc. 4
  • 5. “Scripting Your Application”: What? • Premise: Core functionality implemented in C++ • Interface(s) exported to scripting environment • Plugin-based, interactive (console), ... 5
  • 6. Golden Example: The Web Browser • Script loaded as part of web page • Browser exports tree-based API to page (DOM) • Script manipulates the page 6
  • 7. What is QtScript? • A Qt module • A scripting language • An “engine” for evaluating scripts • An API for embedding into your application 7
  • 8. The QtScript Language • Based on ECMA-262 standard –“JavaScript minus DOM” • Object-oriented, dynamically typed • Java-like syntax 8
  • 9. Disclaimer This is not a JavaScript tutorial! 9
  • 10. The QtScript Engine • Executes scripts • Performs C++ <--> script bridging • It's re-entrant 10
  • 11. The QtScript API • Embed QtScript engine in your application • Expose C++ objects to script environment • Work with script values from C++ 11
  • 12. Why should you use QtScript? • Easy to embed into any Qt application • Tight integration with rest of Qt • Easy-to-use API 12
  • 13. Agenda • What is application scripting? • QtScript tour (the essentials) • Debugger • Misc. 13
  • 14. QtScript tour (the essentials) • Basic embedding (example) • Working with QScriptValues • Configuring the script environment • Qt meta-object system integration 14
  • 15. Example: Meaning of Life Calculator “Our objective: Write an application that uses QtScript to calculate the meaning of life” 15
  • 16. QScriptEngine object QScriptEngine engine; • “Vanilla” environment • Built-in ECMA objects –Array, Math, RegExp, Error, ... –parseInt(), parseFloat(), ... • No Qt-specific script APIs 16
  • 18. Mutating the Environment (II) • Evaluate scripts • Operate on QtScript values in C++ 18
  • 19. QtScript tour (the essentials) • Basic embedding (example) • Working with QScriptValues • Configuring the script environment • Qt meta-object system integration 19
  • 20. QScriptValue class • Represents a QtScript (JavaScript) value • Undefined • Null • Boolean • Number • String • Object –Functions, arrays, regexps, errors, ... • Invalid (no value) 20
  • 21. QScriptValue type checks & conversion • isXXX(): Tests if value is of a certain type • toXXX(): Converts to Qt/C++ type • qscriptvalue_cast(): Converts to C++ type 21
  • 22. QScriptValue construction • Constructors for standard Qt/C++ types • qScriptValueFromValue() • Counterpart to qscriptvalue_cast() • QScriptEngine::newXXX() • Object, QObject, Array, RegExp 22
  • 23. QScriptValue property access JS: myObject.foo = 123; myObject['foo'] = 123; C++: myObject.setProperty(“foo”, 123); 23
  • 24. QScriptValue holds reference to object (I) QScriptValue object = engine.evaluate( “{ foo: 123, toString: function() { return 'MyObject(foo=' + this.foo + ')'; } }”); 24
  • 25. QScriptValue holds reference to object (II) QScriptValue same = object; object.setProperty(“foo”, 456); qDebug() << same.toString(); 25
  • 26. QScriptValue called as function JS: myArray.sort(); or myArray['sort'].call(myArray); C++: myArray.property(“sort”) .call(myArray); 26
  • 27. Calling a function with arguments (I) JS: myObject.myFunction(123, “foo”); C++: myFunction.call( myObject, QScriptValueList() << 123 << “foo”); 27
  • 28. Calling a function with arguments (II) QScriptValue adder = engine.evaluate( “(function(a, b) { return a+b; })”); QScriptValue result = adder.call( /*thisObject=*/QScriptValue(), QScriptValueList() << 41 << 1); 28
  • 29. Calling a function as constructor (I) JS: function Point(x, y) { this.x = x; this.y = y; } ... new Point(10, 20); 29
  • 30. Calling a function as constructor (II) C++: QScriptValue p = Point.construct( QScriptValueList() << 10 << 20); qDebug() << p.property(“x”).toNumber(); 30
  • 31. QScriptValueIterator class QScriptValueIterator it(myObject); while (it.hasNext()) { it.next(); qDebug() << it.name(); } 31
  • 32. Working with JavaScript Arrays (I) JS: var a = [10, 20, 30]; print(a[0], a['0']); // same property a.foo = 123; a.push(40); a[a.length] = 'appended'; 32
  • 33. Working with JavaScript Arrays (II) C++: QScriptValue a = engine.evaluate( “[10, 20, 30]”); // array literal qDebug() << a.property(“length”).toInt32(); qDebug() << a.property(“0”).toNumber(); // for convenience + speed qDebug() << a.property(0).toNumber(); 33
  • 34. Error Handling (I) // Uh-oh, this script will throw an error! QScriptValue result = engine.evaluate( “noSuchVariable”); 34
  • 35. Error Handling (II) if (result.isError()) { QString message = result.toString(); // Notify user about the problem ... } 35
  • 36. QScriptValue API Summary • isXXX(), toXXX() • property() and setProperty() • call(), construct() • QScriptValueIterator for introspection 36
  • 37. QtScript tour (the essentials) • Basic embedding (example) • Working with QScriptValues • Configuring the script environment • Qt meta-object system integration 37
  • 38. The Global Object • A built-in script object • Every QScriptEngine has one • QScriptEngine::globalObject() • Initially contains properties defined by ECMA-262 38
  • 39. Tailoring Scripting to Your Application • Design “contract” of C++ <--> script interaction • Set properties of Global Object • Evaluate scripts that use your API • (Optional: Call back into script objects as appropriate) 39
  • 40. Example: Meaning of Life Environment C++: QScriptEngine engine; QScriptValue global = engine.globalObject(); global.setProperty(“meaningOfLife”, 42, QScriptValue::ReadOnly); JS: var myMeaningOfLife = Math.sqrt(meaningOfLife); 40
  • 41. Example: Fake Navigator Object C++: QScriptValue navigator = engine.newObject(); navigator.setProperty(“appVersion”, 1); navigator.setProperty(“appMinorVersion”, 1); navigator.setProperty(“cookieEnabled”, false); navigator.setProperty(“browserLanguage”, “en_US”); navigator.setProperty(“platform”, “No Such OS”); navigator.setProperty(“userAgent”, “007”); global.setProperty(“navigator”, navigator); 41
  • 42. QtScript tour (the essentials) • Basic embedding (example) • Working with QScriptValues • Configuring the script environment • Qt meta-object system integration 42
  • 43. Qt Meta-Object System • In Qt, QObject-based classes can be introspected at runtime • Properties • Signals & slots • QObject::connect() • Qt Designer property editor • ... 43
  • 44. Relax! Leave it to magic 44
  • 45. QtScript <--> QObject Integration (I) • QScriptEngine::newQObject(QObject*) • Returns a script object that acts as proxy • Proxy delegates property access to QObject::property() and QObject::setProperty() 45
  • 46. QtScript <--> QObject Integration (II) • Proxy provides signals and slots as properties • Proxy provides named child objects as properties 46
  • 47. QtScript <--> QObject Integration (III) • Ownership control • Customize proxy behavior • Don't expose child objects • Don't expose super class contents 47
  • 48. Example: Scripting a QLineEdit 48
  • 49. qScriptConnect() function • Connect a signal to a script function • Let scripts define signal handlers • Application does the “plumbing” 49
  • 50. Summary of “API essentials” • QScriptValue • QScriptEngine • evaluate() • globalObject() • newQObject() • qScriptConnect() 50
  • 51. Agenda • What is application scripting? • QtScript tour (the essentials) • Debugger • Misc. 51
  • 52. The QtScript Debugger • Graphical debugger • Introduced in Qt 4.5 • To make the API available in your application: QT += scripttools #include <QtScriptTools> 52
  • 53. Debugger Demo Look at the pretty little demo! 53
  • 54. The QtScript Debugger API (I) One class in public API: QScriptEngineDebugger QScriptEngine engine; QScriptEngineDebugger debugger; debugger.attachTo(&engine); // evaluate scripts ... 54
  • 55. The QtScript Debugger API (II) • Debugger actions can be programmatically triggered • Individual debugger widgets are accessible 55
  • 56. Invoking the Debugger from a Script • Use the debugger keyword • Becomes no-op if no debugger is attached JS: var a = Math.random(); debugger; // synchronously run debugger var b = Math.random(); debugger; return a + b; 56
  • 57. Agenda • What is application scripting? • QtScript tour (the essentials) • Debugger • Misc. 57
  • 60. Prototype-based Inheritance: Why care? • Not every QObject member is a property / signal / slot • Not everything is a QObject • Not everything should/needs to be a QObject • JS developers are familiar with prototypes 60
  • 61. Prototype-based Inheritance: How? • Create a prototype object for the C++ type • Register the type with Qt's meta-type system • Associate prototype object with meta-type ID • Create QScriptValues from the C++ type (qScriptValueFromValue()) 61
  • 62. Bindings for Qt's APIs (I) • Available as stand-alone project • qtscriptgenerator on Qt Labs (http://labs.qt.nokia.com) 62
  • 63. Bindings for Qt's APIs (II) • Creates plugins that can be imported into a script engine • Provides bindings for most of Qt's classes • Makes it possible to write pure script applications 63
  • 64. Summary • It's easy to make your Qt application scriptable with QtScript • There's a QtScript debugger (also part of Qt) • You can achieve a lot by mastering a small API 64