Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt android, new qml window does not work properly

I have this main window:

ApplicationWindow {
    id : mainWindow
    width: 640
    height: 480
    visible: true

    Button{
        text: "go back to form 1"
        onClicked: {
            form2.visible = true;
        }
    }

    SecondForm{
        id: form2
    }
}

The second window is:

Window{
    id: main
    width: 640
    height: 480
    x: 0
    y: 0
    visible: false;
    Button{
        text: "go back to form 1"
        onClicked: {
            main.visible = false;
        }
    }
}

The desktop version is Ok, But in android when I run the application it's behavior is strange! When I click the button in the mainWindow this error occures: W/Qt ( 8903): (null):0 ((null)): QEGLPlatformContext::swapBuffers(): eglError: 12301, this: 0x6b46e7c0 though it seems that the second form is invoked and the main window become inactive. But the second windows is not visible . Although I can't see the from and button inside, When I touch the area that the button is expected to be located apparently it works and the second window goes away then the first window become active again. When I try to back to the mainWindow by clicking the android back button it goes back to the mainWindow and this warning: W/Qt ( 8903): (null):0 ((null)): Can't find surface 2 occures!

  • Could you possibly tell me how can I force qt to show the second window?
  • According to this article, Qt application will only consist of a single activity which can be launched from the Android application. Is that mean that there is no way to have multiple windows? I mean several qml windows with only a single activity.
  • If this is the case. Could you please tell me if there is an alternative to develop an application that at least seems to be multiview from the user's perspective?
  • What's your opinion about removing all the contents of the application window and replace it with content of the second window? If we add some transition it looks like activity transitions in android. But I'm, worry about performance issues.

Thanks for any help

like image 373
a.toraby Avatar asked Mar 10 '15 10:03

a.toraby


1 Answers

It may be entirely possible that on Android you are limited to one window. It makes sense, since essentially every Android app out there is a single window app, even those which are not full screen.

Which is why you get an error trying to create a second window, it is not a problem with forcing Qt to do it, it is a problem with Android not supporting such "desktop centric" UI paradigms. The issue seems to be one with multiple visible surfaces, not that much with multiple windows itself, in Android there is probably a limitation for only one visible surface (which is why the controls work even if not showing - the objects are there in memory), you can have "offscreen" as much as you need, but you will still have to do the work to compose them onto the visible surface, something which doesn't make a lot of sense to do in Qt for the sake of multiple windows - a feature Android doesn't even support.

You have to find a way to compose the content of your multiple windows in a single window, either at the same time or only on user request.

  • you could implement some layout management to split the main window in different areas to accommodate the content from secondary windows, however this way you might find your "work area" shrinking below the acceptable, but if you need all content on the scree simultaneously, that's your only solution, on the bright side, you will be constrained by the display size even if you use multiple windows, unless they are overlapping

  • for content which is occasionally needed you can just put it in different tabs, or have some docked icons to show and hide that content, it will appear on top of the main window, and you can have it either full screen or just partially, use it and then hide it back.

  • last but not least, you could use a stack view, QML comes with one and it even supports animated transitions - you may have to do some extra work to adapt your app to work in a "stack manner", something you should have done from the start, a visual stack is the most commonly used approach for mobile apps which require the opening of multiple dialogs on top of one another.

In this example, I modified your code to use a stack view and return a value from the second form to the first form to illustrate one way you could accomplish that (or you could just use a property):

ApplicationWindow {
    id: main
    width: 640
    height: 480
    visible: true

    StackView {
        id: stack
        anchors.fill: parent
    }

    Component {
        id: form1
        Rectangle {
            width: 640
            height: 480
            color: "lightblue"
            function setText(text) { txt.text = text }
            Column {
                Button {
                    text: "open form2"
                    onClicked: stack.push(form2)
                }
                Text {
                    id: txt
                    text: "text has not been set yet"
                }
            }
        }
    }

    Component {
        id: form2
        Rectangle {
            id: f2
            width: 640
            height: 480
            color: "lightgreen"
            Column {
                TextEdit {
                    id: txt
                    text: "enter text here"
                }

                Button {
                    text: "set text"
                    onClicked: {
                        var prev = stack.get(f2.Stack.index - 1)
                        prev.setText(txt.text)
                        stack.pop()
                    }
                }
            }
        }
    }

    Component.onCompleted: stack.push(form1)
}

Naturally, you can have the forms in different qml files and without the Component, I put it all at once for the sake of clarity. You can also change the default sliding animation without a custom one.

Next time you want to make an app that works both on desktop and mobile devices, stay away from features which are not available on all your targets, this way you will avoid having to go back and find ways to substitute them.

like image 181
dtech Avatar answered Nov 07 '22 18:11

dtech