multiple qml files in pyqt

2019-09-14 01:24发布

问题:

I am using pyqt and I have the following directory structure:

root
----> apps/
----> ui/

I have a simple qml based application in the app folder as:

apps/testqt.py

from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQuick import QQuickView, QQuickWindow
from PyQt5.QtQml import qmlRegisterType, QQmlApplicationEngine
import sys

app = QApplication(sys.argv)
engine = QQmlApplicationEngine('ui/window.qml')
topLevel = engine.rootObjects()[0]
win = QQuickWindow(topLevel)
win.show()
app.exec_()

ui/window.qml

The qml file defines the app window and uses a StackView as follows:

import QtQuick 2.0
import QtQuick.Window 2.1
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.4


ApplicationWindow {
    id: rootWindow
    objectName: "window"
    visible: true
    width: 800
    height: 480
    title: "Window"
    Component.onCompleted: {
        setX(Screen.width / 2 - width / 2);
        setY(Screen.height / 2 - height / 2);
    }

    property Component loginView: LoginView {}

    StackView {
        id: stackView
        anchors.fill: parent
        Component.onCompleted:
        {
            stackView.push(loginView)
        }
    }
}

This uses the LoginView component which is defined as:

apps/LoginView.qml

import QtQuick 2.0
import QtQuick.Window 2.1
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.0


ControlView {
    ColumnLayout {
        anchors.centerIn: parent
        spacing: 25
        width: 200

        TextField {
            id: username_fld
            placeholderText: qsTr("User name")
            Layout.fillWidth: true
        }

        TextField {
            id: password_fld
            placeholderText: qsTr("Password")
            Layout.fillWidth: true
            echoMode: TextInput.Password
        }

        RowLayout {
            Button {
                id: login_button
                text: "Log In"
                Layout.fillWidth: true
            }

            Button {
                id: cancel_button
                text: "Cancel"
                Layout.fillWidth: true
            }
        }
    }
}

Now when I use qmlscene, the view loads just fine. However, running python results in the application hanging while trying to load the QQmlApplicationEngine. I have a feeling it has something to do with the qml paths perhaps, so I included import ../ui into the window.qml imports but that did not change anything.

I am using Python 2.7 with Qt 5.6 in an Anaconda environment. Qt was installed from here: https://anaconda.org/anaconda/pyqt

回答1:

Similarly i use multiple qml files along with multiple python files. When i access application it loads perfectly two qml files successfully but when stacked through third qml page, the application crashes. It throws segmentation fault.

I use Stackview to push and pop through QML pages, during debugging i found that it works when push and pop are done for just for two pages.

I have came across if i use Swipeview too. What i guess is that the engine that was created can be loaded with two stack QML files.