콜백 작동 방식
소개
Java API를 사용할 때 콜백을 구현해야 하는 상황은 다양합니다. 실제적인 예로는 ContentObserver 를 통해 콘텐츠 변경 사항을 수신하는 콜백, Handler.Callback 과 같은 Java 인터페이스를 구현하는 콜백이 있습니다.
QtJenny는 Java API가 사용하는 것에 따라 Java 인터페이스를 구현하는 경우와 Java 클래스를 확장하는 경우 모두 이러한 콜백을 구현할 수 있습니다. Android에서도 제공되는 많은 크로스 플랫폼 Java API는 인터페이스인 콜백을 사용하지만, Android는 일부 상황에서 추상 베이스 클래스도 사용하며, 대표적인 예로 ContentObserver 를 들 수 있습니다.
Java 인터페이스 구현하기
다음 다이어그램은 C++에서 Java 인터페이스를 구현하는 여러 부분 간의 구조와 논리적 구분을 보여줍니다.
작동 방식은 Java 인터페이스를 래핑하는 java.reflect.Proxy 을 사용하고, 인터페이스 메서드의 호출을 InvocationHandler 의 일반 invoke() 메서드로 전송하는 것입니다.
InvocationHandler 인터페이스는 프로젝트별 NativeInvocationHandler (애플리케이션에서 제공)에 의해 구현되며, 해당 구현은 프로젝트에서 제공하는 네이티브 제네릭 호출 메서드를 호출합니다. 네이티브 메서드는 Java 인터페이스를 모방하는 생성된 C++ 클래스에서 일반 qt_invoke() 가상 함수를 호출합니다.
마지막으로, Qt C++ 프로젝트의 사용자 코드는 생성된 클래스를 상속하고 그 안의 가상 함수를 재정의합니다.
Java 클래스 확장하기
다음 다이어그램은 C++에서 Java 클래스를 확장하는 여러 부분 간의 구조와 논리적 구분을 보여줍니다.
작동 방식은 Java 서브클래스를 생성한 다음 일반 invoke() 메서드를 호출하는 것입니다. NativeInvocationHandler], which is a project-supplied Java class. The \c {NativeInvocationHandler} calls a native generic invocation method that is also project-supplied. The native method calls a generic \c {qt_invoke()} virtual function in a generated C++ class that mimics the Java class. Finally, the user code in the Qt C++ project inherits that generated class, and overrides the virtual functions in it.
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.