pyqt:无法导入快速对话

2024-10-03 19:20:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试使用PyQt5,但有一个奇怪的问题,我无法导入快速对话从python应用程序。在

因此,考虑以下QML文件:

示例.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应用程序简单地调用它,如下所示:

^{pr2}$

现在,在我的python2.7和PyQt5.6上,这个应用程序挂起了。但是,如果您注释掉import QtQuick.Dialogs 1.0,它就可以工作了。在


Tags: importidtrue应用程序widthscreencomponentlayout
1条回答
网友
1楼 · 发布于 2024-10-03 19:20:22

不知道这是否有用。我还没有弄清楚为什么我不能使用QtQuick Dialogs的原因,但是找到了一个解决方法,即使用QQmlEngine,而不是{}。因此,我将应用程序代码改为:

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

相关问题 更多 >