Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem with modelData. QVarient questions.



  • I want to start off by apologizing if this questions seems vague. It is that way because I am confused and don't know what's going on.

    My question is regarding a variable I am working with called modelData. In this application (I did not build) there is a portion of code which confuses me:

    Component.onCompleted:
      {
        ...
        else if(typeof(modelData.value) === "string" && modelData.optionsList.length === 0)
        {
          modelData.settingInvalid ? value_container.allowNext = false : value_container.allowNext = true
          ldr.sourceComponent = textEntryComponent
          value_container.objectName = "textbox"
        }
        else if(typeof(modelData.value) === "number" && modelData.optionsList.length !== 0)
        {
          ldr.sourceComponent = listOptionComponent
          value_container.objectName = "list"
        }
    ....
    

    How is it possible for this modelData's value to be either a string or a number? Does that mean I am dealing with two different variables both called modelData or is it possible that this variable is changing it's initialization on the back-end somehow?



  • What language is this code in?



  • @VRonin The code shown is QML but the code on the back-end is all C++.



  • @Circuits said in Problem with modelData. QVarient questions.:

    How is it possible for this modelData's value to be either a string or a number?

    Why shouldn't it be? Also, this code looks like it's supplying generic code: modelData is some data value from the model, it might come from any row and column, so the code says if it's a string do this and if it's a number do that.

    Code is JavaScript, which is an untyped language. Unlike, say, C++, modelData can hold any value type, it never gets pre-declared as str or int or anything. If anything, think of it as a QVariant, kind of.



  • @JonB OH ic, so your saying the type is probably being determined on the front end? In other words, somewhere (prior to this code) it's value is being specified to be either an int or a string or what-have-you. Is that what your saying?



  • @Circuits
    I suppose the short answer is yes, though its rather determined in the backend of code, which means not in the UI.

    The data presumably started life as some value in some row/column of something. That type may have been preserved, or it may have got converted all over the place, who knows.

    Actually the code tests modelData.value. JavaScript typeof() returns the type that is currently stored in modelData.value. At some point something set modelData.value = "a string" or modelData.value = 99 or modelData.value = -123.45 or whatever. It looks like the code then decides what kind component/widget would be suitable for displaying that type of value.


Log in to reply