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. PageStack and communicating between pages
Qt 6.11 is out! See what's new in the release blog

PageStack and communicating between pages

Scheduled Pinned Locked Moved QML and Qt Quick
12 Posts 4 Posters 9.3k 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.
  • S Offline
    S Offline
    strekazoid
    wrote on last edited by
    #1

    I'm using PageStack with Pages (one of Symbian components) for my application. I wonder is it possible to communicate events from one page to another?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      srikanth_trulyit
      wrote on last edited by
      #2

      I am not aware of page-page communication. But you can put a central dispatch which will inturn communicate the message. This could be easy if you a C++ controller, such that you send your events to this controller which will in turn broadcasts the event such that all connected pages are notified.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        strekazoid
        wrote on last edited by
        #3

        Is there a way to use same instance of an object on two different pages? I mean, say, I have my engine on Qt C++ side. I declare it to be available in QML:

        qmlRegisterType<Communicator>("Communicator", 1, 0, "Communicator");

        Then I declare it in two different .qml files hoping to get an event. But this doesn't work because there seems to be two different instances of the Communicator, created separately for both .qml files.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          favoritas37
          wrote on last edited by
          #4

          If you want to stick to QML only there is a way around. Not exactly what you are looking for but it will have the same results.

          Lets say you have page A at depth 1 and a page B at depth 2. You want page B to emit a signal for an event and page A to receive it and act accordingly. Since page B has visual parent the page A it can access it's functions by using the id of page A. Using this concept, you can define the signal you want in page A and whenever needed page B will emit it. Thus the signal is emitted and you are able to implement a handler function in page A for that signal regardless from where the signal was emitted. Hence that you need to do the same procedure to all the pages you want to notify.

          A small example:
          A.qml
          @
          Page{
          id: A
          signal buttonPressedAtBPage();
          onButtonPressedAtBPage: console.log("Mouse pressed at B page");
          }
          @

          B.qml
          @
          Page{
          id: B
          MouseArea{
          anchors.fill: parent
          onClicked: {
          A.buttonPressedAtBPage()
          //And goes on according to the pages you wan to notify
          // C.buttonPressedAtBPage()
          // ...
          }
          }
          }
          @

          Main.qml
          @
          Window{
          Component.onCompleted:{
          pageStack.push(Qt.createComponent("A.qml"))
          pageStack.push(Qt.createComponent("B.qml"))
          }

          PageStack {
              id: pageStack
              toolBar: commonToolBar
              anchors.fill: parent
          }
          

          }
          @

          1 Reply Last reply
          0
          • S Offline
            S Offline
            strekazoid
            wrote on last edited by
            #5

            Thanks for the idea. At the moment I'm trying to push pages only one at a time using:

            pageStack.push(Qt.resolvedUrl("StartPage.qml"))

            I think I can't push all pages to the stack at once since my B page contains Camera element, which is a bit too heavy for the whole application. Have to learn about PageStack a bit more.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              strekazoid
              wrote on last edited by
              #6

              Basically at the moment I'm doing exactly as you proposed, with only exception that page A pushes page B to the stack when required. In this case triggering signal from page B to page A using id always results in error "Reference error: can't find variable A".

              1 Reply Last reply
              0
              • S Offline
                S Offline
                strekazoid
                wrote on last edited by
                #7

                Ok, it worked now! I just replaced

                pageStack.push(Qt.resolvedUrl("B.qml"))

                with

                pageStack.push(Qt.createComponent("B.qml"))

                Now I'm able to communicate between page B and A.

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  favoritas37
                  wrote on last edited by
                  #8

                  Glad to hear it works :)

                  Regarding you comments about who is pushing the pages or if they are created all at the same time, this is very flexible. You can push the pages whenever you want and from wherever you want, the result will be the same.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    strekazoid
                    wrote on last edited by
                    #9

                    I wonder if this should work from parent to child page? Trying this now, no luck so far.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      strekazoid
                      wrote on last edited by
                      #10

                      So, the problem right now is that page A creates page B:

                      A.qml:
                      @pageStack.push(Qt.createComponent("B.qml"))@

                      After that I need to propagate events from page A to page B. Defining signals on page B and calling them with page B id doesn't work - I get "can't find variable B" error.

                      Main.qml defines PageStackWindow which is used then in the app.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        strekazoid
                        wrote on last edited by
                        #11

                        Found a partial solution. I'm able to communicate events using pageStack.currentPage.signal() call. This of course will only communicate to the current page, but that should be enough for me.

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on last edited by
                          #12

                          Moderator's note:
                          Please use the edit link of an existing comment if you want to add additional information. Don't post a new comment every time, thanks.

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          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