Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. From python to qml
Forum Updated to NodeBB v4.3 + New Features

From python to qml

Scheduled Pinned Locked Moved Solved QML and Qt Quick
20 Posts 3 Posters 6.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Pablo J. RoginaP Offline
    Pablo J. RoginaP Offline
    Pablo J. Rogina
    wrote on last edited by
    #4

    @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?

    Upvote the answer(s) that helped you solve the issue
    Use "Topic Tools" button to mark your post as Solved
    Add screenshots via postimage.org
    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #5

      Are you thinking about using PyQt ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Akhou
        wrote on last edited by
        #6

        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 : 294

        The cordinations of the circle are :
        cX : 267
        cY : 148

        The cordinations of the triangle are :
        x : 432
        y : 38
        width : 343
        height : 190

        The cordinations of the triangle are :
        x : 320
        y : 0
        width : 108
        height : 154

        I want ti send the triangle cordination to the template!
        I don't know if I can do it via python or not

        Thank u for ur help ^^

        1 Reply Last reply
        0
        • Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by Pablo J. Rogina
          #7

          @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.

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Akhou
            wrote on last edited by
            #8

            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 :/ !

            Pablo J. RoginaP 1 Reply Last reply
            0
            • A Akhou

              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 :/ !

              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on last edited by
              #9

              @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?

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Akhou
                wrote on last edited by
                #10

                @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

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Akhou
                  wrote on last edited by
                  #11

                  But is string.Template able to generate a .qml file??

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    A qml file is just a text file. So you can write your "string rendered template" in it and be done with it.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      Akhou
                      wrote on last edited by
                      #13

                      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 ^^

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #14

                        Are you trying to render QML through a web browser ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #15

                          Flask already uses jinja for templating as shown in their tutorial.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            Akhou
                            wrote on last edited by Akhou
                            #16

                            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!

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              Akhou
                              wrote on last edited by Akhou
                              #17

                              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!!

                              1 Reply Last reply
                              0
                              • Pablo J. RoginaP Offline
                                Pablo J. RoginaP Offline
                                Pablo J. Rogina
                                wrote on last edited by
                                #18

                                @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:

                                template.py

                                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...

                                Upvote the answer(s) that helped you solve the issue
                                Use "Topic Tools" button to mark your post as Solved
                                Add screenshots via postimage.org
                                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                                1 Reply Last reply
                                2
                                • A Offline
                                  A Offline
                                  Akhou
                                  wrote on last edited by
                                  #19

                                  I can't thank u enough for ur help
                                  Thank u so much

                                  1 Reply Last reply
                                  0
                                  • Pablo J. RoginaP Offline
                                    Pablo J. RoginaP Offline
                                    Pablo J. Rogina
                                    wrote on last edited by
                                    #20

                                    @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

                                    :-)

                                    Upvote the answer(s) that helped you solve the issue
                                    Use "Topic Tools" button to mark your post as Solved
                                    Add screenshots via postimage.org
                                    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved