循环对象 属性 (QML)

Loop on object property (QML)

当我使用以下 QML 文件时,Qt 在运行时报错:

import QtQuick 2.7
import QtQuick.Controls 2.0

Rectangle
{
    color: palette.grey

    property var spotButtonFunctions :
    {
        'EditViewTool' : function() {switchToEditViewToolButton.source = "../View/Icons/icon_game.png";},
        'SelectTool'   : function() {switchToSelectToolButton.source = "../View/Icons/icon_game.png";}
    }

    property var greyButtonFunctions :
    {
        'EditViewTool' : function() {switchToEditViewToolButton.source = "../View/Icons/icon_settings.png";},
        'SelectTool'   : function() {switchToSelectToolButton.source = "../View/Icons/icon_info.png";}
    }

    // View update slots.
    function onNotifyCurrentToolSignal(currentToolName)
    {
        // Grey all tool buttons.
        for (x in greyButtonFunctions)
            greyButtonFunctions[x]();

        // Spot the current tool button.
        if (currentToolName !== "")
            spotButtonFunctions[currentToolName]();
    }
}

错误消息与第 for (x in greyButtonFunctions) 行相关:

Error: Cannot assign QString to double

有什么想法吗?

您正在分配给 Itemx property

如果你想要一个名为 x 的局部变量,你必须这样做 for (var x in greyButtonFunctions)