Recommand · May 14, 2022 0

Can't show api data in qml table correctly

I’m trying to get data from an api and insert it into table view from python side. Code.


import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 1.4
import QtQuick.Controls 2.15

Window {
    width: 1200
    height: 500
    visible: true
    title: qsTr("TableView")

    Rectangle {
    anchors.fill: parent
    color: "#50505f"

    TableView {
        id: table

        anchors.fill: parent
        model: libraryModel

        rowDelegate: Rectangle {
            color: styleData.selected ? "5a53ff" : "#40405f"
            width: 2000
            height: 40
            border.width: 1

        TableViewColumn {
            role: "title"
            title: "Title"
            width: 500

        TableViewColumn {
            role: "author"
            title: "Author"
            width: 500

// Connections {
//     target: backend
// }

# This Python file uses the following encoding: utf-8
import os
import sys
from turtle import title
import requests
import json

from PySide2.QtCore import QObject, Slot, Signal
from PySide2.QtWidgets import QApplication
from PySide2.QtQml import QQmlApplicationEngine

if __name__ == "__main__":
    app = QApplication(sys.argv)
    engine = QQmlApplicationEngine()

    # Get Context
    # main = MainWindow()
    url = "http://localhost:8085/api/supplier"
    response = requests.get(url).json()
    id = []
    name = []
    for obj in response:
    myModel = []

    engine.rootContext().setContextProperty("libraryModel", id) 

    engine.load(os.path.join(os.path.dirname(__file__), "main.qml"))
    if not engine.rootObjects():

I understand what the error is, basically when the it calls the api it sets the first value it gets and sets it to both the fields, I want the data to be separated (id should go to title and name to author), but I can’t seem to find a way to do it. Any help would be really appreciated. I’ve been working on it for quite a while and can’t find a solution.

You may also like…

amazon-web-services android angular api arrays c# css dart dataframe django docker excel express firebase flutter html ios java javascript jquery json kotlin laravel linux list mongodb mysql node.js pandas php postgresql python python-3.x r react-native reactjs regex spring spring-boot sql sql-server string swift typescript vue.js