Qthread在Golang中仅发出一个信号

时间:2019-05-19 05:25:05

标签: qt go qthread qobject

我正在使用Golang和Qt编写程序。我的Qthread任务繁重。

这是我的代码:

MainUi.go

// Start new thread first
workThread := core.NewQThread(nil)
workThread.Start()

// Start a worker
wkr := worker.InitWorkPool()
wkr.MoveToThread(workThread)
wkr.ConnectReportOne(func(v0 int, v1 int) {

    // I put this code here to check is it called but not see anything
    fmt.Println("Report One") 

    label1.SetText(strconv.Itoa(v0))
    label2.SetText(strconv.Itoa(v1))
}
wkr.ConnectReportTwo(func(v0 int, v1 int, v2 int) {
    label3.SetText(strconv.Itoa(v0))
    label4.SetText(strconv.Itoa(v1))
    label5.SetText(strconv.Itoa(v2))

}
wkr.ConnectFinished(func() {
    widgets.QMessageBox_Information(nil, "Finished", "Task finished", widgets.QMessageBox__Ok, widgets.QMessageBox__Ok)
}
wkr.Run()

Worker.go

type WorkPool struct {
    core.QObject
    _ func(int, int) `signal:"reportOne"`
    _ func() `signal:"finished"`
    _ func(int, int, int) `signal:"reportTwo"`
}

func InitWorkPool() *WorkPool {
    return &WorkPool{}
}

func (wp *WorkPool) Run() {
    wp.ReportOne(11, 12) // This func not work
    wp.ReportTwo(21, 22, 23) // This func not work

    /* Do some stuff */

    wp.Finished() //This func work
}

我运行qtmoc来生成一些附加代码。

如果我先调用函数wp.Finished()或通过StopTask()之类的其他函数调用它,它将起作用。但是wp.ReportOne()wp.ReportTwo()无法正常工作并冻结MainUi

任何人都可以告诉我代码中的错误。我花了两天。

感谢阅读。

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。这是我的解决方法

func InitWorkPool() *WorkPool {
    wp := NewWorkPool() // Not return &WorkPool{}
    return wp
}

顺便说一句,您还使用go routines替换了Go中的Qthread

示例来源:https://github.com/therecipe/qt/issues/567#issuecomment-374654006