pyqt中的多个qml文件 [英] multiple qml files in pyqt
问题描述
我正在使用 pyqt 并且我有以下目录结构:
I am using pyqt and I have the following directory structure:
root
----> apps/
----> ui/
我在 app 文件夹中有一个简单的基于 qml 的应用程序:
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
qml 文件定义应用程序窗口并使用 StackView,如下所示:
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)
}
}
}
这里使用了 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
}
}
}
}
现在当我使用 qmlscene
时,视图加载得很好.但是,运行 python 会导致应用程序在尝试加载 QQmlApplicationEngine
时挂起.我觉得它可能与 qml 路径有关,所以我将 import ../ui
包含到 window.qml
导入中,但这并没有改变任何东西.
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.
我在 Anaconda 环境中使用 Python 2.7
和 Qt 5.6
.Qt 是从这里安装的:https://anaconda.org/anaconda/pyqt
I am using Python 2.7
with Qt 5.6
in an Anaconda environment. Qt was installed from here: https://anaconda.org/anaconda/pyqt
推荐答案
同样,我使用多个 qml 文件和多个 python 文件.当我访问应用程序时,它成功加载了两个 qml 文件,但是当通过第三个 qml 页面堆叠时,应用程序崩溃了.它引发分段错误.
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.
我使用 Stackview 来推送和弹出 QML 页面,在调试过程中我发现它只对两个页面执行推送和弹出操作.
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.
如果我也使用 Swipeview,我也遇到过.我猜是创建的引擎可以加载两个堆栈 QML 文件.
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.
这篇关于pyqt中的多个qml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!