Skip to main content

qtbridge_type_lib/testlib/qsignalspy/
mod.rs

1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only
3
4use crate::{QObject, QVariantList};
5use cxx::UniquePtr;
6use std::pin::Pin;
7#[cxx::bridge]
8mod ffi {
9    unsafe extern "C++" {
10        include!("qtbridge-type-lib/src/testlib/qsignalspy/cpp/qsignalspy.h");
11        /// The `QSignalSpy` is a struct that enables introspection of signal emission.
12        ///
13        /// A `QSignalSpy` can connect to a signal of a `QObject` and record each time the signal is emitted.
14        /// `QSignalSpy` itself is a `QVariantList`. Each emission of the signal will append one item to the list, containing the arguments of the signal.
15        ///
16        /// See also: [QSignalSpy documentation](https://doc.qt.io/qt-6/qsignalspy.html#details).
17        type QSignalSpy;
18        include!("qtbridge-type-lib/src/core/qlist/cpp/qlist_qvariant.h");
19        type QList_QVariant = crate::QList_QVariant;
20        include!("qtbridge-type-lib/src/core/qobject/cpp/qobject.h");
21        type QObject = crate::QObject;
22    }
23    #[namespace = "rust::bridge::qsignalspy"]
24    unsafe extern "C++" {
25        # [rust_name = inline_cpp_fn_new]
26        fn inlineCppFn_new(qobject: &QObject, signal_name: &str) -> UniquePtr<QSignalSpy>;
27        # [rust_name = inline_cpp_fn_count]
28        fn inlineCppFn_count(_obj: &QSignalSpy) -> isize;
29        # [rust_name = inline_cpp_fn_take_at]
30        fn inlineCppFn_take_at(_obj: Pin<&mut QSignalSpy>, idx: isize) -> QList_QVariant;
31        # [rust_name = inline_cpp_fn_take_first]
32        fn inlineCppFn_take_first(_obj: Pin<&mut QSignalSpy>) -> QList_QVariant;
33        # [rust_name = inline_cpp_fn_take_last]
34        fn inlineCppFn_take_last(_obj: Pin<&mut QSignalSpy>) -> QList_QVariant;
35    }
36}
37#[allow(dead_code)]
38#[allow(unused_imports)]
39pub use ffi::QSignalSpy;
40impl QSignalSpy {
41    #[allow(dead_code)]
42    /// Creates a new `QSignalSpy` that listens for emissions of a signal of the `qobject`.
43    ///
44    /// # Arguments
45    ///
46    /// * `signal_name` - The name of the signal function to observe.
47    pub fn new(qobject: &QObject, signal_name: &str) -> UniquePtr<Self> {
48        let cpp = ffi::inline_cpp_fn_new;
49        let ptr = cpp(qobject, signal_name);
50        if ptr.is_null() {
51            panic!("Failed to create QSignalSpy")
52        }
53        ptr
54    }
55    #[allow(dead_code)]
56    /// Returns the number of items currently stored in the underlying [QList][crate::QList].
57    pub fn count(&self) -> isize {
58        let cpp = ffi::inline_cpp_fn_count;
59        cpp(self)
60    }
61    #[allow(dead_code)]
62    /// Removes the element at position `idx` from the underlying [QList][crate::QList] and returns it.
63    pub fn take_at(mut self: Pin<&mut Self>, idx: isize) -> QVariantList {
64        let cpp = ffi::inline_cpp_fn_take_at;
65        cpp(self.as_mut(), idx)
66    }
67    #[allow(dead_code)]
68    /// Removes the first item from the underlying [QList][crate::QList] and returns it.
69    pub fn take_first(mut self: Pin<&mut Self>) -> QVariantList {
70        let cpp = ffi::inline_cpp_fn_take_first;
71        cpp(self.as_mut())
72    }
73    #[allow(dead_code)]
74    /// Removes the last item from the underlying [QList][crate::QList] and returns it.
75    pub fn take_last(mut self: Pin<&mut Self>) -> QVariantList {
76        let cpp = ffi::inline_cpp_fn_take_last;
77        cpp(self.as_mut())
78    }
79}