Created
August 2, 2016 12:47
-
-
Save gen2brain/2d56084e797d98cf26703142613d438b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"fmt" | |
"image/png" | |
"os" | |
"github.com/therecipe/qt/core" | |
"github.com/therecipe/qt/gui" | |
"github.com/therecipe/qt/widgets" | |
) | |
func main() { | |
widgets.NewQApplication(len(os.Args), os.Args) | |
var btn = widgets.NewQPushButton2("Hello World", nil) | |
var btn2 = widgets.NewQPushButton2("Hello World", nil) | |
var btn3 = widgets.NewQPushButton2("Hello World", nil) | |
// icon from filesystem is loaded and shown | |
icon := gui.NewQIcon5("images/refresh.png") | |
// this is data from bindata.go (go-bindata images/refresh.png) | |
// file is checked with pngfix refresh.png | |
// IDAT OK maximum 15 15 616 2730 refresh.png | |
data, err := Asset("images/refresh.png") | |
if err != nil { | |
fmt.Println("Asset:", err) | |
} | |
fmt.Println("len(data):", len(data)) | |
// png decode works with data | |
im, err := png.Decode(bytes.NewReader(data)) | |
if err != nil { | |
fmt.Println("png.Decode:", err) | |
} | |
// data2, bytes from png.Encode | |
buf := new(bytes.Buffer) | |
err = png.Encode(buf, im) | |
data2 := buf.Bytes() | |
fmt.Println("len(data2):", len(data2)) | |
// try to load pixmap from data | |
pixmap := gui.NewQPixmap() | |
fmt.Println("pixmap.LoadFromData2") | |
// returns libpng error: Read Error | |
ok := pixmap.LoadFromData2(string(data), "PNG", core.Qt__AutoColor) | |
if !ok { | |
ok = pixmap.LoadFromData2(string(data[:]), "PNG", core.Qt__AutoColor) | |
if !ok { | |
pixmap.LoadFromData2(string(data[:clen(data)]), "PNG", core.Qt__AutoColor) | |
} | |
} | |
icon2 := gui.NewQIcon() | |
icon2.AddPixmap(pixmap, gui.QIcon__Normal, gui.QIcon__Off) | |
fmt.Println("len(data):", len(data)) | |
fmt.Println("len(data2):", len(data2)) | |
// try to load pixmap from data2 (png.Encoded) | |
pixmap = gui.NewQPixmap() | |
fmt.Println("pixmap.LoadFromData2") | |
// returns libpng error: Read Error | |
ok = pixmap.LoadFromData2(string(data2), "PNG", core.Qt__AutoColor) | |
if !ok { | |
ok = pixmap.LoadFromData2(string(data2[:]), "PNG", core.Qt__AutoColor) | |
if !ok { | |
pixmap.LoadFromData2(string(data2[:clen(data2)]), "PNG", core.Qt__AutoColor) | |
} | |
} | |
icon3 := gui.NewQIcon() | |
icon3.AddPixmap(pixmap, gui.QIcon__Normal, gui.QIcon__Off) | |
btn.SetIcon(icon) | |
btn2.SetIcon(icon2) | |
btn3.SetIcon(icon3) | |
var window = widgets.NewQWidget(nil, 0) | |
var layout = widgets.NewQVBoxLayout() | |
layout.AddWidget(btn, 0, 0) | |
layout.AddWidget(btn2, 0, 0) | |
layout.AddWidget(btn3, 0, 0) | |
window.SetLayout(layout) | |
window.SetWindowTitle("Hello World Example") | |
window.Show() | |
widgets.QApplication_Exec() | |
} | |
// Finds first null byte and returns the length | |
func clen(n []byte) int { | |
for i := 0; i < len(n); i++ { | |
if n[i] == 0 { | |
return i | |
} | |
} | |
return len(n) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment