Solved From python to qml
-
Hi and welcome to devnet,
Are you thinking about IPC (Inter Process Communication) ?
-
@SGaist said in From python to qml:
(Inter Process Communication)
Hey, Thank u,
I'm not very familiar with that concept, IPC, but if it's gonna make the job done, I'll use it. but first let me explain more my problem.
In fact I have to generate a .qml code automaticatty, so far I've been able to create a .qml template using Acceleo and a shape detection programusing python, that can calculate the cordinations of the shapes in the image.
Now I have to send these cordinations to my template.
But I cant seem to find indications abt that matter online.Thank u for ur help
-
@Akhou could it be possible that you share the QML code or template you have/generate so far?
So you want your Python script that does shape detection and calculates the coordinates of the shape share such values with a QML application? Why don't you write some temporary file in Python with the coordinates so the QML app can read from it? -
Are you thinking about using PyQt ?
-
hey,
sorry for the late reply !
this is template that I've generated in .txt and .qml( I couldn't upload it)import QtQuick 2.2 Item { id: width: height: Rectangle { width: x: y: height: id: } Rectangle { width: x: y: height: id: } Image { id: source: } }
an this is my cordination file(.txt)
The cordinations of the circle are :
cX : 143
cY : 294The cordinations of the circle are :
cX : 267
cY : 148The cordinations of the triangle are :
x : 432
y : 38
width : 343
height : 190The cordinations of the triangle are :
x : 320
y : 0
width : 108
height : 154I want ti send the triangle cordination to the template!
I don't know if I can do it via python or notThank u for ur help ^^
-
@Akhou so for me the issue is just about Python templating, not related to Qt or QML itself.
Not that I'm very proficient with Python, but it looks like you can use simple string.Template or more sophisticated template engines (i.e Genshi).
I'd essentially create a template for the QML layout you want having the proper placeholders for the coordinates, and then the same Python script doing shape detection and calculating coordinates will process the template and provide the actual values, without the need to create any text file.
So in just one pass you'll end up with a QML file with everything set.
-
It seems to be the perfect solution for my problem, but I'm not familiar with Genshi, and unfortunatly the tuto that they provided is far from what I intend to do!
I don't know what to do :/ ! -
@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:-)