Solved From python to qml
-
@Akhou said in From python to qml:
I don't know what to do :/ !
So you won't do anything? :-(
If a template engine is too much, why don't you try with string.Template? have you look at the example I suggested? Have you look for more examples?
-
@Pablo-J.-Rogina said in From python to qml:
string.Template
I have to do something, but genshi seems to be hard and there isn't alot of examples online, so I'll see what can I do with string.template
Thank u
-
But is string.Template able to generate a .qml file??
-
A qml file is just a text file. So you can write your "string rendered template" in it and be done with it.
-
so for this example
import string v={'var1':'screen'} x=string.Template(""" { id :$var1 width :22 height :123 """) print'Item: ', x.substitute(v) y=""" { id :$sm1 x :11 y :8 width :12 height :65 } } """ print 'Rectangle ' , y%v
I wanted to add to it "string rendered template" but I couldn't figure where to place it exactly, I tried to work with this example :
from flask import Flask, render_template from flask import request, jsonify app = Flask(__name__) @app.route('/', methods=['GET', 'POST']) def homepage(): if request.method == 'POST': f1 = request.form['firstVerb'] f2 = request.form['secondVerb'] return render_template('index.html', f1=f1, f2=f2) if __name__ == "__main__": app.run(debug=True)
but I keep getting this message :
- Restarting with stat
Could u pls show me how to integrated rendered template to my example!
Thank u ^^ -
Are you trying to render QML through a web browser ?
-
Flask already uses jinja for templating as shown in their tutorial.
-
no, I need to generate a .qml file from my template( that I mentioned in the previous text), but it's just an example that I found online, I wanted to try it, but I kept having this message : Restarting with stat
So I'm kinda out of ideas! -
This is what I came with till now:
if shape == "rectangle": x,y,w,h = cv2.boundingRect(c) cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),0) from string import Template from string import Template def main(): cordinates=[] cordinates.append(dict(abscisse=x,ordonee=y,width=w,height=h)) t=Template(""" x: $abscisse y: $ordonee w: $width h: $height """) print ' Rectangle:' print(" {") for data in cordinates: print (t.substitute(data)) print(" }") if __name__=="__main__": main()
and from this template I need to generate a .qml file!
any ideas!! -
@Akhou the idea is to have a text file as template using QML syntax and some placeholders, and then process that file filling such variables with the results of your calculations. This is a basic example that will create a main.qml file:
import string if __name__ == '__main__': # Open template file and pass string to 'data'. # Will be in QML syntax except with the string.Template replace tags # with the format of '$var'. The 'var' MUST correspond to the items # that will be calculated (i.e. coordinates, sizes, ids, etc.) with open('qml_template.txt', 'r') as my_template: data = my_template.read() # Print template for visual cue. print('Template loaded:') print(data) # Pass 'data' to string.Template object data_template. data_template = string.Template(data) # Do the process for shape detection and coordinate calculation # ... # For the sake of simplicity, here values are stored in a dictionary values = dict(x=10, y=20, w=200, h=100) # Open QML output file and fill its contents by string substitution with open("main.qml", 'w') as output_file: # Run string.Template substitution on data_template # using data from 'values' as source and write to 'output_file'. output_file.write(data_template.substitute(values)) output_file.close() # Print QML generated code for visual cue. with open('main.qml', 'r') as my_qml: qml_code = my_qml.read() print('QML code generated:') print(qml_code)
qml_template.txt
import QtQuick 2.2 Item { id: it width: 600 height: 480 Rectangle { id: rect x: $x y: $y width: $w height: $h } }
and the rest is magic...
-
I can't thank u enough for ur help
Thank u so much -
@Akhou well, at least you could:
Upvote the answer(s) that helped you to solve the issue
Use the "Topic Tools" button to mark your post as Solved:-)