Search code examples
qtqmlqt-creator

How to preview qml documents referencing c++ types?


I frequently use types defined at runtime in c++ in QML documents. It works well, but not with the design view in Qt Creator or with the external preview tools.

For example, in c++:

qmlRegisterType<CustomVideoSource>("MyModule", 1, 0, "CustomVideoSource");

And in QML:

CustomVideoSource { id: customSource }
VideoOutput { source: customSource; anchors.fill: ... }

The "qmlscene" external preview tool quits with the error 'module "MyModule" is not installed'.

The design view is usable as a preview in simple cases, unusable in complex cases, but in any case slow and I can't edit code and see the preview at the same time.

I'm aware of the "dummy context" concept but 1) don't see how it applies in this case and 2) have never had much luck getting it to actually work in other cases when it should.

Does anyone have a good workflow? Maybe I shouldn't be doing things this way at all?

BTW, I'm aware of the Qt forums and I'll probably ask there, too. If I can catch them when they're not down/broken.

Update/clarification:

I'm aware of the options for implementing an extension to QML in C++. My question is not about the mechanics of doing so but about how best to deal with the situation above, e.g., I'd like to register a type at runtime but still have quick previews for UI work.

I'm considering doing a fake plugin purely for preview purposes and passing it via -I to qmlscene. Also modifying qmlscene itself.


Solution

  • This is the strategy I ended up with, which has worked well so far:

    1. To address the "MyModule" issue, I created a qml module with a qml/javascript dummy implementation of MyModule in a "dummyModules" subdirectory. I run the qmlscene preview tool with "-I dummyModules" to make them visible for prototyping. The modules are, of course, not included in release distributions.

    2. I also have several context properties set from C++. To make these work I use the "dummydata" feature of qmlscene.

    3. I modified (hacked) qmlscene to reload the scene on ctrl-r.

    This 1) solves the "MyModule" problem without creating a c++ plugin (impractical in my case) and 2) gives me side-by-side editing and previews.

    I suspect this may work well with the Creator "Design" module, too. IIRC it uses an external program called "qmlpuppet" which is probably similar to qmlscene. But I haven't tried it.