pyqt: cannot import QtQuick.Dialogs

与世无争的帅哥 提交于 2019-12-25 08:18:12

问题


I have been trying to use PyQt5 and I have a strange problem where I cannot import QtQuick.Dialogs from a python app.

So, consider the following QML file:

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
}

Now, I call it simply from my python app as follows:

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_()

Now, on my python 2.7 with PyQt5.6, this application hangs. However, if you comment out the import QtQuick.Dialogs 1.0, it works.


回答1:


Not sure if this is of any help. I have not figured out the reason for why I cannot use QtQuick Dialogs but have found a workaround, which is to use QQmlEngine instead of QQmlApplicationWindow. So, I changed the application code as:

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_()


来源:https://stackoverflow.com/questions/40467572/pyqt-cannot-import-qtquick-dialogs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!