Qt - 如何在不同的QML文件中更改组件属性

3

更新1

这个想法是从main.qml中更改CardForm的前面和后面,因为我想要使用多个CardForm实例。我尝试做他们在这里所做的事情,但它不起作用。

以下是代码:

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() 失败,标准位置为 "Shared Configuration",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:无法为 Column 内部的项目指定 top、bottom、verticalCenter、fill 或 centerIn 锚点。Column 将无法正常工作。

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


唉...有很多东西。请再试着解释一下,你到底想要实现什么目标。一切都始于你的 CardForm 的第 9 行和第 10 行,你试图覆盖 Flipable 的属性,但这不太可能是故事的结局。请还包括所有错误信息。 - derM
在 main.qml 中引用 c1 时似乎未定义。 - selbie
您试图将某些内容分配给“readonly”属性(“property alias callFront:front”创建对具有ID“front”的对象的只读访问)。 - derM
这段代码出了很多问题,我都不知道从哪里开始修复。你应该从头开始重新写,一步一步尝试你的代码,每次修改后都进行测试。如果遇到无法解决的问题,可以再次询问。但请务必提供错误信息。 - derM
为了给您一个好的答案,如果您还没有看过[ask],那么看一眼可能会有所帮助。如果您能提供一个[mcve],那也可能会很有用。 - Mat
我已经更新了帖子和一些代码,@derM对我来说代码是有效的,我在编写和更改它时采取小步骤,请问您能否详细说明它是如何损坏的?我还试图使属性不是只读的,但在文档中找不到相关信息。 - Rene
1个回答

3

经过若干个小时的探索,我发现要创建一个可以被其他.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!"
            }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接