Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*widgets.QPushButton not working for ConnectClicked(func(_ bool) { } #854

Closed
sndnvaps opened this issue May 2, 2019 · 3 comments
Closed

Comments

@sndnvaps
Copy link

sndnvaps commented May 2, 2019

dev platform:

 win7 x64
 qt 5.12.3 x86
 go 1.11.6 x86
 gocv version: 0.19.0
 opencv lib version: 4.0.1

my code is as below
mainform.ui is in videoCapture.zip

//mainform.go
package main

import (
	//"errors"
	"fmt"
	"github.com/satori/go.uuid"
	"github.com/therecipe/qt/core"
	"github.com/therecipe/qt/gui"
	"github.com/therecipe/qt/uitools"
	"github.com/therecipe/qt/widgets"
	"gocv.io/x/gocv"
	"os"
	//"strconv"
	//"time"
	//"unsafe"
)

var (
	webcam *gocv.VideoCapture
	window  *gocv.Window
)
type MainForm struct {
	core.QObject

	widget *widgets.QWidget
	PicBox *widgets.QLabel
	btn1   *widgets.QPushButton
	btn2   *widgets.QPushButton
	btn3   *widgets.QPushButton

	img    gocv.Mat


	m_timer *core.QTimer


	_ func() `constructor:"init"`
	_ func() `slot:"NextFrame,<-(this.m_timer.timeout)"`
	_ func() `slot:"Start"`
	_ func() `slot:"Stop"`
	_ func() `slot:TakePicture"`
}

func (w *MainForm) init() {

	w.m_timer = core.NewQTimer(nil)

	var loader = uitools.NewQUiLoader(nil)
	var file = core.NewQFile2("mainwindow.ui")

	file.Open(core.QIODevice__ReadOnly)
	w.widget = widgets.NewQWidgetFromPointer(loader.Load(file, nil).Pointer())
	file.Close()

	w.PicBox = widgets.NewQLabelFromPointer(w.FindChild("PicBox", core.Qt__FindChildrenRecursively).Pointer())
	w.btn1 = widgets.NewQPushButtonFromPointer(w.FindChild("startBtn", core.Qt__FindChildrenRecursively).Pointer())
	w.btn2 = widgets.NewQPushButtonFromPointer(w.FindChild("stopBtn", core.Qt__FindChildrenRecursively).Pointer())
	w.btn3 = widgets.NewQPushButtonFromPointer(w.FindChild("takePicture", core.Qt__FindChildrenRecursively).Pointer())

	// open WebCam
	w.btn1.ConnectClicked(func(_ bool) {
		w.Start()
	})

	// Stop WebCam
	w.btn2.ConnectClicked(func(_ bool) {
		w.Stop()
	})

	// Take Picture
	w.btn3.ConnectClicked(func(_ bool) {
		w.TakePicture()
	})

	w.ConnectStart(w.start)
	w.ConnectStop(w.stop)
	w.ConnectNextFrame(w.nextFrame)
	w.ConnectTakePicture(w.takePicture)
}

func Mat2QImage(mat gocv.Mat) *gui.QImage {
	dest := gocv.NewMat()
	defer dest.Close()

	gocv.CvtColor(mat, &dest, gocv.ColorBGRToRGB)
	data := dest.ToBytes()
	qimg := gui.NewQImage5(string(data), dest.Cols(), dest.Rows(), gui.QImage__Format_RGB888)

	qimg = qimg.RgbSwapped()
	return qimg
}

func (w *MainForm) start() {

	webcam, _ = gocv.OpenVideoCapture(0)
	defer webcam.Close()

	w.m_timer.SetInterval(50)
	for {
		img := gocv.NewMat()
		defer img.Close()
		if ok := webcam.Read(&img); !ok {
			fmt.Printf("Device closed: 0\n")
			return
		}
		/*
		ImageBox := gui.NewQPixmap()
		qimg := Mat2QImage(w.img)
		ImageBox = ImageBox.FromImage(qimg, core.Qt__AutoColor)
		w.PicBox.Clear()
		w.PicBox.SetPixmap(ImageBox)
		*/
		w.m_timer.Start2()
        //w.NextFrame()
	}
}

func (w *MainForm) stop() {
	w.m_timer.Stop()
	w.PicBox.SetPixmap(gui.NewQPixmap())
	window.Close()
}

func (w *MainForm) nextFrame() {

	window = gocv.NewWindow("Capture Window")
	defer window.Close()

	img := gocv.NewMat()
	defer img.Close()
	if ok := webcam.Read(&img); !ok {
		fmt.Printf("Device closed: 0\n")
		return
	}
	/*
	ImageBox := gui.NewQPixmap()
	qimg := Mat2QImage(w.img)
	ImageBox = ImageBox.FromImage(qimg, core.Qt__AutoColor)
	w.PicBox.Clear()
	w.PicBox.SetPixmap(ImageBox)
    */
	window.IMShow(img)
}

func (w *MainForm) takePicture() {
	uuid := uuid.Must(uuid.NewV4())
	fName := uuid.String() + ".jpg"
	fmt.Printf("fName = %s\n", fName)

	f, _ := os.Create(fName)
	defer f.Close()

	img := gocv.NewMat()
	defer img.Close()
	if ok := webcam.Read(&img); !ok {
		fmt.Printf("Device closed: 0\n")
		return
	}
	fmt.Println("50ms timeout: in TakePicture!")
	buf, _ := gocv.IMEncode(".jpg", img)
	f.Write(buf)

}


func main() {
	app := widgets.NewQApplication(len(os.Args), os.Args)
	window := NewMainForm(nil)
	window.widget.Show()
	app.Exec()
}

videoCapture.zip

@therecipe
Copy link
Owner

Hey

It should work with these changes:

 	var loader = uitools.NewQUiLoader(nil)
 	var file = core.NewQFile2("mainwindow.ui")
 
+	var widget = widgets.NewQWidget(nil, 0)
 	file.Open(core.QIODevice__ReadOnly)
-	w.widget = widgets.NewQWidgetFromPointer(loader.Load(file, nil).Pointer())
+	w.widget = widgets.NewQWidgetFromPointer(loader.Load(file, widget).Pointer())
 	file.Close()
 
-	w.PicBox = widgets.NewQLabelFromPointer(w.FindChild("PicBox", core.Qt__FindChildrenRecursively).Pointer())
-	w.btn1 = widgets.NewQPushButtonFromPointer(w.FindChild("startBtn", core.Qt__FindChildrenRecursively).Pointer())
-	w.btn2 = widgets.NewQPushButtonFromPointer(w.FindChild("stopBtn", core.Qt__FindChildrenRecursively).Pointer())
-	w.btn3 = widgets.NewQPushButtonFromPointer(w.FindChild("takePicture", core.Qt__FindChildrenRecursively).Pointer())
+	w.PicBox = widgets.NewQLabelFromPointer(widget.FindChild("PicBox", core.Qt__FindChildrenRecursively).Pointer())
+	w.btn1 = widgets.NewQPushButtonFromPointer(widget.FindChild("startBtn", core.Qt__FindChildrenRecursively).Pointer())
+	w.btn2 = widgets.NewQPushButtonFromPointer(widget.FindChild("stopBtn", core.Qt__FindChildrenRecursively).Pointer())
+	w.btn3 = widgets.NewQPushButtonFromPointer(widget.FindChild("takePicture", core.Qt__FindChildrenRecursively).Pointer())
 
 	// open WebCam
 	w.btn1.ConnectClicked(func(_ bool) {

It seems like you need to provide a parent when calling loader.Load, and then use that parent to find the children.

@sndnvaps
Copy link
Author

sndnvaps commented May 3, 2019

@therecipe thanks, code is working now .
but, need more fix

@sndnvaps sndnvaps closed this as completed May 3, 2019
@therecipe
Copy link
Owner

@sndnvaps I recently added support for the uic with 01e05b1

Maybe checkout the difference between these examples https://github.com/therecipe/qt/tree/master/internal/examples/uitools/calculator
and
https://github.com/therecipe/qt/tree/master/internal/examples/uitools/calculator_manual

for this to work you will only need to update the binding and then ut your *.ui files inside a folder called ui, then the binding (qtrcc/qtdeploy) should generate all the widget.FindChild( functions for you automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants