2012-05-08 38 views
2

我注意到QML可以通过使用Connections对象接收Python发出的信号。不幸的是,我无法弄清楚如何让该对象接收该信号的参数。PySide无法从QML检索信号参数

我创建了一个最小的测试案例,演示了什么,我想做的事:

min.py

from PySide import QtCore, QtGui, QtDeclarative 
import sys 

# init Qt 
app = QtGui.QApplication(sys.argv) 

# set up the signal 
class Signaller(QtCore.QObject): 
    emitted = QtCore.Signal(str) 

signaller = Signaller() 

# Load the QML 
qt_view = QtDeclarative.QDeclarativeView() 
context = qt_view.rootContext() 
context.setContextProperty('signaller', signaller) 
qt_view.setResizeMode(QtDeclarative.QDeclarativeView.SizeRootObjectToView) 
qt_view.setSource('min.qml') 
qt_view.show() 

# launch the signal 
signaller.emitted.emit("Please display THIS text!") 

# Run! 
app.exec_() 

而且min.qml

import QtQuick 1.0 

Rectangle { 
    width:300; height:100 

    Text { 
     id: display 
     text: "No signal yet detected!" 

     Connections { 
      target: signaller 
      onEmitted: { 
       display.text = "???" //how to get the argument? 
      } 
     } 
    } 
} 

谢谢!

回答

2

从Qt 4.8开始,PySide根本不处理信号参数名称。

但是你可以用命名的参数创建一个QML信号,并使用Javascript你的Python信号连接到它:

import QtQuick 1.0 

Rectangle { 
    width:300; height:100 


    Text { 
     id: display 
     text: "No signal yet detected!" 

     signal reemitted(string text) 
     Component.onCompleted: signaller.emitted.connect(reemitted) 

     onReemitted: { 
      display.text = text;   
     } 
    } 
} 
+0

谢谢!这就是诀窍! –