Skip to content

Instantly share code, notes, and snippets.

@saward
Last active August 29, 2015 13:57
Show Gist options
  • Save saward/9717205 to your computer and use it in GitHub Desktop.
Save saward/9717205 to your computer and use it in GitHub Desktop.
Error with uint32
package main
import (
"fmt"
"github.com/niemeyer/qml"
"image/color"
"math/rand"
"os"
"time"
)
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func run() error {
qml.Init(nil)
engine := qml.NewEngine()
colors := &Colors{}
engine.Context().SetVar("colors", colors)
component, err := engine.LoadFile("delegate.qml")
if err != nil {
return err
}
window := component.CreateWindow(nil)
window.Show()
n := func() uint8 { return uint8(rand.Intn(256)) }
for i := 0; i < 5; i++ {
colors.Add(color.RGBA{n(), n(), n(), 0xff})
}
go func() {
for i := 0; i < 10; i++ {
for i := 0; i < 5; i++ {
colors.Change(i, color.RGBA{n(), n(), n(), 0xff})
time.Sleep(1 * time.Second)
}
}
}()
window.Wait()
return nil
}
type Colors struct {
list []*ColorItem
Len int
}
type ColorItem struct {
Color color.RGBA
Someval uint32
}
func (colors *Colors) Add(c color.RGBA) {
colors.list = append(colors.list, &ColorItem{Color: c})
colors.Len = len(colors.list)
qml.Changed(colors, &colors.Len)
}
func (colors *Colors) Color(index int) *ColorItem {
return colors.list[index]
}
func (colors *Colors) Change(index int, c color.RGBA) {
item := colors.list[index]
item.Color = c
qml.Changed(item, &item.Color)
}
import QtQuick 2.0
Item {
width: 320; height: 200
ListView {
width: 120;
model: colors.len
delegate: Text {
text: "I am color number: " + index + " with someval: " + colors.color(index).someval
color: colors.color(index).color
}
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.horizontalCenter: parent.horizontalCenter
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment