在.java文件重写onAttachedToWindow()函数
然后通过safeArea() 将上下左右的安全区域发出去
public native static void safeArea(int top, int left, int bottom, int right);
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
System.out.println("java -> on attached to window");
if( android.os.Build.VERSION.SDK_INT >= 28 )
{
// // 获取屏幕安全边界
WindowInsets windowInsets = getWindow().getDecorView().getRootWindowInsets();
if( windowInsets != null )
{
DisplayCutout displayCutout = windowInsets.getDisplayCutout();
if( displayCutout != null ) {
int left = displayCutout.getSafeInsetLeft();
int top = displayCutout.getSafeInsetTop();
int right = displayCutout.getSafeInsetRight();
int bottom = displayCutout.getSafeInsetBottom();
safeArea(top, left, bottom, right);
}
else
{
System.out.println("java -> displayCutout is null");
}
}
else
{
System.out.println("java -> windowInsets is null");
}
}
}
Qt .cpp里面接收safeArea()
static int cutOutTop = 0;
static int cutOutLeft = 0;
static int cutOutBottom = 0;
static int cutOutRight = 0;
#ifdef Q_OS_ANDROID
#ifdef __cplusplus
extern "C" {
#endif
// Java_org_qtproject_example_function_ 为.java文件package字段
// 后面safeArea 为.java文件的方法名
JNIEXPORT void JNICALL Java_org_qtproject_example_function_safeArea(JNIEnv */*env*/,
jobject /*obj*/,
int top, int left, int bottom, int right)
{
cutOutTop = top;
cutOutLeft = left;
cutOutBottom = bottom;
cutOutRight = right;
}
#ifdef __cplusplus
}
#endif
#endif
获取到的安全区域值为分辨率的大小
非物理屏幕大小
转换为物理屏幕大小
QScreen *screen = QGuiApplication::primaryScreen();
int top = cutOutTop / screen->devicePixelRatio();
int left = cutOutLeft / screen->devicePixelRatio();
int bottom = cutOutBottom / screen->devicePixelRatio();
int right = cutOutRight / screen->devicePixelRatio();