GithubHelp home page GithubHelp logo

Comments (12)

niemeyer avatar niemeyer commented on July 22, 2024

For the record, working with model views is already possible and convenient today via the indexed mechanism.

Here is a good example:

Let's keep this bug open, though, as there might be advantages in supporting an implementation of QAbstractItemModel.

from qml.

michael-k avatar michael-k commented on July 22, 2024

Regarding TableViews, I had the problem, that role did not work. (At least I could not find out how to get it to work. So I use this workaround:

TableView {
    model: contactList.len

    TableViewColumn {
        title: qsTr("Name")
        delegate: Item {
            Text {
                anchors.verticalCenter: parent.verticalCenter
                color: styleData.textColor
                elide: styleData.elideMode
                text: contactList.name(styleData.row)
            }
        }
    }
}

from qml.

quarnster avatar quarnster commented on July 22, 2024

Is there an efficient way to remove/add a single item out of 1000 with the index model?

Just setting model = array.length will re-create all 999/1001 elements rather than just tweak the single one that was changed.

from qml.

quarnster avatar quarnster commented on July 22, 2024

To answer myself; yes there is. For example:

    ListModel {
        id: myModel
    }
...
    myModel.append({"itemId":rs.item(i).id});

itemId will then exist as a variable in the delagate component that is created as a result. myModel can be located from go so that its functions can be called from there if one wants to.

from qml.

NSkelsey avatar NSkelsey commented on July 22, 2024

@quarnster I am trying to figure out what you did here. Is there any chance you could elaborate?

from qml.

quarnster avatar quarnster commented on July 22, 2024

Sure, here's a quick freestanding example:

package main

import (
    "gopkg.in/qml.v1"
    "log"
)

var qmlFile = `
import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.0

ApplicationWindow {
    id: main
    width: 800
    height: 600

    function validate() {
        for (var i = 0; i < gomodel.len(); i++) {
            var godata = gomodel.get(i);
            var qmldata = lmodel.get(i).goitem;
            // Note that "godata" and "qmldata" are not
            // pointing to the same address though...
            if (godata.text != qmldata.text) {
                console.log(godata.text, qmldata.text);
            }
        }
    }
    function insert(i, data) {
        lmodel.insert(i, {"goitem": data});
        validate();
    }
    function removed(i) {
        lmodel.remove(i);
        validate();
    }
    ListModel {
        id: lmodel
    }
    ColumnLayout {
        Button {
            text: "add"
            onClicked: {
                console.log("adding!!");
                gomodel.add("Item " + Math.floor(Math.random()*1024));
            }
        }
        Button {
            text: "remove"
            onClicked: {
                console.log("removing!!");
                gomodel.remove(lv.currentIndex);
            }
        }

        ScrollView {
            Layout.fillHeight: true
            ListView {
                id: lv
                model: lmodel
                height: contentHeight
                delegate: Rectangle {
                    id: item
                    color: lv.currentIndex == index ? Qt.rgba(0.9,0.9,0.9,1.0) : "white"
                    width: Math.max(t.width, parent.width)
                    height: t.height
                    clip: true
                    Text {
                        id: t
                        text: goitem.text
                    }
                    MouseArea {
                        anchors.fill: parent
                        onClicked: lv.currentIndex = index
                    }
                }
            }
        }
    }
}
`

var window *qml.Window

type (
    Item struct {
        Text string
    }
    Model struct {
        items []*Item
    }
)

func (m *Model) Add(text string) {
    i := len(m.items)
    m.items = append(m.items, &Item{text})
    window.Call("insert", i, m.items[i])
}

func (m *Model) Remove(i int) {
    copy(m.items[i:], m.items[i+1:])
    m.items = m.items[:len(m.items)-1]
    window.Call("removed", i)
}

func (m *Model) Len() int {
    return len(m.items)
}

func (m *Model) Get(i int) *Item {
    return m.items[i]
}

func main() {
    model := &Model{}
    log.Println(qml.Run(func() error {
        engine := qml.NewEngine()
        defer engine.Destroy()
        engine.Context().SetVar("gomodel", model)
        component, err := engine.LoadString("main.qml", qmlFile)
        if err != nil {
            return err
        }
        window = component.CreateWindow(nil)
        window.Show()
        window.Wait()
        return nil
    }))
}

from qml.

NSkelsey avatar NSkelsey commented on July 22, 2024

@quarnster Thanks for the response. That window.Call in Add is very clever! I would not have thought of that.

This should be added to the delegate dir of the examples.

from qml.

pjoe avatar pjoe commented on July 22, 2024

Though putting things through a QML ListModel is workable, in my experience that won't perform anything near the QAbstractItemModel, that can efficiently signal changes in the model directly to the QML ListView, see e.g. beginInsertRows/endInsertRows. This is especially true if you are doing insertion/removal of mutliple items at once. So having a way to implement this QAbstractItemlModel interface in go would be really cool :D

from qml.

pjoe avatar pjoe commented on July 22, 2024

Ohh I just saw that there is already a list-model branch with some work in progress for this, anything we can do to help?

from qml.

niemeyer avatar niemeyer commented on July 22, 2024

I'm not sure. The main blocker is researching in depth how to implement these interfaces in a sane way, and the reason it's taking so long is because the interface seems extensive and non-trivial on the C++ side. Whatever we do needs to be straightforward and sensible, and I haven't managed to work on it enough to get to that point.

from qml.

filcuc avatar filcuc commented on July 22, 2024

Niemeyer we should get in contact. I'm the author of the DOtherSide project that bring Qml bindings for Nim and D. I worked on this topic maybe i can share my implementation and see if it works for you too.

from qml.

pjoe avatar pjoe commented on July 22, 2024

Also lemme know if there is anything i can do to help. I have been using QAbstractItemList for interfacing C++ and QML for several years by now (since sometime from Qt 4.7beta). I'm on IRC freenode (as pjoe or pjoe_dj) during daytime CET

from qml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.