Qt Quick View Android 类

The QtQuickView class lets you easily add QML content to your Android app as a View .

Class: QtQuickView
Package Name: org.qtproject.qt.android
Extends: org.qtproject.qt.android.QtView

– org.qtproject.qt.android.QtLayout

–– android.view.ViewGroup

详细描述

The QtQuickView class lets you easily add QML content to your Android app as a View . QtQuickView instantiates a QQuickView with a given QML component source (a local or network file) and embeds it to itself. You can add it to your Android app's layout as with any other View. QtQuickView is a good choice when you want to extend your non-Qt Android app with QML content but do not want to make the entire app using the Qt framework. It brings the power of Qt Quick into your Android app, making it possible to use various Qt Quick APIs in Android apps.

A typical use of the class:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    QtQuickView qmlView = new QtQuickView(this, "qrc:/qt/qml/target/main.qml", "target");
    qmlView.setStatusChangeListener(status -> {
        Log.i(TAG, "QML loading status changed to " + status);
    });
    // Add QML to your layout
    layout.addView(qmlView, params);
    ...
}
					

For a more detailed example, see 在 Android Studio 工程中的 QML .

构造函数

public QtQuickView(Context parent, String qmlUri, String appName)

Creates a QtQuickView to load and render a QML component. Instantiating a QtQuickView will load the Qt libraries, including the app library specified by appName . Then, it creates a QQuickView that loads the QML source specified by qmlUri .

参数

  • context : the parent Context.
  • qmlUri : the URI of the main QML file.
  • appName : the name of the Qt app library to load and start. This corresponds to the target name set in the Qt app's CMakeLists.txt.

抛出

抛出 InvalidParameterException if a parameter is invalid.

public QtQuickView(Context context, String qmlUri, String appName, String[] qmlImportPaths)

Creates a QtQuickView to load and view a QML component. Instantiating a QtQuickView will load the Qt libraries, including the app library specified by appName . Then, it creates a QQuickView that loads the QML source specified by qmlUri . This overload accepts an array of strings qmlImportPaths in the case where the QML application should load QML modules from custom paths.

参数

  • context : the parent Context.
  • qmlUri : the URI of the main QML file.
  • appName : the name of the Qt app library to load and start. This corresponds to the target name set in the Qt app's CMakeLists.txt.
  • qmlImportPaths : an array of strings for additional import paths to be passed to.

抛出

抛出 InvalidParameterException if a parameter is invalid.

接口

public interface SignalListener<T>

Invoked on the Android UI thread when the signal has been emitted.

参数

  • signalName : literal signal name
  • : the value delivered by the signal or null if the signal is without a parameter.

公共接口 StatusChangeListener

Invoked on the Android UI thread when the QML component status has changed.

参数

  • status : The current status.

字段

状态值

The status can be STATUS_NULL , STATUS_READY , STATUS_LOADING or STATUS_ERROR 。更多信息,见 QQuickView::Status .

方法

public void setProperty(String propertyName, Object value)

Sets the value of an existing property on the QML root object. The supported types are 整数 , Double , Float , 布尔 ,和 字符串 . These types get converted to their corresponding QML types int, double/float, bool, and string. This function does not add properties to the QML root object if they do not exist.

参数

  • propertyName : the name of the existing root object property to set its value
  • : the value of the property

public <T extends Object> T getProperty(String propertyName)

Gets the value of an existing property of the QML root object. The supported return types are 整数 , Double , Float , 布尔 ,和 字符串 . These types get converted from their corresponding QML types int, double/float, bool, and string.

参数

  • propertyName : the name of the existing root object property.

返回

If the property does not exist or the status of the QML component is anything other than STATUS_READY , this function will return null.

抛出

抛出 ClassCastException 若类型铸造失败。

public <T> int addSignalListener(String signalName, Class<T> argType, SignalListener<T> listener)

Associates a SignalListener with a signal of the QML root object.

参数

  • signalName : the name of the root object signal.
  • argType : the Class type of the signal argument.
  • listener : an instance of the SignalListener interface.

返回

A Connection ID between signal and listener or the existing connection ID if there is an existing connection between the same signal and listener. Returns a negative value if the signal does not exist on the QML root object.

public boolean removeSignalListener(int signalListenerId)

停止 SignalListener with a given id obtained from addSignalListener() call, from listening to a signal.

参数

  • signalListenerId :连接 ID。

返回

True if the connection ID is valid and has been successfully removed, otherwise returns false.

public int getStatus()

获取 status of the QML component.

返回

STATUS_READY when the QML is ready. Invoking methods that operate on the QML root object, such as setProperty() , getProperty() ,和 addSignalListener() , would succeed only if the current status is STATUS_READY . It can also return other status values representing the status of the underlying QQuickView 实例。

public void setStatusChangeListener(StatusChangeListener listener)

设置 StatusChangeListener to listen to status changes.

参数