mardi 14 mai 2019

How can one efficiently present arbitrary data with Qt when using QML?

Coming from C# and WPF worlds, I am now learning Qt framework.

For the lesson I am writing a mini Explorer-like where a selection should be previewable in a preview pane, just like Windows Explorer:

enter image description here

In WPF, one would typically achieve this using DataTemplate and ItemTemplateSelector, where (roughly) for the former there would be as many DataTemplate as there are formats to preview; the latter being in charge to fetch the appropriate DataTemplate according the type of object being bound. So basically you have UI-code that is terse and relatively clean.

What I am not looking for:

A heavily-based code-behind solution such as:

  • when selection has changed, emit a signal
  • in the slot receiving that signal
    • delete the relevant parts in UI
    • generate UI by code and insert it manually in tree (cumbersome, no design-time preview)

i.e. the typical bad approach one should avoid doing in WPF for instance

What I am looking for:

How would one typically achieve this under Qt?

  • I was thinking of writing custom UI templates, like an image viewer, a sound player and so on
  • then, whenever it's time for the software to preview another file type, update the UI accordingly by 'inserting' that template in the UI tree by any means provided by the framework
  • etc, leverage the most possible out of QML

If people well versed in Qt see a no go in such approach, I'd like to know how they'd typically achieve that. I am not necessarily asking for a code sample, rather, Qt concepts or relevant documentation one should read to realize such thing in the most efficient way.

(let me know if, and how could the question be improved if needed)

Aucun commentaire:

Enregistrer un commentaire