The QJniArray class is a template class that represents an array in Java. 更多...
| 头: |
#include <QJniArray>
|
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
|
| qmake: |
QT += core
|
| Since: | Qt 6.8 |
| 继承: | QJniArrayBase |
| const_iterator | |
| const_reverse_iterator |
| QJniArray () | |
| QJniArray (Container && container ) | |
| QJniArray (QJniArray<Other> && other ) | |
(从 6.9 起)
|
QJniArray (QJniArrayBase::size_type size ) |
| QJniArray (QJniObject && object ) | |
| QJniArray (const QJniArray<Other> & other ) | |
| QJniArray (const QJniObject & object ) | |
| QJniArray (jarray array ) | |
| QJniArray (std::initializer_list<T> & list ) | |
| ~QJniArray () | |
| auto | arrayObject () const |
| QJniArray<T>::const_reference | at (QJniArrayBase::size_type i ) const |
| QJniArray<T>::iterator | begin () |
| QJniArray<T>::const_iterator | begin () const |
| QJniArray<T>::const_iterator | cbegin () const |
| QJniArray<T>::const_iterator | cend () const |
| QJniArray<T>::const_iterator | constBegin () const |
| QJniArray<T>::const_iterator | constEnd () const |
| QJniArray<T>::const_reverse_iterator | crbegin () const |
| QJniArray<T>::const_reverse_iterator | crend () const |
| QJniArray<T>::iterator | end () |
| QJniArray<T>::const_iterator | end () const |
| QJniArray<T>::reverse_iterator | rbegin () |
| QJniArray<T>::const_reverse_iterator | rbegin () const |
| QJniArray<T>::reverse_iterator | rend () |
| QJniArray<T>::const_reverse_iterator | rend () const |
| 容器 | toContainer (Container && container = {}) const |
| QJniArray<T> & | operator= (QJniArray<Other> && other ) |
| QJniArray<T> & | operator= (const QJniArray<Other> & other ) |
(从 6.9 起)
QJniArray<T>::reference
|
operator[] (QJniArrayBase::size_type i ) |
| QJniArray<T>::const_reference | operator[] (QJniArrayBase::size_type i ) const |
The QJniArray template makes it easy to work with Java methods that return or take a Java array.
注意:
Java arrays
can hold primitive types and objects. The array itself can be treated like a Java Object, and the JNI framework provides explicit APIs to work with such arrays. In addition, the Java class library provides container types such as
List
or
ArrayList
. Objects of those types can not be represented by a QJniArray. Instead, use
QJniObject
to call the class-specific member functions.
To create a QJniArray instance, either construct it from a corresponding C++ container:
QList<int> intList; const QJniArray intArray = QJniArray(intList);
or from an initializer list:
const QJniArray intArray{1, 2, 3};
QJniArray will create a new Java array and copy the C++-side data into it.
When calling functions that return an array via
QJniObject::callMethod
,譬如
char[] toCharArray()
in the Java
String
class, specify the return type as a C array (
jchar[]
in the following):
const auto charArray = stringObject.callMethod<jchar[]>("toCharArray");
The
charArray
variable will be of type
QJniArray<jchar>
, and hold a new global reference to the
jcharArray
JNI object.
Note that the arrays in the code snippets above are all const. Accessing elements in a const array is considerably more efficient than operating on a mutable array.
A QJniArray can also be constructed from an existing
jarray
or
QJniObject
. However, note that no type checking is performed to verify that the
jarray
or
QJniObject
indeed represents an array holding elements of the specified type, and accessing a mismatching QJniArray results in undefined behavior.
The data in a QJniArray can either be accessed element by element using at () 或 operator[] (), or iterated over.
for (const auto &value : array) process(value);
To copy the entire array into a C++ side Qt container, use the toContainer () 函数。
const auto bytes = object.callMethod<jbyte[]>("getBytes"); QByteArray data = bytes.toContainer();
which happens implicitly in
const auto data = object.callMethod<QByteArray>("getBytes");
The return type of
toContainer
() depends on the type that QJniArray has been instantiated with. For
QJniArray<T>
this will typically be
QList<T>
, with the following exceptions:
| Specialization | C++ 类型 |
|---|---|
| QJniArray<jbyte> | QByteArray |
| QJniArray<char> | QByteArray |
| QJniArray<jstring> | QStringList |
| QJniArray< QString > | QStringList |
An array of a fixed size can also be created without any data, and can then be populated element by element using operator[] :
QJniArray<jint> intArray(size); for (int i = 0; i < size; ++i) intArray[i] = i;
or a mutable iterator:
QJniArray<QString> strings(size); int i = 0; for (auto string : strings) // note: not 'auto &string' string = u"Row %1"_s.arg(i++);
As in Java, the size of an array can not be changed, but the array variable can be assigned to a different array.
注意:
Java arrays are limited to 32 bits, and the
size_type
member type of QJniArray is
jsize
, which is a 32bit integer type. Trying to construct a QJniArray from a C++ container that holds more than 2^32 elements will cause a runtime assertion.
[alias]
QJniArray::
const_iterator
A random-access, const iterator for QJniArray .
[alias]
QJniArray::
const_reverse_iterator
A reverse iterator for the
QJniArray
, synonym for
std::reverse_iterator<const_iterator>
.
[noexcept]
QJniArray
<
T
>
::iterator
QJniArray::
begin
()
[noexcept]
QJniArray
<
T
>
::const_iterator
QJniArray::
begin
() const
[noexcept]
QJniArray
<
T
>
::const_iterator
QJniArray::
cbegin
() const
[noexcept]
QJniArray
<
T
>
::const_iterator
QJniArray::
constBegin
() const
返回 STL-style const iterator pointing to the first item in the array.
If the array is 无效 , then this will return the same iterator as the corresponding end () 函数。
[noexcept]
QJniArray
<
T
>
::iterator
QJniArray::
end
()
[noexcept]
QJniArray
<
T
>
::const_iterator
QJniArray::
cend
() const
[noexcept]
QJniArray
<
T
>
::const_iterator
QJniArray::
constEnd
() const
[noexcept]
QJniArray
<
T
>
::const_iterator
QJniArray::
end
() const
返回 STL 样式迭代器 pointing just after the last item in the list.
[noexcept]
QJniArray
<
T
>
::reverse_iterator
QJniArray::
rbegin
()
[noexcept]
QJniArray
<
T
>
::const_reverse_iterator
QJniArray::
crbegin
() const
[noexcept]
QJniArray
<
T
>
::const_reverse_iterator
QJniArray::
rbegin
() const
返回 STL-style reverse iterator pointing to the first item in the array, in reverse order.
If the array is 无效 , then this will return the same iterator as the corresponding rend () 函数。
[noexcept]
QJniArray
<
T
>
::reverse_iterator
QJniArray::
rend
()
[noexcept]
QJniArray
<
T
>
::const_reverse_iterator
QJniArray::
crend
() const
[noexcept]
QJniArray
<
T
>
::const_reverse_iterator
QJniArray::
rend
() const
返回 STL-style reverse iterator pointing just after the last item in the list, in reverse order.
Returns the value at position i in the wrapped Java array.
i must be a valid index position in the list (i.e., 0 <= i < size ()).
另请参阅 size ().
Default constructor of QJniArray. This does not create a Java-side array, and the instance will be invalid.
另请参阅 isValid .
[explicit]
template <typename Container, QJniArrayBase::if_compatible_source_container<Container> = true> QJniArray::
QJniArray
(
容器
&&
container
)
Constructs a QJniArray that wraps a newly-created Java array for elements of type
Container::value_type
, and populates the Java array with the data from
container
.
Participates in overload resolution only if
Container
is a container that stores elements of a
JNI type
or equivalent C++ type, and provides a forward iterator.
The specialization of the constructed QJniArray depends on the value type of the
container
. For a
Container<T>
(such as e.g.
QList<T>
) it will typically be
QJniArray<T>
, with the following exceptions:
| 容器 | Specialization |
|---|---|
| QByteArray | QJniArray<jbyte> |
| QStringList | QJniArray<jstring> |
| Container::value_type | Specialization |
| QJniObject | QJniArray<jobject> |
另请参阅 QJniArrayBase::fromContainer () 和 toContainer ().
[noexcept]
template <typename Other, QJniArrayBase::if_convertible<Other, T> = true> QJniArray::
QJniArray
(
QJniArray
<
其它
> &&
other
)
Constructs a QJniArray by moving from other 。 other array becomes 无效 .
Participates in overload resolution only if the element type
Other
of
other
is convertible to the element type
T
of the QJniArray being constructed. However, no actual conversion takes place.
[explicit, since 6.9]
QJniArray::
QJniArray
(
QJniArrayBase::size_type
size
)
Constructs an empty QJniArray of size size . The elements in the array do not get initialized.
该函数在 Qt 6.9 引入。
[explicit noexcept]
QJniArray::
QJniArray
(
QJniObject
&&
object
)
Constructs a QJniArray by moving from object 。 QJniObject becomes 无效 .
注意: This constructor does not perform any validation of whether the Java-side object is an array of the correct type. Accessing a mismatching QJniArray results in undefined behavior.
Constructs a QJniArray by copying other . Both QJniArray objects will reference the same Java array object.
Participates in overload resolution only if the element type
Other
of
other
is convertible to the element type
T
of the QJniArray being constructed. However, no actual conversion takes place.
[explicit]
QJniArray::
QJniArray
(const
QJniObject
&
object
)
Constructs a QJniArray that wraps the same Java array as object , creating a new global reference. To construct a QJniArray from an existing local reference, use a QJniObject constructed via fromLocalRef ().
注意: This constructor does not perform any validation of whether the Java-side object is an array of the correct type. Accessing a mismatching QJniArray results in undefined behavior.
[explicit]
QJniArray::
QJniArray
(
jarray
array
)
Constructs a QJniArray that wraps the Java-side array array , creating a new global reference to array .
注意: This constructor does not perform any validation of whether the Java-side object is an array of the correct type. Accessing a mismatching QJniArray results in undefined behavior.
[default]
QJniArray::
QJniArray
(
std::initializer_list
<
T
> &
list
)
Constructs a QJniArray that wraps a newly-created Java array for elements of type
T
, and populates the Java array with the data from
list
.
另请参阅 QJniArrayBase::fromContainer () 和 toContainer ().
销毁 QJniArray object and releases any references held to the wrapped Java array.
Returns the wrapped Java object as the suitable
jarray
type that matches the element type
T
of this
QJniArray
对象。
| T | jarray type |
|---|---|
| jbyte | jbyteArray |
| jchar | jcharArray |
| ... | ... |
| jobject | jobjectArray |
| QJniObject | jobjectArray |
| Q_DECLARE_JNI_CLASS | jobjectArray |
Returns a container populated with the data in the wrapped Java array.
若无
container
is provided, then the type of the container returned depends on the element type of this
QJniArray
。对于
QJniArray<T>
this will typically be
QList<T>
, with the following exceptions:
| Specialization | C++ 类型 |
|---|---|
| QJniArray <jbyte> | QByteArray |
| QJniArray <char> | QByteArray |
| QJniArray <jstring> | QStringList |
| QJniArray < QString > | QStringList |
If you pass in a named container (an lvalue) for container , then that container is filled, and a reference to it is returned. If you pass in a temporary container (an rvalue, incl. the default argument), then that container is filled, and returned by value.
This function returns immediately if the array is 无效 .
另请参阅 QJniArrayBase::fromContainer ().
[noexcept]
template <typename Other, QJniArrayBase::if_convertible<Other, T> = true>
QJniArray
<
T
> &QJniArray::
operator=
(
QJniArray
<
其它
> &&
other
)
移动 other 到此 QJniArray , and returns a reference to this. The other array becomes 无效 .
Participates in overload resolution only if the element type
Other
of
other
is convertible to the element type
T
of this
QJniArray
. However, no actual conversion takes place.
赋值 other 到此 QJniArray , and returns a reference to this. Both QJniArray objects will reference the same Java array object.
Participates in overload resolution only if the element type
Other
of
other
is convertible to the element type
T
of this
QJniArray
. However, no actual conversion takes place.
[since 6.9]
QJniArray
<
T
>
::reference
QJniArray::
operator[]
(
QJniArrayBase::size_type
i
)
Returns a reference object for value at position i in the wrapped Java array.
i must be a valid index position in the list (i.e., 0 <= i < size ()).
The returned reference object holds the value at position i , and in most cases will convert implicitly to the value. Assigning to the returned reference will overwrite the entry in the Java array. However, calling mutating member functions on the object will not modify the entry in the array. To call member function on the result of this operator, dereference the reference object:
QJniArray<QString> strings = object.callMethod<QString[]>("getStrings"); if (!strings.isEmpty()) { if (!(*array[0]).isEmpty()) { // ... } }
However, if modifying the value in the array itself is not intended, make the array const, or use at () 代替。
该函数在 Qt 6.9 引入。