QML, Python, Signals, and Slots Part 2

Eric Mesa ericsbinaryworld at gmail.com
Mon Jun 25 00:02:02 UTC 2012


On Sun, Jun 24, 2012 at 4:33 PM, Viranch Mehta <viranch.mehta at gmail.com>wrote:

>
>
> On Mon, Jun 25, 2012 at 12:52 AM, Eric Mesa <ericsbinaryworld at gmail.com>wrote:
>
>> 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.
>>
>
> say you have mySignal(string foo) in QML. this is roughly what you'd do in
> python:
>
> view = QDeclarativeView()
> view.setSource(...)
> connect(view.rootObject(), SIGNAL(mySignal(QString)), receiver, SLOT(...))
>
> I'm assuming the mySignal is in QML's root object. Hope this helps.
>
> Viranch
>
>

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"

So to simplify things to make sure I am getting the concept I put them in
this toy program:

app.py:
#need to use a SLOTT in this file to catch the signal emitted from QML

import sys

from PyQt4 import QtCore
from PyQt4.QtCore import QDateTime, QObject, QUrl, pyqtSignal
from PyQt4.QtGui import QApplication
from PyQt4.QtDeclarative import QDeclarativeView


# This class will emit the current date and time as a signal when
# requested.
class Now(QObject):

    now = pyqtSignal(int)

    def emit_now(self):
        number = 1
        self.now.emit(number)


app = QApplication(sys.argv)

now = Now()

# Create the QML user interface.
view = QDeclarativeView()
view.setSource(QUrl('message.qml'))
view.setResizeMode(QDeclarativeView.SizeRootObjectToView)

# Get the root object of the user interface.  It defines a
# 'messageRequired' signal and JavaScript 'updateMessage' function.  Both
# can be accessed transparently from Python.
rootObject = view.rootObject()

# Provide the current date and time when requested by the user interface.
rootObject.messageRequired.connect(now.emit_now)

# Update the user interface with the current date and time.
now.now.connect(rootObject.updateMessage)

# Provide an initial message as a prompt.
rootObject.updateMessage("Click to Change Colour")

print connect(view.rootObject(),SIGNAL(mySignal(QString)), receiver,
SLOT(QUrl('message.qml')))
# Display the user interface and allow the user to interact with it.
view.setGeometry(100, 100, 400, 240)
view.show()

app.exec_()

message.qml:

import Qt 4.7

Rectangle {
    signal messageRequired;

    function updateMessage(number) {
        if(number==1)
    {
      Rectangle.color = "white"
      messageText.text = "clicked"
    }
    else
    {
      messageText.text = "default"
    }
    }

    function mySignal(string)
    {
      return 1
    }

    anchors.fill: parent; color: "black"

    Text {
        id: messageText
        anchors.centerIn: parent; color: "white"
    }

    MouseArea {
        anchors.fill: parent
        onClicked: messageRequired()
    }
}


What am I doing wrong in getting it into python's slot?

Thanks again for your help!

--
Eric Mesa
http://about.me/ericmesa
http://www.ericsbinaryworld.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/plasma-devel/attachments/20120624/a77d590e/attachment.html>


More information about the Plasma-devel mailing list