PDA

View Full Version : XML to QML converter



mburakalkan
20th April 2011, 10:17
Hello all,

I am working on a project which I use QML to draw my GUI. But somehow I need to generate QML files from XML based solution like this;



<general>
<text name="src_text1" value="this is text 1" />
<text name="src_text2" value="this is text 2" />
<image name="src_image1" location="/opt/cutekit/data/images/qt.png">
<!-- many more slots to come! -->
</general>


to;



Rectangle {
width: 1920
height: 1080
color: "#00000000"

Text {
id: text1
x: 252
y: 574
width: 80
height: 20
text: src_text1
font.pixelSize: 12
opacity: 0
}

Text {
id: text2
x: 492
y: 574
width: 80
height: 20
text: src_text2
font.pixelSize: 12
opacity: 0
}

Image {
id: image1
x: 772
y: 297
width: 100
height: 100
source: src_image1
opacity: 0
}
}



So my question is, do I have to do this manually on runtime(parsing xml, creating qml file), or is there any tool to help me on this one?

high_flyer
21st April 2011, 11:49
What is the use of expressing QML in XML?
Why not use QML directly?
What are you trying to achieve?

mburakalkan
21st April 2011, 14:19
Hello,

I know that sounds stupid and actually that was what I proposed at first , but development head
wants to use generated XML's for more that one project... so they do not want to deal with QML creation and stuff.
so I need to write some tool for that matter I guess.

Added after 15 minutes:

Anyway,

if any other person needs something like this, check that project on gitorious
https://gitorious.org/xml2qml

mcosta
21st April 2011, 17:09
Have you tried with QtXmlPatterns module?

See here for more details.

wysota
21st April 2011, 22:44
I would rather ask why you want to use qml at all. Since you don't want to be bound to qml, you won't be able to use any of qml's declarative features which implies your xml file will only contain a static structure of elements. Generating a ui file (e.g. using xslt) seems more appropriate in this situation.

mburakalkan
22nd April 2011, 09:46
@wysota actually all the application based on graphics/declarative view, application does not contain QWidget based standart Qt widgets, but graphicsitem based stuff (images/text etc.) so in this case I actually want to use to decouple what do draw and when to draw, animation and state support etc.. I don't want to use graphicsview classes to do the all drawing, so basically QML will be my ui file.. see project details at https://gitorious.org/cutekit so in this case that makes sense right?

@mcosta thanks for suggestion!

wysota
22nd April 2011, 11:01
@wysota actually all the application based on graphics/declarative view, application does not contain QWidget based standart Qt widgets, but graphicsitem based stuff (images/text etc.) so in this case I actually want to use to decouple what do draw and when to draw, animation and state support etc.. I don't want to use graphicsview classes to do the all drawing, so basically QML will be my ui file.. see project details at https://gitorious.org/cutekit so in this case that makes sense right?
No, not really. In this situation I would use the xml file directly and build Graphics View objects from it in code. I don't see any advantage from using qml here.

mcosta
22nd April 2011, 11:07
I agree with wysota.

I think QML is usefull for non-programmers ui designers.
I prefer use Graphics View Framework.

mburakalkan
22nd April 2011, 13:51
I guess I need to review application workflow here, maybe using QML is not that advantageous here as you say..

BTW I heard actually using DeclarativeView + QML is more performance sane vs using plain GraphicsView even though
former is based on latter, since they do a lot of optimization drawing with QML(lazy loading, and other stuff etc..) what do you guys
think about that?

wysota
25th April 2011, 12:32
BTW I heard actually using DeclarativeView + QML is more performance sane vs using plain GraphicsView even though
former is based on latter, since they do a lot of optimization drawing with QML(lazy loading, and other stuff etc..) what do you guys
think about that?

You can do the exact same optimizations in your code. Besides there is no point in worrying about optimizations if you're not suffering from efficiency problems.

BreezeUA
10th September 2012, 14:17
Good time of day.
I have (almost) the same problem, but let me describe it in details.
What has to be done: user double-clicks on config file (list of XML files) and a nice GUI magically appears where he can change values of attributes.
The way I see it can be one: transform XML into QML via XSLT and show that, +inform the user if there is unrecognized fields detected (i.e. wrong config structure).
Why bother with transformation? Boss doesn't want the analytical part to be hardcoded with C++, a config template should be "easily" editable on-the-go. XSLT template fits here nicely since it can allow me to check and "easily" modify the config structure. The problem is that meh completely unfamiliar with XSTL and QML -.- soooo
The question: can it all be done in a more elegant way? (I feel like I'm looking for some magical tool that will do all the work instantly, duh :rolleyes: )

(p.s. As alternative to QML I can see only QUiLoader, but QML is imo more pretty)

wysota
10th September 2012, 16:21
You want to make a config editor or does the xml file contain the contents of what you want to display? Either way I don't think QML is a good approach here (unless the XML contains a description of some fancy graphical scene). Parse the XML and generate your UI from it using C++ and widgets.

BreezeUA
11th September 2012, 08:47
I need a config editor (actually it's just a part of an utility that allows to control the modules of an application). Let's suppose I made a DOM parser to read the data directly and placed it all on widgets - plain and easy. But when I need to change the config structure, or the way it is shown on a form I have to modify and recompile my app, right? As I mentioned above it's (unfortunately) not an option, so I need some middle layer of something that will allow me to describe that config logical and visual structure (QT UI file / QML file / ?..) and then transform input config into that something, process it and get some QObject I can work with in result. So when the user (another programmer really) wants to change the visuals/logic he just changes the "script" text, nothing more.

wysota
11th September 2012, 09:02
But when I need to change the config structure, or the way it is shown on a form I have to modify and recompile my app, right?
Possibly yes.


As I mentioned above it's (unfortunately) not an option, so I need some middle layer of something that will allow me to describe that config logical and visual structure (QT UI file / QML file / ?..) and then transform input config into that something, process it and get some QObject I can work with in result.
It's like squaring the circle... What if you need to handle something you have not forseen earlier? Trying to be too smart often backfires. Using XSLT if you don't know it has this significant drawback that you waste a lot of time when trying to do anything complex with it. Besides, a good design is one that does not need changing later :) Spend twice the time you intended to design your xml structure and it will be less probably that you'll need to change it later. By the way, a DOM parser is probably not the best idea unless you make it very generic. QXmlStreamReader would be much more flexible as it practically requires you to write a stateless recursive parser without assuming a pre-defined hierarchy of elements.


So when the user (another programmer really) wants to change the visuals/logic he just changes the "script" text, nothing more.
So implement it using a scripting language, e.g. JavaScript (handled by QtScript) or Python (e.g. using PySide).

BreezeUA
11th September 2012, 09:52
Thanks for ideas! Will dig in that way now