PDA

View Full Version : Signal and Slots with QML confusion with QML items



technoViking
8th November 2010, 07:24
Hi,

I was wondering if anyone can explain how SIgnals and Slots work in QML.

I understand them in Qt.

So if I have 2 files.
A.qml

B.qml


I want A.qml to send a signal to B.qml

The signal I want A to send to B is a string.

//A.qml



Item
{
id: aItem


signal sendStringSignal

Rectangle
{
onClicked: { //need to send a to B.qml }

}

}



I wasn't able to find code snippets of anyone doing this, any ideas?

Thanks

somnathbanik
8th November 2010, 07:46
Hi,

I was wondering if anyone can explain how SIgnals and Slots work in QML.

I understand them in Qt.

So if I have 2 files.
A.qml

B.qml


I want A.qml to send a signal to B.qml

The signal I want A to send to B is a string.

//A.qml



Item
{
id: aItem


signal sendStringSignal

Rectangle
{
onClicked: { //need to send a to B.qml }

}

}



I wasn't able to find code snippets of anyone doing this, any ideas?

Thanks

Rectangle {

onClicked: { container.clicked(); }
}

technoViking
8th November 2010, 09:44
Thanks so that seems to be sending a clicked() signal but I don't see how you can send a string through that signal and how does the other .qml file accept that signal, aka how does it make a slot to receive the string?

somnathbanik
8th November 2010, 10:40
Thanks so that seems to be sending a clicked() signal but I don't see how you can send a string through that signal and how does the other .qml file accept that signal, aka how does it make a slot to receive the string?

Please refer to the example C:\Qt\4.7.0\demos\declarative\flickr. it has got everything, hope this will help you.

wysota
8th November 2010, 14:01
Thanks so that seems to be sending a clicked() signal but I don't see how you can send a string through that signal and how does the other .qml file accept that signal, aka how does it make a slot to receive the string?

In QtQuick you usually use property binding which is a higher-level mechanism than signals and slots (and it uses signals under the hood in case of C++ code). Most of the times signals are used as notification of some property changes and all you need to do is to follow this pattern - you declare a property in one object and write a statement in the other object that uses the name of the property defined earlier.

Here is a dirty example:
import Qt 4.7

Rectangle {
id: win
width: 200
height: 200

TextInput {
anchors.top: win.top; anchors.left: win.left; anchors.right: win.right;
id: objectA
text: "xyz"
height: 100
}

Text {
id: objectB
anchors.bottom: win.bottom; anchors.left: win.left; anchors.right: win.right;
height: 100
text: "=>"+objectA.text+"<="
}
}

Try changing the text of the first object and see what happens.

Remember QML is a declarative language. You declare text of objectB to be the text of objectA surrounded by arrows.

technoViking
9th November 2010, 02:48
I see, I didn't realize that's the proper way of doing things. It seems like that would make a ton of global objects, and everyone is simply modifying the global objects

wysota
9th November 2010, 11:10
I see, I didn't realize that's the proper way of doing things.
It's not a "proper" way, it's one of the ways that is more declarative than using signals and slots directly (which is imperative).


It seems like that would make a ton of global objects, and everyone is simply modifying the global objects
You don't need to modify any global objects, you can create a local alias to a property of another object. Or maybe I don't understand what you mean by those global objects... hmm...

It would be best if you explained what do you need all this for and maybe then we'll be able to find a solution tailored for the specific situation.

technoViking
10th November 2010, 04:16
Thanks for the response. I will get a better description in a few!