How to convert QML to QWidgets .ui XML code?
-
As I am the fan of QML so I want to make a QtCreator plugin so that I may be able to write QWidgets ui in QML like code. QML is readable and takes less real-state.
My question is how can I convert the following QML like code to the following .ui XML code.
My proposed QML like code:QWidget { name: "centralWidget" QPushButton { name: "pushButton" geometry: { x: 10 y: 20 width: 150 height: 50 } text: "Hello World!" } }
.ui XML code:
<widget class="QWidget" name="centralWidget"> <widget class="QPushButton" name="pushButton"> <property name="geometry"> <rect> <x>10</x> <y>20</y> <width>150</width> <height>50</height> </rect> </property> <property name="text"> <string>Hello World!</string> </property> </widget> </widget> </widget>
I would be thankful if someone point me to the right direction or provide me a sample code/library please!
-
Hi
The QML format is very close to JSON syntax.
So if you use a relaxed parser like
https://hjson.org/
that accepts values with no "" then it should be able to parse most of it.
or have a look at
https://github.com/jesperhh/qmlfmt
to see if it can parse enough. ( and/or use it for the plugin to format the QML)
If you need full blown parsing, then you could look at
https://code.woboq.org/qt5/qtdeclarative/src/qml/parser/For saving to UI format, you might have a look at
https://doc.qt.io/qt-5/qabstractformbuilder.html#save
https://doc.qt.io/qt-5/qformbuilder.htmlHowever, thats for saving QWidgets runtime so might not be possible with QML objects.
So you might end up using a XML library.
https://doc.qt.io/qt-5/qtxml-index.html
https://pugixml.org/to construct the UI files your self from the QML tree.
-
Hi,
You might also want to check KDAB's DeclarativeWidgets project.