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

标签 qt qml

更新 1

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

代码如下:

卡片表格.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
    }
}

主.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: 无法为 Column 内的项目指定顶部、底部、verticalCenter、fill 或 centerIn anchor 。列将不起作用。

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

最佳答案

经过数小时的努力,我发现要创建一个可由其他 .qml 文件访问的属性,您必须创建一个属性别名: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!"
            }
        }
    }
}

关于Qt - 从不同的 qml 文件中的组件更改属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40669446/

相关文章:

string - Qml 文本换行

qt - Qml 可滑动页面

c++ - QQuickItem 不会渲染

c++ - QT Creator中如何使用FMOD工具?

c++ - 使用QMediaPlayer时出现"QWidget::paintEngine: Should no longer be called"

c++ - 升级到 Qt 5.15 后,ListView 委托(delegate)中的父级为空

c++ - Box2D 旋转中心

qt - 如何删除 qmake 中的更改默认 CFLAGS?

qt - 如何在 PySide/PyQt 中将 TreeView 另存为 pdf?

c++ - 如何将 VXYModelMapper 与 QStandardItemModel 一起使用?