Error when trying to append listmodel with predefined variable

271 Views Asked by At

I am trying to append to a list using variables:

property var varOne: "#asdflkj"

It works if I type it out:

listModel.append({"name":"#asdflkj"})

If I try to do it as I intended to do, I get an error (Can't assign to existing role 'name' of different type [VariantMap -> String]):

listModel.append({"name":varOne})

I use this same variable elsewhere as a string and no problem:

property var temp: ""
temp += varOne

What am I doing wrong?

EDIT: Ok, I think I narrowed down the problem quite a bit. I think the issue is the function Qt.rgba(), which gives me a hex code for a user defined color. I can use the output from this function as a string anywhere (i.e. labels, writing to memory, etc.) Appending to the list model fails though. I am guessing I am missinterpreting what the Qt.rgba output is. Here is my minimum reproducible.

import QtQuick 2.2
import QtQuick.Window 2.0
import QtQuick.Layouts 1.1

memTest: api.memory.get("Saved Colors").split(";")
ListModel {
    id: colorModel
    Component.onCompleted: {
        for (var i = 0; i < memTest.length; ++i) {
            append({"color1": memTest[0]})
        }
    }
}

function assignHexCodetoList() {
        
    //this works
    var temp = "#3d5c3e"
    colorModel.append({"color1":temp})
    
    //this doesn't work
    var temp = Qt.rgba(0.5, 0.5, 0.5)
    colorModel.append({"color1":temp})
    
    //yet this works with either
    var temp = Qt.rgba(0.5, 0.5, 0.5)
    temp = memTest + ";" + temp
    api.memory.set("Saved Colors",temp);
    
}

//Button that triggers the function
Rectangle {
    id: button
    Keys.onPressed: {
        if (api.keys.isAccept(event)) {
            assignHexCodetoList();
        }
    }
}

EDIT 2: I got it to work, but what I did is totally stupid. I can display the output of Qt.rgba() as if it's a string:

Text {
  text: Qt.rgba(0.5,0.5,0.5);
}

I get a hex code. I can output to the memory with:

var temp = Qt.rgba(0.5,0.5,0.5);

Why I can't input Qt.rgba() output directly into a listmodel.append() I'm not sure:

//doesn't work
listModel.append({"color":Qt.rgba(0.5,0.5,0.5)})

I assigned it as a string and then converted it back to a hex code and it works:

var temp = "" + Qt.rgba(0.5,0.5,0.5); //basically convert to Qt.rgba() to string.
listModel.append({"color":temp});

I'm dying to know why this is though. The Qt.rgba details on the website are particularly cryptic, and it makes no sense to me why I can display the output as text, output it to memory as a string, but can't use it in the listModel.append() function as a string. Any ideas?

0

There are 0 best solutions below