-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_qt.cpp
153 lines (130 loc) · 4.41 KB
/
test_qt.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "pybind11_qt/pybind11_qt.h"
#include <pybind11/pybind11.h>
#include <tuple>
namespace py = pybind11;
using namespace pybind11::literals;
PYBIND11_MODULE(qt, m)
{
// QString
m.def("create_qstring_with_emoji", []() {
return QString::fromUtf16(u"\U0001F600");
});
m.def("consume_qstring_with_emoji", [](QString const& qstring) {
return qstring.length();
});
m.def("qstring_to_stdstring", [](QString const& qstring) {
return qstring.toStdString();
});
m.def("stdstring_to_qstring", [](std::string const& string) {
return QString::fromStdString(string);
});
m.def("qstring_to_int", [](QString const& qstring) {
return qstring.toInt();
});
m.def("int_to_qstring", [](int value) {
return QString::number(value);
});
// QStringList
m.def("qstringlist_join", [](QStringList const& values, QString const& sep) {
return values.join(sep);
});
m.def("qstringlist_at", [](QStringList const& values, int index) {
return values.at(index);
});
// QMap
m.def("qmap_to_length", [](QMap<QString, QString> const& map) {
QMap<QString, int> res;
for (auto it = map.begin(); it != map.end(); ++it) {
res[it.key()] = it.value().size();
}
return res;
});
// QDateTime
m.def(
"datetime_from_string",
[](QString const& date, Qt::DateFormat format) {
return QDateTime::fromString(date, format);
},
"string"_a, "format"_a = Qt::DateFormat::ISODate);
m.def(
"datetime_to_string",
[](QDateTime const& datetime, Qt::DateFormat format) {
return datetime.toString(format);
},
"datetime"_a, "format"_a = Qt::DateFormat::ISODate);
// QVariant
m.def("qvariant_from_none", [](QVariant const& variant) {
return std::make_tuple(variant.userType() == QVariant::Invalid,
variant.isValid());
});
m.def("qvariant_from_int", [](QVariant const& variant) {
return std::make_tuple(variant.userType() == QVariant::Int, variant.toInt());
});
m.def("qvariant_from_bool", [](QVariant const& variant) {
return std::make_tuple(variant.userType() == QVariant::Bool, variant.toBool());
});
m.def("qvariant_from_str", [](QVariant const& variant) {
return std::make_tuple(variant.userType() == QVariant::String,
variant.toString());
});
m.def("qvariant_from_list", [](QVariant const& variant) {
return std::make_tuple(variant.userType() == QVariant::List, variant.toList());
});
m.def("qvariant_from_map", [](QVariant const& variant) {
return std::make_tuple(variant.userType() == QVariant::Map, variant.toMap());
});
m.def("qvariant_none", []() {
return QVariant();
});
m.def("qvariant_int", []() {
return QVariant(42);
});
m.def("qvariant_bool", []() {
return QVariant(true);
});
m.def("qvariant_str", []() {
return QVariant("hello world");
});
m.def("qvariant_list", [] {
QVariantMap subMap;
subMap["bar"] = 42;
subMap["moo"] = QVariantList{44, true};
QVariantList list;
list.push_back(33);
list.push_back(QVariantList{4, "foo"});
list.push_back(false);
list.push_back("hello");
list.push_back(QVariant());
list.push_back(subMap);
list.push_back(45);
return QVariant(list);
});
m.def("qvariant_map", []() {
QVariantMap map;
map["bar"] = 42;
map["moo"] = true;
map["baz"] = "world hello";
return map;
});
// QFlags
enum SimpleEnum { Value0 = 0x1, Value1 = 0x2, Value2 = 0x4 };
Q_DECLARE_FLAGS(SimpleEnumFlags, SimpleEnum);
py::enum_<SimpleEnum>(m, "SimpleEnum", py::arithmetic())
.value("Value0", Value0)
.value("Value1", Value1)
.value("Value2", Value2);
m.def("qflags_explode", [](SimpleEnumFlags const& flags) {
return std::make_tuple(flags.toInt(), flags.testFlag(Value0),
flags.testFlag(Value1), flags.testFlag(Value2));
});
m.def("qflags_create", [](bool v0, bool v1, bool v2) {
SimpleEnumFlags r;
if (v0)
r.setFlag(Value0);
if (v1)
r.setFlag(Value1);
if (v2)
r.setFlag(Value2);
return r;
});
}