Exemple #1
0
// On connects the named signal from obj with the provided function, so that
// when obj next emits that signal, the function is called with the parameters
// the signal carries.
//
// The provided function must accept a number of parameters that is equal to
// or less than the number of parameters provided by the signal, and the
// resepctive parameter types must match exactly or be conversible according
// to normal Go rules.
//
// For example:
//
//     obj.On("clicked", func() { fmt.Println("obj got a click") })
//
// Note that Go uses the real signal name, rather than the one used when
// defining QML signal handlers ("clicked" rather than "onClicked").
//
// For more details regarding signals and QML see:
//
//     http://qt-project.org/doc/qt-5.0/qtqml/qml-qtquick2-connections.html
//
func (obj *Common) On(signal string, function interface{}) {
	funcv := reflect.ValueOf(function)
	funct := funcv.Type()
	if funcv.Kind() != reflect.Func {
		panic("function provided to On is not a function or method")
	}
	if funct.NumIn() > C.MaxParams {
		panic("function takes too many arguments")
	}
	csignal, csignallen := unsafeStringData(signal)
	var cerr *C.error
	gui(func() {
		cerr = C.objectConnect(obj.addr, csignal, csignallen, obj.engine.addr, unsafe.Pointer(&function), C.int(funcv.Type().NumIn()))
		if cerr == nil {
			connectedFunction[&function] = true
			stats.connectionsAlive(+1)
		}
	})
	cmust(cerr)
}
Exemple #2
0
// On connects the named signal from obj with the provided function, so that
// when obj next emits that signal, the function is called with the parameters
// the signal carries.
//
// The provided function must accept a number of parameters that is equal to
// or less than the number of parameters provided by the signal, and the
// resepctive parameter types must match exactly or be conversible according
// to normal Go rules.
//
// For example:
//
//     obj.On("clicked", func() { fmt.Println("obj got a click") })
//
// Note that Go uses the real signal name, rather than the one used when
// defining QML signal handlers ("clicked" rather than "onClicked").
//
// For more details regarding signals and QML see:
//
//     http://qt-project.org/doc/qt-5.0/qtqml/qml-qtquick2-connections.html
//
func (obj *Common) On(signal string, function interface{}) {
	if obj.connections == nil {
		obj.connections = make(map[string]map[uintptr]uintptr)
	}

	funcv := reflect.ValueOf(function)
	funct := funcv.Type()
	if funcv.Kind() != reflect.Func {
		panic("function provided to On is not a function or method")
	}
	if funct.NumIn() > C.MaxParams {
		panic("function takes too many arguments")
	}
	csignal, csignallen := unsafeStringData(signal)

	var cerr *C.error
	var connection uintptr
	RunMain(func() {
		cerr = C.objectConnect(
			obj.addr,
			csignal,
			csignallen,
			obj.engine.addr,
			unsafe.Pointer(&function),
			C.int(funcv.Type().NumIn()),
			(*unsafe.Pointer)(unsafe.Pointer(&connection)),
		)
		if cerr == nil {
			table, exists := obj.connections[signal]
			if exists == false {
				table = make(map[uintptr]uintptr)
				obj.connections[signal] = table
			}
			table[funcv.Pointer()] = connection
			connectedFunction[&function] = true
			stats.connectionsAlive(+1)
		}
	})

	cmust(cerr)
}