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

Is there any common elements between dynamic objects?



  • Hi. I have a little problem. I prepared listview and I am creating 3 the same objects with the same parameters. First object set correct value (0), and object number 2 and 3 set value 22. Why?

    main.qml

     Component.onCompleted:
        {
            object_infinite.visible = true
            object_infinite.setValues(0, 23, 2)
    //        object_infinite.setValues()
    
    //        object_infinite.setGradientColor("transparent")
            object_infinite.setBorderSize(1)
    
            object_infinite.x=150
            object_infinite.y=150
    
            object_infinite.width=120
            object_infinite.height=270
    
    ////////////////////////////////////////////////////////////
    
    
            object_infinite2.visible = true
    
            object_infinite2.x=270
            object_infinite2.y=150
    
            object_infinite2.width=120
            object_infinite2.height=270
    
            object_infinite2.setValues(0, 23, 2)
    //        object_infinite2.setValues(0, 59, 2)
    
            object_infinite2.setBorderSize(1)
    
    ////////////////////////////////////////////////////////////
    
            object_infinite3.visible = true
    
            object_infinite3.x=390
            object_infinite3.y=150
    
            object_infinite3.width=120
            object_infinite3.height=270
    
            object_infinite3.setValues(0, 23, 2)
    //        object_infinite3.setValues(0, 59, 2)
    
            object_infinite3.setBorderSize(1)
    
        }
    

    infinite_listview.qml

        function setValues(first = 0, last = 59, fill_length = 2, fill_value = '0')
        {
            console.log("START: setValues")
            listviewID.first_value = Number(first)
            listviewID.last_value = Number(last)
            listviewID.fill_length_value = Number(fill_length)
            listviewID.fill_sign_value = fill_value
    
            if (listviewID.first_value > listviewID.last_value)
            {
                //Pierwsza wartosc musi byc wieksza, niz zdruga
                modelID.clear()
                modelID.append( {"itemID": 0, "number": "ERROR: 1"} )
                return -1
            }
            else if ( numberOfElements( listviewID.first_value, listviewID.last_value) <= 3)
            {
                //Ilosc elementow miedzy pierwszym, a ostatnim elementem musi byc min 3 elementy, zeby wszystkie pola byly zajete w liscie
                modelID.clear()
                modelID.append( {"itemID": 0, "number": "ERROR: 2"} )
                return -2
            }
    
            //Obliczenia zwiazane z okresleniem srodkowego elementu listy, ktory ma byc na poczatku listy
            var sum = numberOfElements(listviewID.first_value, listviewID.last_value)
            var half1 = 0
            var half2 = 0
    
            var resultOfMod = sum % 2
            if (resultOfMod === 0)
            {
                half1 = Math.floor(sum/2)
            }
            else
            {
                half1 = Math.floor(sum/2)
                half2 = sum - half1
            }
    
            modelID.clear()
            //Dodawania elementow do listy
            for (var index_tmp = listviewID.last_value - half1, i = listviewID.first_value ; i <= listviewID.last_value; i++, index_tmp++)
            {
                console.log("index_tmp: " + index_tmp + " : i: " + i + " : listviewID.first_value: " + listviewID.first_value)
                if ( index_tmp <= listviewID.last_value)
                {
                    modelID.append( {"itemID": index_tmp, "number": index_tmp.toString().padStart(listviewID.fill_length_value, listviewID.fill_sign_value)} )
                }
                else
                {
                    //Jak dodawany element dojdzie do wartosci listviewID.last_value, to licznik sie resetuje, zeby lista zaczynala sie na srodku, na srodku byla wartosc 0 i ostatni element takze byl na srodku listy
                    index_tmp = listviewID.first_value
                    modelID.append( {"itemID": index_tmp, "number": index_tmp.toString().padStart(listviewID.fill_length_value, listviewID.fill_sign_value)} )
                }
                console.log("modelID.count: " + modelID.count)
                console.log("listviewID.count: " + listviewID.count)
            }
    
            console.log("listviewID.set_values_first_time: " + listviewID.set_values_first_time)
            listviewID.currentIndex = -1    //Z tym ustaiwniem, poprawnie sie ustawia na 0, ale uzywajac scroola, przeskakuje w dzziwne miejsce
            for (var i = listviewID.first_value, index_tmp = 0 ; i <= listviewID.last_value; i++, index_tmp++)
            {
    
                console.log("modelID.get(listviewID.currentIndex).itemID: " + modelID.get(index_tmp).itemID)
                if(modelID.get(index_tmp).itemID === listviewID.first_value)
                {
                    listviewID.positionViewAtIndex(index_tmp, ListView.Center)
                    if(listviewID.set_values_first_time === false)
                    {
                        //Uzywane, gdy ustawia sie liste po razz pierwszy po uruchomieniu
                        listviewID.currentIndex = index_tmp-2
                        console.log("FALSE: listviewID.currentIndex: " + listviewID.currentIndex)
                    }
                    else
                    {
                        //Uzywane, gdy uzywa sie funkcji set_values po raz drugi i kolejny podczas dzialania programu. Nie wiem, od czego to zalezy.
                        listviewID.currentIndex = index_tmp
                        console.log("TRUE: listviewID.currentIndex: " + listviewID.currentIndex)
                    }
                    break
                }
            }
            listviewID.set_values_first_time = true
    
            return 0
        }
    

    Full app -> https://github.com/pawlosck/QML_Controls/tree/c3307a8e320fe3f5ae7063f84ee2135a1bc1edf1

    4df8698e-81d9-4c5d-87d9-aeeda4688d48-obraz.png



  • Problem solved

    Function setValues should be run as last. Listview didn't have correct position and size.

            object_infinite.x=150
            object_infinite.y=150
            object_infinite.width=120
            object_infinite.height=270
            object_infinite.setBorderSize(1)
            object_infinite.setValues(0, 23, 2)
            object_infinite.visible = true
    

Log in to reply