createQmlObject using comments
-
Hi All,
I'm trying to create dynamic components using Qt.createQmlObject
Qt.createQmlObject(text, contentRect, "dynamicItem")
However, the text needs to be single line.
'import QtQuick 2.0;Rectangle{ width: parent.width; contentForReplace}'
The contentForReplace is replaced dynamically to create different components at runtime
Is it possible to have // or /* comments */ in the single line?
Kindly advice
-
However, the text needs to be single line.
No. Why? Doesn't matter till it is a valid string.
The contentForReplace is replaced dynamically to create different components at runtime
Do you mean
contentForReplace
is some sort of variable which will append string to that exsiting string ?Is it possible to have // or /* comments */ in the single line?
Yes. Until it stays a valid string.
-
@p3c0 thanks for the feedback.
In the example for "object createQmlObject(string qml, object parent, string filepath)", it's mentioned as
var newObject = Qt.createQmlObject('import QtQuick 2.0; Rectangle {color: "red"; width: 20; height: 20}', parentItem, "dynamicSnippet1");
Where the string qml is a single line, where each line is seperated with a semi-colon
contentForReplace can be replaced with another qml Rectangle object or qml TextEdit object, etc..
If my contentForReplace has "//" , the entire string becomes as comment and the error reported is
"qrc:/qml/main.qml:146: Error: Qt.createQmlObject(): failed to create object:
qrc:/qml/dynamicItem:1:200: Expected token `}'"If my contentForReplace has "/* Test comment */", it fails to create an object with the following error
"qrc:/qml/main.qml:146: Error: Qt.createQmlObject(): failed to create object:
qrc:/qml/dynamicItem:1:200: Unexpected token `;'"Kindly advice
-
Where the string qml is a single line, where each line is seperated with a semi-colon
Yes. Multiple properties on same line are separated by a semicolon.
If my contentForReplace has "//" , the entire string becomes as comment and the error reported is ...
That is because the parser could not find the end of a comment. If you are using a single line use
/* */
for commenting.Also it doesnt have to be in single line compulsarily. Its that it just saves some lines.
You can also write it is:var newObject = Qt.createQmlObject("import QtQuick 2.0 Rectangle { color: \"red\" width: 20 height: 20 }", parentItem, "dynamicSnippet1");
This is more easily understandable.
-
Thanks @p3c0
I have a QML TextArea, where a user can type the qml to be shown.
To display this qml written using createQmlObject, I have to run the following checks
-
replace \n with ;
-
replace \t with empty char
The problem I am facing if a user types /* */ in the qml to be displayed, I need to parse it and find the first and last occurence which can be tedious
Kindly advice
-