PageStack and communicating between pages



  • 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?



  • 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.



  • 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.



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

    }
    @



  • 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.



  • 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".



  • 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.



  • 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.



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



  • 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.



  • 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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.