This is the 9th day of my participation in the August Wen Challenge.More challenges in August
Background:
When making QT for Android, our APP is a playback APP, there will be a need for full screen display. After trying various QT full-screen solutions, I found that it was impossible to achieve full screen, either including the navigation bar or the status bar. So we had to find a way to get to full screen.
Before reading this article, it is recommended to read the previous one, as this article requires some basic QT and Android interaction.
How to call Android methods in Qt
Qt for Android (2) — Qt to call custom Android methods in detail tutorial
Qt for Android (3) — Get Android Services in Qt
The QT method tries:
In QT, we often use the following methods to make an application full screen:
Method one:
QSCreen screen = QGuiApplication::primaryScreen();
int screenW = screen->geometry().width();
int screenH = screen->geometry().height();
Copy the code
The height information obtained in method 1 does not contain the height of the bottom navigation bar, so it is not the real height of the device.
Method 2:
QDesktopWidget *pDesktopW = QApplication::desktop();
QRect m_screenRect = pDesktopW->screenGeometry();
int screenW = m_screenRect.width();
int screenH = m_screenRect.height();
Copy the code
The height information obtained in method 2 does not include the height of the top status bar, so it cannot meet our requirements.
Break new ground:
After trying all the QT methods, it was found that the complete and real device width and height could not be obtained, so we need to adopt the Android method to obtain the width and height information from the Android layer.
Public void getScreenRealHeight() {system.out.println ("getScreenRealHeight"); /** @return */ public void getScreenRealHeight() {system.out.println ("getScreenRealHeight"); Display display = getWindowManager().getDefaultDisplay(); DisplayMetrics dm = new DisplayMetrics(); @SuppressWarnings("rawtypes") Class c; try { c = Class.forName("android.view.Display"); @SuppressWarnings("unchecked") Method method = c.getMethod("getRealMetrics", DisplayMetrics.class); method.invoke(display, dm); SystemInfo.PHONEWIDTH = dm.widthPixels; SystemInfo.PHONEHEIGHT = dm.heightPixels; System.out.println("getHasVirtualKey PHONEWIDTH:" + dm.widthPixels); System.out.println("getHasVirtualKey PHONEHEIGHT:" + dm.heightPixels); } catch (Exception e) { System.out.println("getHasVirtualKey Exception:" + e.toString()); e.printStackTrace(); }}Copy the code
Based on the previous article, we need to have a custom Activity that inherits from QtActivity and then call the above method on the custom Activity. Then get the value assigned to the custom class SystemInfo static variable, and then use Qt for Android (a) – Qt how to call the Android method in Qt to get the value in Qt, perfect to obtain the true width and height of the device.