Accessing C++ objects from QML

Let's say that we want to perform a heavy calculation in C++ and access it from our QML calculator. We will choose factorial for this project.

The QML engine is really fast, so you can most likely calculate factorials directly in JavaScript without performance problems. We just use it here as a simple example.

Our goal is to inject our C++ class into the QML engine as a JavaScript object that will be available in our QML files. We will do that exactly like we did it in Chapter 10, Scripting. The main function creates a QQmlApplicationEngine object that inherits QJSEngine, so we have access to the API that is already familiar to us from that chapter. Here, we'll just show how we can apply this knowledge to our ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.