Using QtAbstractItemModel in Android Studio Projects | Qt Quick (original) (raw)

The Qt Quick for Android API examples are provided as Android Studio projects. The project folders are found in your Qt install location.

For example, under the default Windows install path, they are found here:

C:\Qt\Examples\Qt-/1\platforms\android

Overview

This example consists of two projects: an Android Studio project (qtabstractitemmodel_java) and a QML project (qtabstractitemmodel). You can import the QML project into an Android project.

The example shows how to handle complex data types between Java and QML. It demonstrates how to use the QtAbstractItemModel and QtModelIndex Java API classes. In QML, the data usage is demonstrated with the TableView item. In Java, data usage is demonstrated with a model of nested ArrayList items for rows and columns. For more information on how QML works, see Qt Qml.

Running the example

To run this example, you need Android Studio and Qt Tools for Android Studio on top of a standard Qt for Android installation. Open qtabstractitemmodel_java in Android Studio and follow the instructions in Qt Tools for Android Studio to import the qtabstractitemmodel.

QML project

On the QML project side, the example uses a Rectangle as the root object. The dataModel property variable holds the data model created and delivered from the Java side.

Rectangle { id: mainRectangle

property AbstractItemModel dataModel

TableView displays our data model.

[TableView](qml-qtquick-tableview.html) {
    id: tableView

    model: mainRectangle.dataModel

    anchors {fill: parent; margins: 20}
    columnSpacing: 4
    rowSpacing: 6
    boundsBehavior: TableView.OvershootBounds
    clip: true

    ScrollBar.vertical: ScrollBar {
       policy: ScrollBar.AsNeeded
    }
    ScrollBar.horizontal: ScrollBar{
       policy: ScrollBar.AsNeeded
    }

On the delegate property, each cell item of the model is defined with a Rectangle containing a TextEdit. The text property of the TextEdit is set using the QAbstractItemModel::data() which returns a value based on the given role and index.

Calling these methods from QML means the execution takes place in the Qt qtMainLoopThread thread context.

    delegate: Rectangle {
        implicitWidth: (tableView.height > tableView.width) ? tableView.width / 10 : tableView.height / 5
        implicitHeight: implicitWidth

        required property [var](qml-var.html) model

        color: "#2CDE85"
        border {color: "#00414A"; width: 2}

        [TextEdit](qml-qtquick-textedit.html) {
            // Calls MyDataModel::data to get data based on the roles.
            // Called in Qt qtMainLoopThread thread context.
            //
            // After editing is finished, call MyDataModel::setData()
            // to update the value of selected cell.
            onEditingFinished: parent.model.edit = text

            text: parent.model.display
            [font](qml-font.html) {pixelSize: 26; bold: true}
            padding: 5
            anchors.fill: parent
            wrapMode: TextEdit.Wrap
            horizontalAlignment: TextEdit.AlignHCenter
            verticalAlignment: TextEdit.AlignVCenter
        }
    }

In the case of editing the TextEdit field, onEditingFinished() handler sets the model's edit role value to the edited text. This calls the QAbstractItemModel::setData() method, where the edited text of the cell is updated to the corresponding index of the model.

For more information see QAbstractItemModel.

Android Studio project

The Android Studio project (qtabstractitemmodel_java) contains one Activity class MainActivity and MyDataModel class.

Data Model

The data model, MyDataModel, extends QtAbstractItemModel class. The QtAbstractItemModel is a wrapper for QAbstractItemModel.

As the methods of MyDataModel class are called from both, QML and Android sides, the execution occurs in both thread contexts, Qt qtMainLoopThread, and Android main thread contexts. You must ensure synchronization when accessing member variables in methods of the MyDataModel class.

First, the example initializes the model with a simple row and column mock data set. Note that this constructor method is called in the Android main thread context.

/*

The example overrides the QtAbstractItemModel methods for different purposes. The columnCount() and rowCount() methods return the count of each in a model. The execution of each rowCount() occurs in both thread contexts, Qt qtMainLoopThread and Android main thread contexts.

/*

/*

Method data() provides model data based on the role and index from Java to QML. The roleNames() method returns a hash matching numerical role values to their names as strings; in QML, we use these role names to fetch corresponding data from the model. The index() method returns the new model index. Method parent() should return a parent of the index. Still, as this example focuses on data without parent indices, we override the method and return an empty QtModelIndex(). As the methods are called from QML, the execution occurs in the Qt qtMainLoopThread thread context.

/*

/*

/*

/*

The example overrides the QAbstractItemModel::setData() method, which is called when model's data at index is set from the QML side of the application.

/*

The example implements methods on the model side for MainActivity UI interaction to add and remove rows and columns. Calls begin, end, insert, and remove rows to update model indexes, like beginInsertRow(). Because the example uses the QtAbstractItemModel, it must call beginInsertRows() and endInsertRows() every time it inserts new rows into the model. The same applies to removal. As the methods are called from the Android side, the execution takes place in the Android main thread context.

/*

/*

The example implements methods on the model side for MainActivity UI interaction to add and remove columns. Calls begin, end, insert, and remove columns to update model indexes, like beginRemoveColumn(). The same context awareness applies as with the add and remove row methods.

/*

/*

Main Activity

MainActivity implements the QtQmlStatusChangeListener interface to get status updates when the QML is loaded. It is also the main Android activity.

The example creates and initializes the data model. See also QtQuickView

private final MyDataModel m_model = new MyDataModel();

The example sets the UI button and its listeners to allow the users to interact with the model via the UI.

/*

/*

The example starts loading the QML content. Loading happens in the background until the ready status is updated.

/*

/*

/*

/*

The example sets the data model when the QML content is loaded, and the status is ready.

/*