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. accessing Component from multiple other Components
Qt 6.11 is out! See what's new in the release blog

accessing Component from multiple other Components

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 4 Posters 1.0k Views 1 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    I have a Component:

    Component {
        id: activitySelector
        ScheduleActivitySelector { // in ScheduleActivitySelector.qml
            onSaveClicked: (activity) => {
                               if (activity) {
                               // do some stuff
                           }
        }
    }
    

    I need to push this component onto a StackView from multiple other Components that I've defined. How do I do this?

    Thanks...

    dheerendraD 1 Reply Last reply
    0
    • mzimmersM mzimmers

      @Bob64 said in accessing Component from multiple other Components:

      is it the same instance that you need to access, or do you need to instantiate it multiple times for each of the client components?

      I think it can be the same instance. There never will be a case when both clients are active at the same time.

      I did a little more reading, and found that this helps:

      var scheduleActivitySelector = Qt.createComponent("ScheduleActivitySelector.qml")
      if (scheduleActivitySelector.status === Component.Ready) {
          scheduleSideDrawerStackView.push(scheduleActivitySelector)
      }
      

      Now I have to figure out how to connect to a signal from the Component.

      EDIT:

      I tried this:

      ColumnLayout {
          property var scheduleActivitySelector: Qt.createComponent("ScheduleActivitySelector.qml")
      
          Button {
              onClicked: {
                  scheduleSideDrawer.open()
                  // scheduleActivitySelector = Qt.createComponent("ScheduleActivitySelector.qml")
                  if (scheduleActivitySelector.status === Component.Ready) {
                      scheduleSideDrawerStackView.push(scheduleActivitySelector)
                  }
              }
              Connections {
                  target: scheduleActivitySelector
                  function onSaveClicked (activity) {
                      // do stuff
                 }
              }
          }
      

      But I get a run-time error:
      QML Connections: Detected function "onSaveClicked" in Connections element. This is probably intended to be a signal handler but no signal of the target matches the name.

      The signal is definitely in my component; any ideas what I'm doing wrong here?

      Thanks...

      GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #6

      @mzimmers Qt.createComponent retuns a Component and not an actual object instance.

      Alternatively StackView.push can take an url so you can just do scheduleSideDrawerStackView.push("ScheduleActivitySelector.qml")

      StackView.push returns the item created, you can use that as the target of your Connections

      mzimmersM 1 Reply Last reply
      1
      • mzimmersM mzimmers

        Hi all -

        I have a Component:

        Component {
            id: activitySelector
            ScheduleActivitySelector { // in ScheduleActivitySelector.qml
                onSaveClicked: (activity) => {
                                   if (activity) {
                                   // do some stuff
                               }
            }
        }
        

        I need to push this component onto a StackView from multiple other Components that I've defined. How do I do this?

        Thanks...

        dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
        wrote on last edited by
        #2

        @mzimmers

        Better you define this on different qml file as external component & access it from everywhere.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        https://www.pthinks.com

        mzimmersM 1 Reply Last reply
        1
        • dheerendraD dheerendra

          @mzimmers

          Better you define this on different qml file as external component & access it from everywhere.

          mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #3

          @dheerendra how do I code that? I tried this:

          scheduleSideDrawerStackView.push(ScheduleActivitySelector)
          

          but that produces this error:

          QML StackView: push: nothing to push
          
          B 1 Reply Last reply
          0
          • mzimmersM mzimmers

            @dheerendra how do I code that? I tried this:

            scheduleSideDrawerStackView.push(ScheduleActivitySelector)
            

            but that produces this error:

            QML StackView: push: nothing to push
            
            B Offline
            B Offline
            Bob64
            wrote on last edited by Bob64
            #4

            @mzimmers is it the same instance that you need to access, or do you need to instantiate it multiple times for each of the client components?

            (The error you mentioned there is that you are pushing the name of the component and not the id of an instance of the component.)

            mzimmersM 1 Reply Last reply
            1
            • B Bob64

              @mzimmers is it the same instance that you need to access, or do you need to instantiate it multiple times for each of the client components?

              (The error you mentioned there is that you are pushing the name of the component and not the id of an instance of the component.)

              mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by mzimmers
              #5

              @Bob64 said in accessing Component from multiple other Components:

              is it the same instance that you need to access, or do you need to instantiate it multiple times for each of the client components?

              I think it can be the same instance. There never will be a case when both clients are active at the same time.

              I did a little more reading, and found that this helps:

              var scheduleActivitySelector = Qt.createComponent("ScheduleActivitySelector.qml")
              if (scheduleActivitySelector.status === Component.Ready) {
                  scheduleSideDrawerStackView.push(scheduleActivitySelector)
              }
              

              Now I have to figure out how to connect to a signal from the Component.

              EDIT:

              I tried this:

              ColumnLayout {
                  property var scheduleActivitySelector: Qt.createComponent("ScheduleActivitySelector.qml")
              
                  Button {
                      onClicked: {
                          scheduleSideDrawer.open()
                          // scheduleActivitySelector = Qt.createComponent("ScheduleActivitySelector.qml")
                          if (scheduleActivitySelector.status === Component.Ready) {
                              scheduleSideDrawerStackView.push(scheduleActivitySelector)
                          }
                      }
                      Connections {
                          target: scheduleActivitySelector
                          function onSaveClicked (activity) {
                              // do stuff
                         }
                      }
                  }
              

              But I get a run-time error:
              QML Connections: Detected function "onSaveClicked" in Connections element. This is probably intended to be a signal handler but no signal of the target matches the name.

              The signal is definitely in my component; any ideas what I'm doing wrong here?

              Thanks...

              GrecKoG 1 Reply Last reply
              0
              • mzimmersM mzimmers

                @Bob64 said in accessing Component from multiple other Components:

                is it the same instance that you need to access, or do you need to instantiate it multiple times for each of the client components?

                I think it can be the same instance. There never will be a case when both clients are active at the same time.

                I did a little more reading, and found that this helps:

                var scheduleActivitySelector = Qt.createComponent("ScheduleActivitySelector.qml")
                if (scheduleActivitySelector.status === Component.Ready) {
                    scheduleSideDrawerStackView.push(scheduleActivitySelector)
                }
                

                Now I have to figure out how to connect to a signal from the Component.

                EDIT:

                I tried this:

                ColumnLayout {
                    property var scheduleActivitySelector: Qt.createComponent("ScheduleActivitySelector.qml")
                
                    Button {
                        onClicked: {
                            scheduleSideDrawer.open()
                            // scheduleActivitySelector = Qt.createComponent("ScheduleActivitySelector.qml")
                            if (scheduleActivitySelector.status === Component.Ready) {
                                scheduleSideDrawerStackView.push(scheduleActivitySelector)
                            }
                        }
                        Connections {
                            target: scheduleActivitySelector
                            function onSaveClicked (activity) {
                                // do stuff
                           }
                        }
                    }
                

                But I get a run-time error:
                QML Connections: Detected function "onSaveClicked" in Connections element. This is probably intended to be a signal handler but no signal of the target matches the name.

                The signal is definitely in my component; any ideas what I'm doing wrong here?

                Thanks...

                GrecKoG Offline
                GrecKoG Offline
                GrecKo
                Qt Champions 2018
                wrote on last edited by
                #6

                @mzimmers Qt.createComponent retuns a Component and not an actual object instance.

                Alternatively StackView.push can take an url so you can just do scheduleSideDrawerStackView.push("ScheduleActivitySelector.qml")

                StackView.push returns the item created, you can use that as the target of your Connections

                mzimmersM 1 Reply Last reply
                1
                • GrecKoG GrecKo

                  @mzimmers Qt.createComponent retuns a Component and not an actual object instance.

                  Alternatively StackView.push can take an url so you can just do scheduleSideDrawerStackView.push("ScheduleActivitySelector.qml")

                  StackView.push returns the item created, you can use that as the target of your Connections

                  mzimmersM Offline
                  mzimmersM Offline
                  mzimmers
                  wrote on last edited by
                  #7

                  @GrecKo interesting. Do I need to concern myself with the lifetime of the object returned by the push() function?

                  1 Reply Last reply
                  0
                  • mzimmersM mzimmers has marked this topic as solved on

                  • Login

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