go-qml / qml

QML support for the Go language
Other
1.96k stars 189 forks source link

Problem with On() and threads #94

Closed neclepsio closed 9 years ago

neclepsio commented 9 years ago

The following code produces this error while executing On():

2014/09/04 16:21:59 qobject.cpp:683: QObject: Cannot create children for a parent that is in a different thread.
(Parent is QQuickMouseArea(0x2139e00), parent's thread is QThread(0x1e4a680), current thread is QSGRenderThread(0x213cc30)

The code is:

package main

import (
    "gopkg.in/qml.v1"
)

const gopher_qml = `
import QtQuick 2.2
import QtQuick.Controls 1.1

import GoExtensions 1.0

ApplicationWindow {
    title: "My Application"
    width: 700; height: 500

    Column {
        Gopher {
            id: gopher
            width: 600; height: 400

            MouseArea {
                objectName: "glSceneMouseArea"
                anchors.fill: parent
            }
        }
    }
}
`

type Gopher struct{}

func (r *Gopher) Paint(p *qml.Painter) {}

func main() {
    qml.Run(run)
}

func run() error {
    engine := qml.NewEngine()

    qml.RegisterTypes("GoExtensions", 1, 0, []qml.TypeSpec{{
        Init: func(g *Gopher, obj qml.Object) {
            setOn(obj)
        },
    }})

    component, _ := engine.LoadString("gopher.qml", gopher_qml)
    win := component.CreateWindow(nil)
    win.Show()
    win.Wait()
    return nil
}

func setOn(obj qml.Object) {
    mouseArea := obj.ObjectByName("glSceneMouseArea")
    mouseArea.On("pressed", func(mouseEvent qml.Object) {})
}

Even modifying Init in this way triggers the error.

...
        Init: func(g *Gopher, obj qml.Object) {
            qml.RunMain(func() { setOn(obj) })
        },
...
niemeyer commented 9 years ago

Thanks, this is the second report of this issue, but your example is cleaner, so I'll go with it.

I believe I understand what is the underlying issue, so expect a fix very soon.