pyqt:无法导入QtQuick.Dialogs

时间:2016-11-07 14:26:59

标签: python qt qml qtquick2 pyqt5

我一直在尝试使用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_()

现在,在我使用PyQt5.6的python 2.7上,这个应用程序挂起了。但是,如果您注释掉import QtQuick.Dialogs 1.0,则可以。

1 个答案:

答案 0 :(得分:0)

不确定这是否有任何帮助。我还没弄清楚为什么我无法使用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_()