【问题标题】:pyqt: cannot import QtQuick.Dialogspyqt:无法导入 QtQuick.Dialogs
【发布时间】:2016-11-07 14:26:58
【问题描述】:

我一直在尝试使用 PyQt5,但我遇到了一个奇怪的问题,我无法从 python 应用程序导入 QtQuick.Dialogs。

因此,请考虑以下 QML 文件:

example.qml

import QtQuick 2.0
import QtQuick.Window 2.1
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Dialogs 1.0 // Offending line!
import DicomSorter 1.0

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

    style: ApplicationWindowStyle {
        background: Rectangle {
            color: "#FFFFFF"
        }
    }   

    // Login Form
    Rectangle {
        id: loginForm
        ColumnLayout {
            anchors.centerIn: parent
            spacing: 25
            width: 200

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

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

            RowLayout {
                Button {
                    id: loginButton
                    text: "Log In"
                    Layout.fillWidth: true
                    onClicked: {
                        stackView.push(dirSelector)
                    }
                }

                Button {
                    id: cancelButton
                    text: "Cancel"
                    Layout.fillWidth: true
                }
            }
        }
    } // Login Form    

    // The main stackview component
    StackView {
        id: stackView
        anchors.fill: parent
        Component.onCompleted:
        {
            stackView.push(loginForm)
        }
    } // StackView
}

现在,我简单地从我的 python 应用程序中调用它,如下所示:

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

app = QApplication(sys.argv)
engine = QQmlApplicationEngine(example.qml')

print "Created"

topLevel = engine.rootObjects()[0]
win = QQuickWindow(topLevel)
win.show()
app.exec_()

现在,在我的 python 2.7 和 PyQt5.6 上,这个应用程序挂起。但是,如果您将 import QtQuick.Dialogs 1.0 注释掉,它会起作用。

【问题讨论】:

  • 运行带有QML_IMPORT_TRACE=1 环境变量集的应用程序,并附加输出。

标签: python qt qml qtquick2 pyqt5


【解决方案1】:

不确定这是否有帮助。我还没有弄清楚为什么我不能使用QtQuick Dialogs 的原因,但找到了一种解决方法,即使用QQmlEngine 而不是QQmlApplicationWindow。因此,我将应用程序代码更改为:

from PyQt5.QtQml import QQmlEngine, QQmlComponent

app = QApplication(sys.argv)
engine = QQmlEngine(app)
component = QQmlComponent(engine)
component.loadUrl(QUrl('ui/window.qml'))
top = component.create()
top.show()
app.exec_()

【讨论】:

    猜你喜欢
    • 2017-05-01
    • 2010-10-04
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 2021-11-20
    • 2012-08-04
    • 1970-01-01
    相关资源
    最近更新 更多