3 ms·
Where it is that you see "magic"? QML is a markup language (just like HTML is for web) which can have inline code in it (JavaScript). These QML files are quite
by j1elo 3y ago
Where it is that you see "magic"? QML is a markup language (just like HTML is for web) which can have inline code in it (JavaScript). These QML files are quite literally embedded into the binary, loaded in-memory at runtime, and processed by a rendering and execution engine. Quite similar to how a web browser would render HTML and execute JS code. In fact, the latter in QML originally was done by V8 itself!
The extra feature of the QML engine wrt. a web browser is to provide callbacks and hooks, doing what I guess we could call FFI, allowing for events and function calls to travel between C++ and JS. Complex, but not much sort of magic in there.
As far as I understand, we would draw a parallel between QML and what Tauri does. It just embeds a whole interpreter into your program, in order to be able to draw the markup and run the scripting.
- saidinesh5 3y agoFor a beginner to Qt (not necessarily programming. not even C++. Just Qt.), most of this tends to be magic though. The build system automatically embeds all the QML files into the program binary. The QObject system transparently adds reflection to C++, using the build system. The QmlEngine that keeps track of properties that are in use and automatically updating the dependent values, and the headaches that come with broken bindings. Not saying it is a big deal to learn or anything, but a lot of this stuff is not what "traditional C++ devs" are used to. I had to teach some new devs "Intro to Qt for C++ progammers", so i got to see this from their perspective.
- j1elo 3y agoBut that's a knowledge issue. Which is fine, if I can say one thing for sure is that none of us was born knowing anything at all :-) C++ programmers would probably be used to lower level kind of technologies, but when talking about this I think it just helps to think about what would you do if you wanted to embed an HTML and JS file into your program and wanted it to work like QML works. For an experienced C++ dev, a list of stuff will naturally start flowing through their mind: * A mechanism to inject the files into the binary upon compilation. * How to interpret the HTML and paint on the screen? We'll need an HTML rendering engine and a surface painter. * About JS, how to run it? Well, maybe time to include V8 into the program. It will take care of running JS code. * Now to make calls between both languages. Probably V8 already provides callbacks because it implements FFI already for you. * And other stuff you mentioned, like a property manager that remembers what has been registered and creates change observers in order to trigger events for C++ to know. Thing is, Qt already gives you all of this done, otherwise their proposal of QML wouldn't have gone too far... These series of articles touch on the topic and can enlighten a bit about some details: * https://www.kdab.com/qml-engine-internals-part-1-qml-file-loading/ https://www.kdab.com/qml-engine-internals-part-1-qml-file-lo... * https://www.kdab.com/qml-engine-internals-part-2-bindings/ https://www.kdab.com/qml-engine-internals-part-2-bindings/ * https://www.kdab.com/qml-engine-internals-part-3-binding-types/ https://www.kdab.com/qml-engine-internals-part-3-binding-typ... * https://www.kdab.com/qml-engine-internals-part-4-custom-parsers/ https://www.kdab.com/qml-engine-internals-part-4-custom-pars...
- photoGrant 3y agoThe reality behind 'Magic' is always a 'Knowledge' issue ;-)
- saidinesh5 3y ago> But that's a knowledge issue. Which is fine, if I can say one thing for sure is that none of us was born knowing anything at all :-) It is just that for them, "non c++" tools ended up doing a lot of this magic work. Plus the idea of "embedding another language or two into a language where i already made UI without any fuss" tended to keep some of the more experienced C++ devs away from QML in my experience. Giving them tasks like animating the UI, writing custom styles etc.. that's where QML converted more C++ devs in my experience. Those kdab blogposts were indeed very helpful for us back then.
- righthand 3y agoNo offense to Qt but if I wanted any of that I’d write my app for a browser. There is very little benefit to write something that works just like a browser with extra overhead when I’m trying to write in C++. That’s usually what people mean by magic, extra complexity that is not apparently beneficial even when partially understood.