File tree 5 files changed +88
-1
lines changed
tests/qt_types_standalone
5 files changed +88
-1
lines changed Original file line number Diff line number Diff line change 14
14
namespace rust {
15
15
namespace cxxqtlib1 {
16
16
17
+ using c_void = void ;
18
+
17
19
template <typename T, typename ... Args>
18
20
T
19
21
construct (Args... args)
Original file line number Diff line number Diff line change @@ -95,3 +95,33 @@ pub use qvariant::{QVariant, QVariantValue};
95
95
96
96
mod qvector;
97
97
pub use qvector:: { QVector , QVectorElement } ;
98
+
99
+ #[ cxx:: bridge]
100
+ mod ffi {
101
+ #[ namespace = "rust::cxxqtlib1" ]
102
+ unsafe extern "C++" {
103
+ include ! ( "cxx-qt-lib/common.h" ) ;
104
+ type c_void ;
105
+ }
106
+ }
107
+
108
+ /// This is a workaround for CXX missing support for `*mut c_void`/`*const c_void` types.
109
+ ///
110
+ /// To use this type add this to your bridge:
111
+ /// ```rust
112
+ /// # #[cxx::bridge]
113
+ /// # mod ffi {
114
+ /// #
115
+ /// #[namespace = "rust::cxxqtlib1"]
116
+ /// unsafe extern "C++" {
117
+ /// include!("cxx-qt-lib/common.h");
118
+ /// type c_void = cxx_qt_lib::c_void;
119
+ /// }
120
+ /// #
121
+ /// # }
122
+ /// #
123
+ /// # fn main() {}
124
+ /// ```
125
+ ///
126
+ /// See: <https://github.com/dtolnay/cxx/issues/1049>
127
+ pub use ffi:: c_void;
Original file line number Diff line number Diff line change @@ -34,11 +34,16 @@ mod ffi {
34
34
/// Returns the sibling at row for the current column. If there is no sibling at this position, an invalid QModelIndex is returned.
35
35
#[ rust_name = "sibling_at_row" ]
36
36
fn siblingAtRow ( self : & QModelIndex , row : i32 ) -> QModelIndex ;
37
+
38
+ /// Returns a `*mut c_void` pointer used by the model to associate the index with the internal data structure.
39
+ #[ rust_name = "internal_pointer_mut" ]
40
+ fn internalPointer ( self : & QModelIndex ) -> * mut c_void ;
37
41
}
38
42
39
43
#[ namespace = "rust::cxxqtlib1" ]
40
44
unsafe extern "C++" {
41
45
include ! ( "cxx-qt-lib/common.h" ) ;
46
+ type c_void = crate :: c_void ;
42
47
43
48
#[ doc( hidden) ]
44
49
#[ rust_name = "qmodelindex_init_default" ]
Original file line number Diff line number Diff line change 6
6
// SPDX-License-Identifier: MIT OR Apache-2.0
7
7
#pragma once
8
8
9
+ #include < QtCore/QAbstractListModel>
9
10
#include < QtCore/QModelIndex>
10
11
#include < QtCore/QStringListModel>
11
12
#include < QtTest/QTest>
12
13
13
14
#include " cxx-qt-gen/qmodelindex_cxx.cxx.h"
14
15
15
- class QModelIndexTest : public QObject
16
+ // We subclass from QAbstractListModel to have a valid model to use for
17
+ // access to createIndex();
18
+ class QModelIndexTest : public QAbstractListModel
16
19
{
17
20
Q_OBJECT
18
21
22
+ public:
23
+ int rowCount (const QModelIndex& parent = QModelIndex()) const override
24
+ {
25
+ return 0 ;
26
+ }
27
+
28
+ QVariant data (const QModelIndex& index,
29
+ int role = Qt::DisplayRole) const override
30
+ {
31
+ return QVariant ();
32
+ }
33
+
19
34
private Q_SLOTS:
20
35
void construct ()
21
36
{
@@ -38,4 +53,23 @@ private Q_SLOTS:
38
53
QCOMPARE (c.isValid (), true );
39
54
QCOMPARE (c.row (), 0 );
40
55
}
56
+
57
+ void internalPointer ()
58
+ {
59
+ const auto index = createIndex (0 , 0 , (void *)&my_data);
60
+
61
+ auto pointer = internal_pointer_qmodelindex (index );
62
+ QCOMPARE ((int *)pointer, &my_data);
63
+ }
64
+
65
+ void internalId ()
66
+ {
67
+ const auto index = createIndex (0 , 0 , (quintptr)1234 );
68
+
69
+ auto id = internal_id_qmodelindex (index );
70
+ QCOMPARE (id, 1234 );
71
+ }
72
+
73
+ private:
74
+ int my_data = 42 ;
41
75
};
Original file line number Diff line number Diff line change @@ -12,10 +12,18 @@ mod qmodelindex_cxx {
12
12
type QModelIndex = cxx_qt_lib:: QModelIndex ;
13
13
}
14
14
15
+ #[ namespace = "rust::cxxqtlib1" ]
16
+ unsafe extern "C++" {
17
+ include ! ( "cxx-qt-lib/common.h" ) ;
18
+ type c_void = cxx_qt_lib:: c_void ;
19
+ }
20
+
15
21
extern "Rust" {
16
22
fn construct_qmodelindex ( ) -> QModelIndex ;
17
23
fn read_qmodelindex ( i : & QModelIndex ) -> bool ;
18
24
fn clone_qmodelindex ( i : & QModelIndex ) -> QModelIndex ;
25
+ fn internal_pointer_qmodelindex ( i : & QModelIndex ) -> * mut c_void ;
26
+ fn internal_id_qmodelindex ( i : & QModelIndex ) -> usize ;
19
27
}
20
28
}
21
29
@@ -30,3 +38,11 @@ fn read_qmodelindex(i: &QModelIndex) -> bool {
30
38
fn clone_qmodelindex ( i : & QModelIndex ) -> QModelIndex {
31
39
i. clone ( )
32
40
}
41
+
42
+ fn internal_pointer_qmodelindex ( i : & QModelIndex ) -> * mut qmodelindex_cxx:: c_void {
43
+ i. internal_pointer_mut ( )
44
+ }
45
+
46
+ fn internal_id_qmodelindex ( i : & QModelIndex ) -> usize {
47
+ i. internal_id ( )
48
+ }
You can’t perform that action at this time.
0 commit comments