[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-panel-devel
Subject:    Re: QML, Python, Signals, and Slots Part 2
From:       Eric Mesa <ericsbinaryworld () gmail ! com>
Date:       2012-06-25 0:02:02
Message-ID: CAKYHq8+4nTXWnMBMaEtyshgqqBftkkbRREZFEr9c4q9qnS0-Hg () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


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

>
>
> On Mon, Jun 25, 2012 at 12:52 AM, Eric Mesa <ericsbinaryworld@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

[Attachment #5 (text/html)]

<div class="gmail_quote">On Sun, Jun 24, 2012 at 4:33 PM, Viranch Mehta <span \
dir="ltr">&lt;<a href="mailto:viranch.mehta@gmail.com" \
target="_blank">viranch.mehta@gmail.com</a>&gt;</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">&lt;<a \
href="mailto:ericsbinaryworld@gmail.com" \
target="_blank">ericsbinaryworld@gmail.com</a>&gt;</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&#39;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&#39;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&#39;m assuming the mySignal is in QML&#39;s root object. Hope \
this helps.</div><div><br></div><div>Viranch</div><div> \
</div></div></blockquote></div><br>Thanks, that&#39;s very helpful.  Just want to \
make sure I&quot;m doing the right thing because I&quot;m getting an error \
&quot;NameError: name &#39;connect&#39; is not defined&quot;<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(&#39;message.qml&#39;))<br>view.setResizeMode(QDeclarativeView.SizeRootObjectToView)<br><br># \
Get the root object of the user interface.  It defines a<br> # \
&#39;messageRequired&#39; signal and JavaScript &#39;updateMessage&#39; 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(&quot;Click to Change \
Colour&quot;)<br> <br>print connect(view.rootObject(),SIGNAL(mySignal(QString)), \
receiver, SLOT(QUrl(&#39;message.qml&#39;)))<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 = \
&quot;white&quot;<br>  messageText.text = &quot;clicked&quot;<br>    }<br>    \
else<br>    {<br>      messageText.text = &quot;default&quot;<br>    }<br>    }<br>   \
<br>    function mySignal(string)<br>    {<br>      return 1<br>    }<br> <br>    \
anchors.fill: parent; color: &quot;black&quot;<br><br>    Text {<br>        id: \
messageText<br>        anchors.centerIn: parent; color: &quot;white&quot;<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&#39;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>



_______________________________________________
Plasma-devel mailing list
Plasma-devel@kde.org
https://mail.kde.org/mailman/listinfo/plasma-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic