Problems with QQmlViewer



  • ello, I'm working on an application in QML for android, so I'm using the QQmlViewer to make use of the data types created by me, but I have a problem: when the application starts, I have to press the back button to be able to trigger the different components of my application. How can i fix this?


  • Lifetime Qt Champion

    Hi,

    Without any code to analyse, it's going to be pretty difficult to help you.



  • Sorry about that, Here is the code:

    int main(int argc, char *argv[])
    {
        QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QApplication app(argc, argv);
        QQuickView viewer;
        viewer.setResizeMode(QQuickView::SizeRootObjectToView);
    
        SomeName somename;
    
        QQuickStyle::setStyle("Material");
    
        QQmlContext *context = viewer.rootContext();
    
        context->setContextProperty("SomeName", &somename);
    
        QObject::connect(viewer.engine(), &QQmlEngine::quit, &viewer, &QWindow::close);
        viewer.setSource(QUrl("qrc:/main.qml"));
    
        viewer.show();
    
        return app.exec();
    
    }
    
    

  • Lifetime Qt Champion

    What do you have in your QML file ?



  • This is all the code in my mail.qml file:

    import QtQuick 2.6
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.0
    import Qt.labs.settings 1.0
    import QtQuick.Controls.Material 2.0
    import QtQuick.LocalStorage 2.0
    
    ApplicationWindow {
        id: appwindow
        visible: true
        width: 340
        height: 480
        title: qsTr("RegCharge")
    
    
        Settings{
            id: settings
            property string style: "Material"
        }
    
        Loader{
            id: splashLoader
            anchors.fill: parent
            source: "qrc:/SplashScreen.qml"
        }
    
        header: ToolBar{
            Material.foreground: "white"
            Material.background: Material.Blue
            RowLayout{
                spacing: 20
                anchors.fill: parent
    
    
                ToolButton{
                    id: hamButton
                    contentItem: Image{
                        fillMode: Image.Pad
                        horizontalAlignment: Image.AlignHCenter
                        verticalAlignment: Image.AlignVCenter
                        source: "qrc:/Images/drawer.png"
                    }
                    onClicked: drawer.open()
                }
    
                Label{
                    id: titleLabel
                    text: "RegCharge"
                    font.pixelSize: 20
                    elide: Label.ElideRight
                    horizontalAlignment: Qt.AlignHCenter
                    verticalAlignment: Qt.AlignVCenter
                    Layout.fillWidth: true
                }
    
                ToolButton{
                    id: dotsButton
                    contentItem: Image{
                        fillMode: Image.Pad
                        horizontalAlignment: Image.AlignHCenter
                        verticalAlignment: Image.AlignVCenter
                        source: "qrc:/Images/menu.png"
                    }
                    onClicked: optionsMenu.open()
    
                    Menu{
                        id: optionsMenu
                        x: parent.width - width
                        transformOrigin: Menu.TopRight
    
                        MenuItem{
                            text: "About"
                            onTriggered: {
                                listView.currentIndex = listView.count - 1
                                titleLabel.text = "Acerca de"
                                stackView.replace("qrc:/About.qml")
                            }
                        }
                    }
                }
            }
        }
    
    
        Drawer{
            id: drawer
            width: Math.min(appwindow.width, appwindow.height) / 3 * 2
            height: appwindow.height
    
    
            ListView{
                id: listView
                currentIndex: 0
                anchors.fill: parent
    
                delegate: ItemDelegate {
                    width: parent.width
                    text: model.title
                    highlighted: ListView.isCurrentItem
                    onClicked: {
                        if(listView.currentIndex != index){
                            listView.currentIndex = index
                            titleLabel.text = model.title
                            stackView.replace(model.source)
                        }
                        drawer.close()
                    }
                }
    
                model: ListModel{
                    id: listModel
                    ListElement {title: "Principal"; source: "qrc:/Principal.qml" }
                    ListElement {title: "DataBase"; source: "qrc:/DataBase.qml" }
                    ListElement {title: "Estadística"; source: "qrc:/Statistic.qml" }
                    ListElement {title: "Costos"; source: "qrc:/Costs.qml" }
                    ListElement {title: "Acerca de"; source: "qrc:/About.qml"  }
                }
            }
        }
    
        StackView{
            id: stackView
            anchors.fill: parent
            
            ToolTip {
                id: toolTip
                timeout: 3000
                visible: Keys.onBackPressed()
                text: "Presione nuevamente para salir"
                Keys.onBackPressed:
                {
                    Qt.quit()
                }
            }
    
            initialItem: Principal{}
            Component.onCompleted: {
                splashLoader.visible = false
            }
            Keys.onEscapePressed: console.log("Escape")
        }
    
        function createCostsTable() {
            var db = LocalStorage.openDatabaseSync("RegStore", "1.0", "RegStore Database", 5242880);
    
            db.transaction(
                        function(tx) {
                            tx.executeSql('CREATE TABLE IF NOT EXISTS Costs(Type integer, Valor double)');
    
                            var rs = tx.executeSql('SELECT * FROM Costs');
    
                            if(rs.rows.length <= 0)
                            {
                                var arr = [3, 5, 11, 14, 17, 22, 27]
                                var ar2 = [2.8, 4.8, 10, 13, 16.5, 20.8, 25.8]
    
                                for(var i = 0; i < arr.length; i++)
                                {
                                    tx.executeSql('INSERT INTO Costs VALUES(?,?)', [arr[i], ar2[i]])
                                }
                                console.log("Correcto")
                            }
                        }
                        )
        }
    
        Component.onCompleted: createCostsTable()
    }
    
    


  • People, I solved the problem myself, although I really do not know what happens with QQmlViewer. In the end I used QQmlApplicationEngine , so the code was like this:

    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
    
        QQuickStyle::setStyle("Material");
        
        BackEnd backend;
    
        QQmlApplicationEngine engine;
        engine.rootContext()->setContextProperty("backEnd", &backend);
        engine.load(QUrl(QLatin1String("qrc:/main.qml")));
    
        return app.exec();
    }
    

Log in to reply
 

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