Comments (12)
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.
Regarding TableView
s, 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.
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.
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.
@quarnster I am trying to figure out what you did here. Is there any chance you could elaborate?
from qml.
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.
@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.
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.
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.
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.
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.
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)
- Can we use QT5 with Microsoft VS2015 Community?
- Statically compiling with qt/qml for a truly standalone app HOT 9
- Something wrong with WebView HOT 3
- Install on Mac without Homebrew
- cgo result has Go pointer
- go-qml is broken under Go 1.7 HOT 21
- Error Build a sample HOT 3
- How Build and Install QT on Go 1.8?
- Error Build
- Tray icon/menu support
- types foe model QML
- Error! HOT 1
- Error while building/testing on windows HOT 1
- How do I register a singleton qml file/component
- Qt 5.11 Support HOT 6
- Broken under Go 1.12 (relocation target runtime.acquirem not defined for ABI0 (but is defined for ABIInternal)) HOT 5
- build go-qml failure HOT 1
- How to connect Material?
- on Mac QT 5.13.0 + Go 1.12.7 installing error when did 'go get gopkg.in/qml.v1' HOT 1
- Installation on Ubuntu 19.10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from qml.