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