5

QMLからPythonにデータを送信しようとしていますが、エラーが発生します。

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")
            }
        }
    }
}

ボタンがクリックされると、「テスト」が出力されると思っていましたが、代わりに次のエラーが発生します。

TypeError:printText()は正確に2つの引数を取ります(1つ指定)

私は何が欠けていますか?

編集:例をより単純なものに変更しました。

4

1 に答える 1

3

スロットの引数の種類を指定するのを忘れました。printText()の宣言を次のように変更して修正しました。

@QtCore.Slot('QString')
def printText(self,text):
    print text
于 2012-11-17T12:39:38.097 に答える