title: How to connect Python and QML with PySide2?
body: <p>I want to write a simple desktop application on Ubuntu and I thought that an easy way was to use Qt with QML as GUI and Python as the language for the logic, since I am somewhat familiar with Python.</p>
  
  <p>Now I am trying for hours to somehow connect the GUI and the logic, but it is not working. 
  I managed the connection QML --> Python but not the other way around. I have Python classes which represent my data model and I added JSON encode and decode functions. So for now there is no SQL database involved. But maybe a direct connection between QML view and some database would make things easier?</p>
  
  <p>So now some code.</p>
  
  <p><strong>QML --> Python</strong></p>
  
  <p>The QML file:</p>
  
  <pre><code>ApplicationWindow {
  
  // main window
  id: mainWindow
  title: qsTr("Test")
  width: 640
  height: 480
  
  signal tmsPrint(string text)
  
  Page {
      id: mainView
  
      ColumnLayout {
          id: mainLayout
  
          Button {
              text: qsTr("Say Hello!")
              onClicked: tmsPrint("Hello!")
          }
      }
  }    
  }
  </code></pre>
  
  <p>Then I have my slots.py:</p>
  
  <pre><code>from PySide2.QtCore import Slot
  
  def connect_slots(win):
      win.tmsPrint.connect(say_hello)
  
  @Slot(str)
  def say_hello(text):
      print(text)
  </code></pre>
  
  <p>And finally my main.py:</p>
  
  <pre><code>import sys
  from controller.slots import connect_slots
  
  from PySide2.QtWidgets import QApplication
  from PySide2.QtQml import QQmlApplicationEngine 
  
  if __name__ == '__main__':
      app = QApplication(sys.argv)
  
      engine = QQmlApplicationEngine()
      engine.load('view/main.qml')
  
      win = engine.rootObjects()[0]
      connect_slots(win)
  
      # show the window
      win.show()
      sys.exit(app.exec_())
  </code></pre>
  
  <p>This works fine and I can print "Hello!". But is this the best way to do it or is it better to create a class with slots and use <code>setContextProperty</code> to be able to call them directly without adding additional signals?</p>
  
  <p><strong>Python --> QML</strong></p>
  
  <p>I cannot get this done. I tried different approaches, but none worked and I also don't know which one is the best to use. What I want to do is for example show a list of objects and offer means to manipulate data in the application etc.</p>
  
  <ol>
  <li>include Javascript:
  I added an additional file <code>application.js</code> with a function just to print something, but it could probably be used to set the context of a text field etc. 
  Then I tried to use QMetaObject and invokeMethod, but just got errors with wrong arguments etc.</li>
  </ol>
  
  <p>Does this approach make any sense? Actually I don't know any javascript, so if it is not necessary, I would rather not use it.</p>
  
  <ol start="2">
  <li><p>ViewModel approach
  I created a file viewmodel.py</p>
  
  <pre><code>from PySide2.QtCore import QStringListModel
  
  class ListModel(QStringListModel):
  
  def __init__(self):
       self.textlines = ['hi', 'ho']
       super().__init__()
  </code></pre></li>
  </ol>
  
  <p>And in the main.py I added:</p>
  
  <pre><code>model = ListModel()
  engine.rootContext().setContextProperty('myModel', model)
  </code></pre>
  
  <p>and the ListView looks like this:</p>
  
  <pre><code>ListView {
              width: 180; height: 200
  
              model: myModel
              delegate: Text {
                  text: model.textlines
              }
          }
  </code></pre>
  
  <p>I get an error "myModel is not defined", but I guess that it can't work anyway, since delegates only take one element and not a list.
  Is this approach a good one? and if yes, how do I make it work?</p>
  
  <ol start="3">
  <li>Is there a totally different approach to manipulate data in a QML view? </li>
  </ol>
  
  <p>I appreciate your help! 
  I know the Qt documentation but I am not happy with it. So maybe I am missing something. But PyQt seems to be way more popular than PySide2 (at least google searches seem to indicate that) and PySide references often use PySide1 or not the QML QtQuick way of doing things...</p>