[Solved] Signal & Slot among QML Items / Components
-
Hi,
Without using C++, I need to send a Signal from one QML Items / Components to another QML Items / Components (separate qml files) for changing property of other components. I have used signal from C++ to QML and within QML item (same qml file).
Thank you in advance. -
Here Qmlfile is a component of anotherqmlfile. But, I want to send signal to all components which has the corresponding slot. It is like Broadcasting signal with parameters (sender, receiver, propertyValue) and receiving by some components. But only the receiver it is sent for will change its property. Similar I have done from C++ to QML. Is it possible in Qml itself.
-
@myQtQml How about Connections ?
Example:
import QtQuick 2.4 import QtQuick.Controls 1.2 Item { id: item width: 200 height: 200 signal sendMessage(string msg, int compId) Button { text: "SendMessage" onClicked: sendMessage("hello",1) } Item { id: item1 Connections { target: item onSendMessage: if(compId==1) { console.log("Hello by Comp 1") } } } Item { id: item2 Connections { target: item onSendMessage: if(compId==2) { console.log("Hello by Comp 2") } } } }
-
Some what like this. But all items are in different files. Let me explain.
main.qml
import QtQuick 2.4 Item { id: item width: 200 height: 200 MyText { text: "default text" myString: "text1" } MyText { text: "default text" myString: "text2" } MyButton { text: "Click to change text2" onClicked: sendSignal("text2", "new text") } }
MyText.qml
import QtQuick 2.4 import QtQuick.Controls 1.2 Item { property string myString Text { } // when sendSignal comes if (myText === myString) text = changedText }
MyButton.qml
import QtQuick 2.4 import QtQuick.Controls 1.2 Item { signal sendSignal(string myText, string changedText) Button { } }
Connections will work if id is known.
Similar to MyText there could be some other items which can receive the signal and process it according to the parameters passed. How to send a Signal to multiple Slots? -
So do you require a signal at all ? You can create property instead and bind them to Text Components.
property string text1 property string text2 MyText { text: "default text" myString: text1 } MyText { text: "default text" myString: text2 } MyButton { text: "Click to change text2" onClicked: text2 = "new text" }
-
As I have written before - similar to MyText there could be some other items which can receive the signal and process it according to the parameters passed. Number of these items and their properties are not known as they are dynamically loaded.
What I did from C++ is to create an object which has a signal and made it available to qml by setContextProperty. In the qml I used Connections with target as the same object. Now whenever the signal is sent, all qml who has connected to that object can receive the signal. I want to do similar within qml domain. Is it possible? -
Hi,
I haven't done this myself, but I think the QML connect() method is what you're after.
-
As I have written before - similar to MyText there could be some other items which can receive the signal and process it according to the parameters passed. Number of these items and their properties are not known as they are dynamically loaded.
Even if they are dynamically created you can bind those properties to that of these dynamically created Items.
What I did from C++ is to create an object which has a signal and made it available to qml by setContextProperty. In the qml I used Connections with target as the same object. Now whenever the signal is sent, all qml who has connected to that object can receive the signal. I want to do similar within qml domain. Is it possible?
So here when you set the contextProperty, you know who the sender is and hence could be used in QML.
AFAIK there's no way to use signal and slot without the knowledge of sender whether you use connect or Connections.