<div class="gmail_quote">On Sun, Jun 24, 2012 at 4:33 PM, Viranch Mehta <span dir="ltr"><<a href="mailto:viranch.mehta@gmail.com" target="_blank">viranch.mehta@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br><br><div class="gmail_quote"><div class="im">On Mon, Jun 25, 2012 at 12:52 AM, Eric Mesa <span dir="ltr"><<a href="mailto:ericsbinaryworld@gmail.com" target="_blank">ericsbinaryworld@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
OK, I did a bunch of googling and I think I can better word what I want to do. I want to emit a signal in QML and catch it in a python slot. That way it can be the arguments to a function within python. Can I please get some help with this? The syntax is just indirect enough (like the signal and slot have different names and all kinds of weirdness) that it's just slightly out of reach for my brain to wrap itself around.<br>
</blockquote><div><br></div></div><div>say you have mySignal(string foo) in QML. this is roughly what you'd do in python:</div><div><br></div><div>view = QDeclarativeView()</div><div>view.setSource(...)</div><div>connect(view.rootObject(), SIGNAL(mySignal(QString)), receiver, SLOT(...))</div>
<div><br></div><div>I'm assuming the mySignal is in QML's root object. Hope this helps.</div><div><br></div><div>Viranch</div><div> </div></div></blockquote></div><br>Thanks, that's very helpful. Just want to make sure I"m doing the right thing because I"m getting an error "NameError: name 'connect' is not defined"<br>
<br>So to simplify things to make sure I am getting the concept I put them in this toy program:<br><br>app.py:<br>#need to use a SLOTT in this file to catch the signal emitted from QML<br><br>import sys<br><br>from PyQt4 import QtCore<br>
from PyQt4.QtCore import QDateTime, QObject, QUrl, pyqtSignal<br>from PyQt4.QtGui import QApplication<br>from PyQt4.QtDeclarative import QDeclarativeView<br><br><br># This class will emit the current date and time as a signal when<br>
# requested.<br>class Now(QObject):<br><br> now = pyqtSignal(int)<br><br> def emit_now(self):<br> number = 1<br> self.now.emit(number)<br><br><br>app = QApplication(sys.argv)<br><br>now = Now()<br><br>
# Create the QML user interface.<br>view = QDeclarativeView()<br>view.setSource(QUrl('message.qml'))<br>view.setResizeMode(QDeclarativeView.SizeRootObjectToView)<br><br># Get the root object of the user interface. It defines a<br>
# 'messageRequired' signal and JavaScript 'updateMessage' function. Both<br># can be accessed transparently from Python.<br>rootObject = view.rootObject()<br><br># Provide the current date and time when requested by the user interface.<br>
rootObject.messageRequired.connect(now.emit_now)<br><br># Update the user interface with the current date and time.<br>now.now.connect(rootObject.updateMessage)<br><br># Provide an initial message as a prompt.<br>rootObject.updateMessage("Click to Change Colour")<br>
<br>print connect(view.rootObject(),SIGNAL(mySignal(QString)), receiver, SLOT(QUrl('message.qml')))<br># Display the user interface and allow the user to interact with it.<br>view.setGeometry(100, 100, 400, 240)<br>
view.show()<br><br>app.exec_()<br><br>message.qml:<br><br>import Qt 4.7<br><br>Rectangle {<br> signal messageRequired;<br><br> function updateMessage(number) {<br> if(number==1)<br> {<br> Rectangle.color = "white"<br>
messageText.text = "clicked"<br> }<br> else<br> {<br> messageText.text = "default"<br> }<br> }<br> <br> function mySignal(string)<br> {<br> return 1<br> }<br>
<br> anchors.fill: parent; color: "black"<br><br> Text {<br> id: messageText<br> anchors.centerIn: parent; color: "white"<br> }<br><br> MouseArea {<br> anchors.fill: parent<br>
onClicked: messageRequired()<br> }<br>}<br><br><br>What am I doing wrong in getting it into python's slot? <br><br>Thanks again for your help!<br><br>--<br>Eric Mesa<div><a href="http://about.me/ericmesa" target="_blank">http://about.me/ericmesa</a><br>
<a href="http://www.ericsbinaryworld.com" target="_blank">http://www.ericsbinaryworld.com</a></div>