Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Troubles with switching windows in Android using QtQuick 2.2 and QQuickView
Forum Updated to NodeBB v4.3 + New Features

Troubles with switching windows in Android using QtQuick 2.2 and QQuickView

Scheduled Pinned Locked Moved Mobile and Embedded
8 Posts 2 Posters 2.1k 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.
  • A Offline
    A Offline
    andrew_21_12
    wrote on last edited by
    #1

    Hello!

    Im using QtQuick 2.2 qml-files to load them as sources for apps windows. Here is my main.cpp:
    @
    #include "mainwindow.h"
    #include <QApplication>
    #include <QSplashScreen>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    MainWindow w;
    //w.show();
    
    return a.exec&#40;&#41;;
    

    }
    @

    Then in MainWindow I`m initializing UI-elements. Header:
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QtNetwork>
    #include <QUrl>
    #include <QNetworkAccessManager>
    #include <QtDebug>
    #include <QSettings>
    #include <QJsonObject>
    #include <QJsonDocument>
    #include <QJsonValue>
    #include <QUuid>
    #include <QtQml>
    #include <QtQuick>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget * parent = 0);
    ~MainWindow();

    Q_INVOKABLE void FunctionRequest(); 
    Q_INVOKABLE void FunctionRegister(); 
    

    private:
    QQuickView ui;
    QObject * root;
    QNetworkAccessManager * nam;
    QObject * btnLogin;

    private slots:
    void slotFinished(QNetworkReply*);
    };

    #endif // MAINWINDOW_H
    @

    Cpp:
    @
    #include "mainwindow.h"
    #include "rootscreen.h"
    #include "registerform1.h"
    #include "statics.h"

    MainWindow::
    MainWindow(QWidget * parent)
    : QMainWindow(parent)
    {
    ui.setSource(QUrl("qrc:/main.qml"));
    // setCentralWidget(QWidget::createWindowContainer(&ui));
    ui.setResizeMode(QQuickView::SizeRootObjectToView);
    ui.show();

    root = ui.rootObject();
    ui.rootContext()->setContextProperty("window", this);
    
    nam = new QNetworkAccessManager(this);
    connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(slotFinished(QNetworkReply*)));
    
    btnLogin = root->findChild<QObject*>("btnMainLogin");
    

    }

    void MainWindow::FunctionRequest() {
    QObject * etLogin = root->findChild<QObject*>("etMainLogin");
    QObject * etPassword = root->findChild<QObject*>("etMainPassword");
    btnLogin->setProperty("enabled", "false");

    QString getRequest(Statics::host);
    getRequest.append("/api/login?");
    getRequest.append("login=");
    getRequest.append(etLogin-&gt;property("text").toString());
    getRequest.append("&");
    getRequest.append("password=");
    getRequest.append(etPassword-&gt;property("text").toString());
    getRequest.append("&");
    QSettings settings("settings.ini", QSettings::IniFormat);
    getRequest.append("device=");
    getRequest.append(settings.value("settings/device").toString());
    
    QNetworkRequest request(QUrl(getRequest.toUtf8()));
    QNetworkReply * pnr = nam->get(request);
    

    }

    void MainWindow::FunctionRegister() {
    new RegisterForm1;
    }

    void MainWindow::slotFinished(QNetworkReply * pnr) {
    if (pnr->error() != QNetworkReply::NoError) {
    btnLogin->setProperty("color", "#FFC4C5");
    btnLogin->setProperty("enabled", "true");
    } else {
    btnLogin->setProperty("enabled", "true");
    QString resultString = QString::fromUtf8( pnr->readAll() );
    QJsonDocument result = QJsonDocument::fromJson(resultString.toUtf8());
    QJsonObject mainJson = result.object();
    QJsonValue value = mainJson.value("error");
    if (value.toString() == NULL) {
    mainJson = mainJson.value("done").toObject();
    mainJson = mainJson.value("date").toObject();
    value = mainJson.value("key"); // Парсим JSON с ответом
    QSettings settings("settings.ini", QSettings::IniFormat);

            QString md5device = QString(QCryptographicHash::hash(qPrintable(settings.value("settings/device").toString()), QCryptographicHash::Md5).toHex());
            QString md5response = QString(QCryptographicHash::hash(qPrintable(QString::number(value.toDouble(), 'f', 0)), QCryptographicHash::Md5).toHex());
            QString md5appended = md5device + md5response;
            QString md5total = QString(QCryptographicHash::hash(qPrintable(md5appended), QCryptographicHash::Md5).toHex());
    
            settings.setValue("settings/key", md5total);
            settings.sync();
    
            new RootScreen();
        } else {
            btnLogin->setProperty("color", "#FFC4C5");
        }
    }
    pnr->deleteLater();
    

    }

    MainWindow::~MainWindow()
    {
    delete nam;
    delete root;
    }
    @

    But when new window opens (new RegisterForm1), it doesnt render on the screen, and I can see only current window. If well turn off and then turn on screen new window normally appears. It also works while its invisible (TextInputs), but we cant see window itself.

    I tried to set QQuickView ui as a central widget and call QMainWindow`s show() function, but then display becomes simply gray and nothing works.

    How can I fix this strange behavior on Android devices (on Windows everything works great)? I found exactly same problem`s description as mine in bug report: https://bugreports.qt-project.org/browse/QTBUG-39454 . But there were no any solutions, that I could implement.

    p.s. sorry, if my English is very bad(

    1 Reply Last reply
    0
    • GianlucaG Offline
      GianlucaG Offline
      Gianluca
      wrote on last edited by
      #2

      I don't see any reason in your code to embed a QtQuick view inside a QMainWindow.
      So, if there is no special reason for doing that, then do not use QMainWindow and directly show the QtQuick on the screen.
      Maybe, in this way you bypass the problem.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andrew_21_12
        wrote on last edited by
        #3

        Gianluca, thank you so much for your answer. I tried solution, that you recommended, but it didn`t help. New screens are invisible still. Also tried to create new QQuickViews in main() method of the application - no result. Terrible frustration(

        1 Reply Last reply
        0
        • GianlucaG Offline
          GianlucaG Offline
          Gianluca
          wrote on last edited by
          #4

          Please, can you post also the qml code?? So, we can see if there is something wrong there.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andrew_21_12
            wrote on last edited by
            #5

            Of course

            main.qml:
            @
            import QtQuick 2.2

            Rectangle {
            width: 320
            height: 600
            color: "#F0F3F6"
            Rectangle {
            anchors.centerIn: parent
            width: parent.width * 2
            height: parent.height * 0.45
            color:"#FFFFFF"
            rotation: -20
            Rectangle {
            anchors.centerIn: parent
            width: parent.width
            height: parent.height * 0.95
            color:"#3ABDED"
            }
            }
            Text {
            anchors.fill: parent
            text: "MOBILE\nAQUA"
            color: "#FFFFFF"
            font.bold: true
            font.pixelSize: parent.height * 0.047
            font.letterSpacing: parent.height * 0.0025
            font.family: "Leelawadee"
            horizontalAlignment: Text.AlignHCenter
            anchors.leftMargin: parent.width * 0.17
            verticalAlignment: Text.AlignVCenter
            }
            Image {
            source: "aqua.png"
            x:parent.width * 0.5 - width * 2
            y:parent.height * 0.5 - height / 2
            height: parent.height * 0.115
            width: height / 1.4375
            }
            MouseArea {
            anchors.fill: parent
            onClicked: {
            mainScreen.visible = true;
            }
            }

            Rectangle {
                id: mainScreen
                anchors.fill: parent
                color: "#F0F3F6"
                visible: false
            EditText {
                id: etLogin
                anchors.horizontalCenter: parent.horizontalCenter
                y: parent.height * 0.4
                EditText_get_setText {
                    id: etLoginText
                    objectName: "etMainLogin"
                    text: "+7 (xxx) xxx xx xx"
                    inputMethodHints: Qt.ImhFormattedNumbersOnly
                    validator: RegExpValidator {
                        regExp: /^((8|\+7)[\-]?)?(\(?\d{3}\)?[\-]?)?[\d\-]{7,10}$/
                    }
                    onActiveFocusChanged: {
                        if (etLoginText.focus == false) {
                            if (etLoginText.text == "") {
                                etLoginText.text = "+7 (xxx) xxx xx xx"
                                etLoginText.color = "#F0F3F6"
                            }
                        } else {
                            etLoginText.color = "#000000"
                        }
                    }
                    Keys.onPressed: {
                        if (event.key == Qt.Key_Return || event.key == Qt.Key_Enter) {
                            etLogin.focus = false;
                            etPasswordText.focus = true;
                            etPasswordText.color = "#000000"
                            if (etPasswordText.text == "**********") {
                                etPasswordText.text = "";
                            }
                            event.accepted = true;
                        }
                    }
                    EditText_onClick {
                        onClicked: {
                            if (etLoginText.text == "+7 (xxx) xxx xx xx") {
                                etLoginText.text = "";
                            }
                        }
                    }
                }
            }
            
            EditText {
                id: etPassword
                anchors.horizontalCenter: parent.horizontalCenter
                y: parent.height * 0.515
                EditText_get_setText {
                    id: etPasswordText
                    objectName: "etMainPassword"
                    text: "**********"
                    echoMode: TextInput.Password
                    inputMethodHints: Qt.ImhPreferLatin
                    validator: RegExpValidator {
                        regExp: /[a-zA-Z0-9]{1,32}/
                    }
                    Keys.onPressed: {
                        if (event.key == Qt.Key_Return || event.key == Qt.Key_Enter) {
                            etPasswordText.focus = false;
                            Qt.inputMethod.hide();
                        }
                    }
                    onActiveFocusChanged: {
                        if (etPasswordText.focus == false) {
                            if (etPasswordText.text == "") {
                                etPasswordText.text = "**********"
                                etPasswordText.color = "#F0F3F6"
                            }
                        } else {
                            etPasswordText.color = "#000000"
                        }
                    }
                    EditText_onClick {
                        onClicked: {
                            if (etPasswordText.text == "**********") {
                                etPasswordText.text = "";
                            }
                        }
                    }
                }
            }
            
            Label {
                text: "АВТОРИЗАЦИЯ"
                anchors.horizontalCenter: parent.horizontalCenter
                y: parent.height * 0.165
            }
            
            Link {
                text: "Зарегистрироваться?"
                anchors.horizontalCenter: parent.horizontalCenter
                y: parent.height * 0.69
            }
            
            Button {
                id: btnLogin
                objectName: "btnMainLogin"
                anchors.horizontalCenter: parent.horizontalCenter
                y: parent.height * 0.83
                Button_setText {
                    text: "Войти"
                }
                MouseArea {
                    id: btnLoginArea
                    anchors.fill: parent
                    onPressed: btnLogin.color = "#A8DEEC"
                    onReleased: btnLogin.color = "#3ABDED"
                    onClicked: {
                        if ((etLoginText.text == "") || (etPasswordText.text == "")
                                || (etLoginText.text == "+7 (xxx) xxx xx xx") || (etPasswordText.text == "**********")) {
                            etLogin.color = "#FFC4C5"
                            etPassword.color = "#FFC4C5"
                        } else {
                            window.FunctionRequest()
                            etLogin.color = "#ffffff"
                            etPassword.color = "#ffffff"
                        }
                        etLoginText.focus = false;
                        etPasswordText.focus = false;
                        etLoginText.closeSoftwareInputPanel();
                        etPasswordText.closeSoftwareInputPanel();
                    }
                }
            }
            

            }
            }
            @

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andrew_21_12
              wrote on last edited by
              #6

              EditText.qml:
              @
              import QtQuick 2.2

              Rectangle {
              width: 0.8 * parent.width
              height: 0.073 * parent.height
              color: "#FFFFFF"
              }
              @

              EditText_get_setText.qml:
              @
              import QtQuick 2.2

              TextInput {
              color: "#F0F3F6"
              selectionColor: "#3ABDED"
              anchors.fill: parent
              anchors.topMargin: 0.2 * parent.height
              anchors.leftMargin: 0.05 * parent.width
              font.pixelSize: 0.5 * parent.height
              font.bold: true
              width: parent.width - parent.width * 0.075
              height: parent.height
              selectByMouse: true
              readOnly: false
              }
              @

              EditText_onClick.qml:
              @
              import QtQuick 2.2

              MouseArea {
              anchors.fill: parent
              onClicked: parent.color = "#000000",
              parent.focus = false,
              parent.focus = true
              }
              @

              Button.qml:
              @
              import QtQuick 2.2

              Rectangle {
              id: btn
              width: 0.8 * parent.width
              height: 0.085 * parent.height
              color: "#3ABDED"
              MouseArea {
              anchors.fill: parent
              onPressed: btn.color = "#A8DEEC"
              onReleased: btn.color = "#3ABDED"
              }
              }
              @

              Button_setText.qml:
              @
              import QtQuick 2.2

              Text {
              anchors.centerIn: parent
              font.pixelSize: 0.5 * parent.height
              color: "#FFFFFF"
              }
              @

              Label.qml:
              @
              import QtQuick 2.2

              Text {
              font.pixelSize: 0.046 * parent.height
              color: "#000000"
              font.bold: true
              }
              @

              Link.qml:
              @
              import QtQuick 2.2

              Text {
              font.pixelSize: 0.03 * parent.height
              color: "#3ABDED"
              font.underline: true
              verticalAlignment: Text.AlignVCenter
              height: 0.1 * parent.height
              MouseArea {
              anchors.fill: parent
              onPressed: color = "#A8DEEC"
              onReleased: color = "#3ABDED"
              onClicked: {
              window.FunctionRegister();
              }
              }
              }
              @

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andrew_21_12
                wrote on last edited by
                #7

                Upd: tested on iOS - everything works great. This trouble appears only in Android.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andrew_21_12
                  wrote on last edited by
                  #8

                  Decided to use QQmlApplicationEngine. topic closed.

                  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