Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] Signal & Slot among QML Items / Components
Forum Updated to NodeBB v4.3 + New Features

[Solved] Signal & Slot among QML Items / Components

Scheduled Pinned Locked Moved QML and Qt Quick
signalslot
11 Posts 4 Posters 14.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    myQtQml
    wrote on last edited by myQtQml
    #1

    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.

    1 Reply Last reply
    0
    • fecubF Offline
      fecubF Offline
      fecub
      wrote on last edited by p3c0
      #2

      Example:

      [Qmlfile.qml]

      Item {
          Rectangle {
               signal testSignal
          }
      }
      

      [anotherqmlfile.qml]

      Qmlfile {
      ontestSignal:{
          ....
          }
      }
      

      hope this helps
      p.s: sorry i dont the markup for code highlight and format

      p3c0P 1 Reply Last reply
      0
      • fecubF fecub

        Example:

        [Qmlfile.qml]

        Item {
            Rectangle {
                 signal testSignal
            }
        }
        

        [anotherqmlfile.qml]

        Qmlfile {
        ontestSignal:{
            ....
            }
        }
        

        hope this helps
        p.s: sorry i dont the markup for code highlight and format

        p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #3

        @fecub put the code inside ``` (3 backticks)

        157

        1 Reply Last reply
        1
        • M Offline
          M Offline
          myQtQml
          wrote on last edited by
          #4

          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.

          p3c0P 1 Reply Last reply
          0
          • M myQtQml

            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.

            p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by p3c0
            #5

            @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") }
                    }
                }
            }
            

            157

            1 Reply Last reply
            0
            • M Offline
              M Offline
              myQtQml
              wrote on last edited by myQtQml
              #6

              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?

              1 Reply Last reply
              0
              • p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by p3c0
                #7

                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"
                    }
                

                157

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  myQtQml
                  wrote on last edited by myQtQml
                  #8

                  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?

                  p3c0P 1 Reply Last reply
                  0
                  • JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #9

                    Hi,

                    I haven't done this myself, but I think the QML connect() method is what you're after.

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    1 Reply Last reply
                    0
                    • M myQtQml

                      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?

                      p3c0P Offline
                      p3c0P Offline
                      p3c0
                      Moderators
                      wrote on last edited by
                      #10

                      @myQtQml

                      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.

                      157

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        myQtQml
                        wrote on last edited by
                        #11

                        I could connect by changing the target of Connections dynamically in loader.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved