首页 文章

Qt - 从不同qml文件中的组件更改属性

提问于
浏览
1

Update 1

我的想法是能够从main.qml更改CardForm的正面和背面,因为我希望能够使用多个CardForm实例 . 我试着做他们所做的here但它不起作用 .

这是代码:

CardForm.qml

import QtQuick 2.0

Flipable {
    id: sCard
    width: 75
    height: 200

    property bool flipped: false
    property string front: "Front"
    property string back: "Back"

    property alias callFront : front
    property alias callBack : back

    front: Rectangle{
        id: front
        anchors.fill: sCard
        border.width: 2
        border.color: "black"
        radius: 5
        Text{
            anchors.centerIn: parent
            text: sCard.front
        }
    }

    back: Column{
        Rectangle{
            id: back
            anchors.fill: sCard
            radius: 5
            border.width: 2
            border.color: "black"
            Text{
                anchors.centerIn: parent
                text: sCard.front
            }
            Text{
                anchors.centerIn: parent
                text: sCard.front
            }
        }
    }

    transform: Rotation{
        id: flip
        origin.x: sCard.width
        origin.y: sCard.height/2
        axis.x: 0; axis.y: 1; axis.z: 0     // set axis.y to 1 to rotate around y-axis
        angle: 0    // the default angle
    }

    states: State {
        name: "back"
        PropertyChanges {
            target: flip
            angle: 180
        }
        when: sCard.flipped
    }

    transitions: Transition{
        NumberAnimation {
            target: flip
            property: "angle"
            duration: 200
        }
    }

    MouseArea{
        anchors.fill: parent
        onClicked: sCard.flipped = !sCard.flipped
    }
}

main.qml

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Neuro Seed")

    SwipeView {
        id: swipeView
        anchors.fill: parent
        currentIndex: tabBar.currentIndex

        Column {
            CardForm{
                id: test
                anchors.centerIn: parent
                test.callFront: "Hello World!"
                test.callBack: "Bonjour le Monde!
            }
        }
    }
}

以下是错误消息:

SHGetSpecialFolderPath()因标准位置“共享配置”而失败,clsid = 0x1c . ()

qrc:/main.qml:17:13:QML CardForm:back是一次写入属性

qrc:/main.qml:17:13:QML CardForm:front是一次写入属性

qrc:/main.qml:16:9:QML列:无法为Column内的项指定top,bottom,verticalCenter,fill或centerIn锚点 . 列不起作用 .

c1.getFront()和getBack()来自我制作的C类 . 我将这些更改为“Hello World!”和“Bonjour le Monde!”

1 回答

  • 0

    因此,经过数小时的努力,我发现要创建一个可由其他.qml文件访问的属性,您必须创建一个 property alias name: id.property . id必须指向代码中对象的现有实例以及您希望能够从外部更改的此实例的属性 . 所以在我的情况下它会是这样的:

    CardForm.qml

    Flipable {
        id: sCard
        width: 75
        height: 200
    
        property bool flipped: false
        property alias frontText : front.text
    
        front: Rectangle{
            id: front
            anchors.fill: sCard
            border.width: 2
            border.color: "black"
            radius: 5
            Text{
                anchors.centerIn: parent
                text: frontText
            }
        }
    }
    

    并在 main.qml

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.0
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Neuro Seed")
    
            Rectangle {
                anchors.fill: parent
                CardForm{
                    id: test
                    anchors.centerIn: parent
                    frontText: "Hello World!"
                }
            }
        }
    }
    

相关问题