Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [Solved] PySide: passing data from QML to Python
Forum Updated to NodeBB v4.3 + New Features

[Solved] PySide: passing data from QML to Python

Scheduled Pinned Locked Moved Language Bindings
2 Posts 1 Posters 4.0k Views 1 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.
  • T Offline
    T Offline
    tiredtyrant
    wrote on last edited by
    #1

    I want to send data from QML to Python. The QML has a button, that when clicked will call a slot with a hardcoded string as parameter. This slot should print the string sent from the QML.

    test.py:

    @#!/usr/bin/env python

    import sys
    from PySide import QtCore, QtGui, QtDeclarative

    class Test( QtCore.QObject ):
    def init( self ):
    QtCore.QObject.init(self)

    @QtCore.Slot()
    def printText(self,text):
        print text
    

    class MainWindow( QtDeclarative.QDeclarativeView ):
    def init( self, parent=None ):
    super( MainWindow, self ).init( parent )
    self.setWindowTitle( "Test" )
    self.setSource( QtCore.QUrl.fromLocalFile( './test.qml' ) )
    self.setResizeMode( QtDeclarative.QDeclarativeView.SizeRootObjectToView )

    app = QtGui.QApplication( sys.argv )
    window = MainWindow()
    context = window.rootContext()
    context.setContextProperty("testModel",Test())
    window.show()
    sys.exit( app.exec_() )@

    test.qml:

    @import QtQuick 1.0

    Rectangle {
    width: 200
    height: 200
    color: "white"

    Rectangle {
        anchors.centerIn: parent
        width: 100
        height: 50
        color: "black"
        Text {
            anchors.centerIn: parent
            text: "click"
            color: "white"
        }
        MouseArea {
            anchors.fill: parent
            onClicked: {
                testModel.printText("test")
            }
        }
    }
    

    }@

    When I click the button, the following error occurs:

    @TypeError: printText() takes exactly 2 arguments (1 given)@

    What am I doing wrong?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tiredtyrant
      wrote on last edited by
      #2

      I figured it out. I forgot to specify the slot's argument type. Fixed it by changing the slot's declaration to this:

      @@QtCore.Slot('QString')
      def printText(self,text):
      print text@

      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