qt icon indicating copy to clipboard operation
qt copied to clipboard

NewQImage LoadFromData broken

Open Aj5y8HBIFf opened this issue 3 years ago • 2 comments

I'm trying to use embedded image file and render it on screen. But LoadFromData does not seems to work. Is it a known issue?

Here is a example code.

package main

import (
	"embed"
	"github.com/therecipe/qt/core"
	"github.com/therecipe/qt/gui"
	"github.com/therecipe/qt/widgets"
	"os"
)

//go:embed img.png
var bindata embed.FS

type MyWidget struct {
	widget *widgets.QWidget
}

func New(parent *widgets.QWidget) *MyWidget {
	w := widgets.NewQWidget(parent, core.Qt__Drawer)
	myWidget := &MyWidget{widget: w}
	myWidget.widget.ConnectPaintEvent(myWidget.paintEvent)
	return myWidget
}

func (w *MyWidget) paintEvent(*gui.QPaintEvent) {
	painter := gui.NewQPainter2(w.widget)

	// This is not working. Even though the "by" variable does have the image content
	by, _ := bindata.ReadFile("img.png")
	im := gui.NewQImage()
	im.LoadFromData(by, len(string(by)), "PNG")
	painter.DrawImage8(core.NewQPoint2(0, 0), im)

	// This is working fine
	painter.DrawImage8(core.NewQPoint2(0, 0), gui.NewQImage9("img.png", "png"))

	painter.DestroyQPainter()
}

func main() {
	core.QCoreApplication_SetAttribute(core.Qt__AA_ShareOpenGLContexts, true)
	widgets.NewQApplication(len(os.Args), os.Args)

	mainWindow := widgets.NewQMainWindow(nil, 0)
	myWidget := New(nil)

	mainWindow.SetCentralWidget(myWidget.widget)
	mainWindow.SetFixedSize2(800, 600)
	mainWindow.Show()

	widgets.QApplication_Exec()
}

Aj5y8HBIFf avatar Mar 24 '22 03:03 Aj5y8HBIFf

I also have the same issue.

hcyang0207 avatar May 19 '22 16:05 hcyang0207

Has this issue been resolved? I'm encountering the same problem where QPixmap.LoadFromData is unable to load the content.

PantsuDango avatar Oct 08 '23 09:10 PantsuDango