[Interest] Creating Qt Quick controls from C++

André Somers andre at familiesomers.nl
Mon Jul 18 06:44:30 CEST 2016



Op 07/07/2016 om 23:52 schreef Rob Allan:
> I'm part of a team that is looking at migrating an existing Windows 
> C++ app to Qt. The first decision is whether to use Widgets or Qt 
> Quick. Since Qt Quick is newer, shinier, faster, etc, that seems like 
> the obvious choice. However, for reasons that I won't go into here, 
> the vast majority of forms in our app are built dynamically in code. 
> This kind of approach seems easy enough with Widgets, but looks like 
> it could be difficult, or even infeasible, with Qt Quick. That's 
> because the preferred way of creating Qt Quick layouts is using QML, 
> and while there is an object model backing that, my impression is that 
> this object model is not designed to support heavy-duty creation and 
> manipulation of layout elements. For example, while QQuickItem is 
> documented, none of its derived classes are, and I believe their 
> interfaces may be private - so the only way to manipulate items is 
> using a generic "setProperty" syntax. And while there are a few 
> articles around that talk about accessing the Qt Quick model from C++, 
> such as this one 
> [http://stackoverflow.com/questions/31890372/add-objects-to-qml-layout-from-c], 
> they tend to use snippets of QML to get the job done in a rather hacky 
> way, and make various comments about the perils and pitfalls of trying 
> to manipulate the model from code.
>
> I can't help comparing this with two other popular layout frameworks: 
> WPF/XAML, and Android/AXML. In both of these worlds, the markup 
> language and the "code-behind" class hierarchy of UI elements are 
> absolutely equivalent 1st class citizens. Anything you can do in XAML, 
> you can also do in the C# code-behind, whether it be creating 
> controls, changing their properties, altering layouts, etc. Likewise 
> in Android/AXML, I can (if I choose) create FrameLayouts, 
> RelativeLayouts, TextViews, etc in code, and arrange them and 
> manipulate them any way I like, as an alternative to creating an AXML 
> designer layout.
>
> It seems very unfortunate that Qt Quick doesn't take this approach, 
> and that the "code-behind" experience is so limited.
>
> Am I missing something here? Assuming I'm not, are there any plans to 
> make the Qt Quick class model more "open", with full documentation and 
> public interfaces for all relevant properties and methods?
>
No, you are not missing anything here. You are right that there is 
currently no exposed C++ interface for QML that allows you to define 
your UIs like you can from QML. I understand the reasoning for not (yet) 
providing it, but it is indeed an often-requested thing and I would 
apreciate one appearing. AFAIK, the main reason for not having an 
exposed C++ backend is the stability that would enforce. Having only a 
QML backend frees the developers from having a stable C++ backend with 
binary compatibility guarantees. That is quite a burden to have for a 
young technology like QML en Qt Quick.

But of course there are still several supported options available. 
Xavier already mentioned one, but did you consider just generating the 
actual QML on the fly and loading that? That would allow totally dynamic 
forms.

Still, you may want to consider sticking with Qt Widgets. There is 
nothing wrong with that approach. I don't know the needs of your 
application, but most applications consisting of lots of dialogs with 
buttons, line edits, drop-downs, lists, etc. Qt Widgets is just a very 
good fit. It is very mature, stable and has the C++ interface you're 
looking for. Where it does *not* shine is if you want performant 
animations of your UI like you have mobile devices. If that's not what 
you're after, then Qt Widgets may just be a better fit for you.

HTH,

André



More information about the Interest mailing list