duanchui1279 2019-05-19 05:25
浏览 122

Qthread在Golang中仅发出一个信号

I'm working on program with use Golang and Qt. I have a problem with Qthread for a heavy task.

This is my code:

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
}

I ran qtmoc to generation some addition code.

A function wp.Finished() work if I call it first or via another function like StopTask(). But wp.ReportOne() and wp.ReportTwo() not work and freeze MainUi.

Anyone can tell me what wrong in my code. It took me 2 days.

Thanks for reading.

  • 写回答

1条回答 默认 最新

  • douhuan1648 2019-05-19 07:31
    关注

    I solved my problem. This is my solution

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

    By the way, you use go routines as well to replace Qthread in Go.

    Example source: https://github.com/therecipe/qt/issues/567#issuecomment-374654006

    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示