aboutsummaryrefslogtreecommitdiffstats
path: root/examples/androidextras/musiclist/doc/src/qtandroidextras-example-musiclist.qdoc
blob: ec9d19526097fccb810c03e56dd3ad74bb06eae8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtAndroidExtras module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

/*!
\title Qt JNI Music List
    \ingroup examples-qtandroidextras
    \example musiclist
    \brief Demonstrates how to exchange data from complex Java objects.

    \image musiclist.png

    This example demonstrates how to move around data from a Java ArrayList of
    objects over to Qt. The example uses Android APIs to retrieve a list of music
    tracks, and displays some information about them with QML.

    When the application starts, it displays a list of music tracks, showing the
    track name, artist, and duration.

    \include examples-run.qdocinc

    \section1 Create the Music Classes

    Let's create a Java class, \c MusicTrack, that defines some of the basic
    information about a track. In the Java side, create the following:

    \quotefromfile musiclist/android/src/org/qtproject/example/musiclist/MusicList.java
    \skipto class MusicTrack
    \printuntil /^\}/

    Create another class with the same variables or information on the C++ side.
    The definition for \c MusicTrack class is the following:

    \quotefromfile musiclist/musiclist.h
    \skipto class MusicTrack
    \printuntil };

    \note The \c MusicTrack class must be a \l{QObject}-derived class to be used
        with as a QML context property. For more information, see
        \l{Overview - QML and C++ Integration}{QML and C++ Integration}.

    \section1 Fetch the Music List

    To retrieve the music list, the Android APIs are used. Add the following method
    to find music tracks that are available on the system. This method returns
    an ArrayList of \c MusicTrack.

    \quotefromfile musiclist/android/src/org/qtproject/example/musiclist/MusicList.java
    \skipto package
    \printuntil /^\ {4}\}/
    \printline }

    Using the JNI helpers provided with Qt, call the previous method to
    first get an \l{QAndroidJniObject} containing an ArrayList of objects. In the
    C++ code, you need to go through the Java ArrayList and create a parallel list
    on C++. Add the following lines to do that:

    \quotefromfile musiclist/musiclist.cpp
    \skipto QAndroidJniObject
    \printuntil /^\ {4}\}/

    Then, add a function to return the resulted \l{QList}:

    \quotefromfile musiclist/musiclist.cpp
    \skipto MusicList::assembledMusicList
    \printuntil }

    To display the music list, create a \l{ListView} that uses the \l{QList}
    as a property. Register the property as follows:

    \quotefromfile musiclist/main.cpp
    \skipto MusicList
    \printuntil assembledMusicList

    In the QML code, define the model of the \l{ListView} as follows:

    \quotefromfile musiclist/main.qml
    \skipto model
    \printline model

    \sa {Qt for Android}, {Qt Android Extras}
*/