d
denekaApril 8, 2018, 4:13 p.m.

Не заполняется модель в ListView данными json

QML

Народ ну помогите. Код взят из хабра, практически не изменен, но он не работает. Точнее js отрабатывает, в консоль летит выхлоп с json данными, но модель не заполняется. Код работал точно, т.к брал его же раньше и все было нормально. Может были какие то изменения в самой библиотеке. З.Ы. qt 5.10

Вот код

import QtQuick 2.9
import QtQuick.Window 2.2
Window {
    id: mainM

    property int userId: XXX //здесь ваш user_id
    property var friends

    width: 320
    height: 640
    color: 'skyblue'
    visible: true

        function getFriends() {
                var requ = new XMLHttpRequest()
                requ.open('POST', 'https://api.vk.com/method/friends.get')
                requ.onreadystatechange = function() {
                    if(requ.readyState === XMLHttpRequest.DONE) {
                        if(requ.status && requ.status === 200) {
                           console.log("response", requ.responseText)
                           var results = JSON.parse(requ.responseText)
                           mainM.friends = results.response
                        } else {
                            console.log('HTTP:', requ.status, requ.responseText)
                        }
                    }
                }
                requ.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
                requ.send('&fields=photo_medium' + '&user_id=' + mainM.userId + '&v=5.73')

        }

    ListView {
        id: view

        anchors.margins: 10
        anchors.fill: parent
        model: friends
        spacing: 10

        delegate: Rectangle {
            width: view.width
            height: 100
            anchors.horizontalCenter: parent.horizontalCenter
            color: 'white'
            border {
                color: 'lightgray'
                width: 2
            }
            radius: 10

            Row {
                anchors.margins: 10
                anchors.fill: parent
                spacing: 10

                Image {
                    id: image

                    height: parent.height
                    fillMode: Image.PreserveAspectFit
                    source: modelData['photo_medium']
                }

                Text {
                    width: parent.width - image.width - parent.spacing
                    anchors.verticalCenter: parent.verticalCenter
                    elide: Text.ElideRight
                    renderType: Text.NativeRendering
                    text: "%1 %2".arg(modelData['first_name']).arg(modelData['last_name'])
                }
            }
        }
    }

    Component.onCompleted: {
        getFriends()
    }
}

 

We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.

Do you like it? Share on social networks!

1
Evgenii Legotckoi
  • April 21, 2018, 6:53 a.m.

Ошибка в функции getFriends().

Полагаю, что VK изменили API и возвращаемый ответ.
Вот исправленный вариант данной функции
function getFriends() {
    var requ = new XMLHttpRequest()
    requ.open('POST', 'https://api.vk.com/method/friends.get')
    requ.onreadystatechange = function() {
        if(requ.readyState === XMLHttpRequest.DONE) {
            if(requ.status && requ.status === 200) {
               var results = JSON.parse(requ.responseText)
               mainM.friends = results.response.items
            } else {
                console.log('HTTP:', requ.status, requ.responseText)
            }
        }
    }
    requ.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
    requ.send('&fields=photo_medium' + '&user_id=' + mainM.userId + '&v=5.73')
}

    Comments

    Only authorized users can post comments.
    Please, Log in or Sign up
    AD

    C ++ - Test 004. Pointers, Arrays and Loops

    • Result:50points,
    • Rating points-4
    m

    C ++ - Test 004. Pointers, Arrays and Loops

    • Result:80points,
    • Rating points4
    m

    C ++ - Test 004. Pointers, Arrays and Loops

    • Result:20points,
    • Rating points-10
    Last comments
    i
    innorwallNov. 15, 2024, 8:26 a.m.
    Qt/C++ - Lesson 031. QCustomPlot – The build of charts with time buy generic priligy We can just chat, and we will not lose too much time anyway
    i
    innorwallNov. 15, 2024, 6:03 a.m.
    Qt/C++ - Lesson 060. Configuring the appearance of the application in runtime I didnt have an issue work colors priligy dapoxetine 60mg revia cost uk August 3, 2022 Reply
    i
    innorwallNov. 14, 2024, 11:07 p.m.
    Circuit switching and packet data transmission networks Angioedema 1 priligy dapoxetine
    i
    innorwallNov. 14, 2024, 10:42 p.m.
    How to Copy Files in Linux If only females relatives with DZ offspring were considered these percentages were 23 order priligy online uk
    i
    innorwallNov. 14, 2024, 8:09 p.m.
    Qt/C++ - Tutorial 068. Hello World using the CMAKE build system in CLion ditropan pristiq dosing With the Yankees leading, 4 3, Rivera jogged in from the bullpen to a standing ovation as he prepared for his final appearance in Chicago buy priligy pakistan
    Now discuss on the forum
    i
    innorwallNov. 14, 2024, 2:39 p.m.
    добавить qlineseries в функции priligy amazon canada 93 GREB1 protein GREB1 AB011147 6
    i
    innorwallNov. 11, 2024, 9:55 p.m.
    Всё ещё разбираюсь с кешем. priligy walgreens levitra dulcolax carbs The third ring was found to be made up of ultra relativistic electrons, which are also present in both the outer and inner rings
    9
    9AnonimOct. 25, 2024, 7:10 p.m.
    Машина тьюринга // Начальное состояние 0 0, ,<,1 // Переход в состояние 1 при пустом символе 0,0,>,0 // Остаемся в состоянии 0, двигаясь вправо при встрече 0 0,1,>…

    Follow us in social networks