Skip to content

Commit 728220c

Browse files
authored
Qvariant improvements (#308)
* Add QVariant::to_qvariantmap wrapper * Add QVariant::to_qstring wrapper * Add QVariant::type_name wrapper * Improve debug output, add some a few tests * Use tests module * Add Debug for QVariantList * Use more conscise debug write * Add QVariant::toInt wrapper
1 parent d1441fa commit 728220c

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

qttypes/src/qtcore/qlist/qvariantlist.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ impl IndexMut<usize> for QVariantList {
9191
}
9292
}
9393

94+
impl std::fmt::Debug for QVariantList {
95+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
96+
f.debug_map().entries(self.into_iter().enumerate()).finish()
97+
}
98+
}
99+
94100
impl<'a> IntoIterator for &'a QVariantList {
95101
type Item = &'a QVariant;
96102
type IntoIter = QListIterator<'a, QVariantList, QVariant>;
@@ -160,4 +166,16 @@ mod tests {
160166
assert_eq!(qs2.to_string(), "hello");
161167
assert_eq!(qba4.to_string(), "hello");
162168
}
169+
170+
#[test]
171+
fn qvariantlist_debug() {
172+
let mut list = QVariantList::default();
173+
list.push(42.into());
174+
list.push(QString::from("String!").into());
175+
list.push(QByteArray::from("Bytearray!").into());
176+
assert_eq!(
177+
format!("{:?}", list),
178+
"{0: QVariant(int: \"42\"), 1: QVariant(QString: \"String!\"), 2: QVariant(QByteArray: \"Bytearray!\")}"
179+
);
180+
}
163181
}

qttypes/src/qtcore/qvariant.rs

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt;
22

33
use crate::{
4-
cpp, cpp_class, QByteArray, QDate, QDateTime, QString, QStringList, QTime, QUrl, QVariantList,
4+
cpp, cpp_class, QByteArray, QDate, QDateTime, QString, QStringList, QTime, QUrl, QVariantList, QVariantMap
55
};
66

77
cpp_class!(
@@ -57,12 +57,54 @@ impl QVariant {
5757
})
5858
}
5959

60+
/// Wrapper around [`toMap()`][method] method.
61+
///
62+
/// [method]: https://doc.qt.io/qt-5/qvariant.html#toMap
63+
pub fn to_qvariantmap(&self) -> QVariantMap {
64+
cpp!(unsafe [self as "const QVariant*"] -> QVariantMap as "QVariantMap" {
65+
return self->toMap();
66+
})
67+
}
68+
69+
/// Wrapper around [`toString()`][method] method.
70+
///
71+
/// [method]: https://doc.qt.io/qt-5/qvariant.html#toString
72+
pub fn to_qstring(&self) -> QString {
73+
cpp!(unsafe [self as "const QVariant*"] -> QString as "QString" {
74+
return self->toString();
75+
})
76+
}
77+
78+
/// Wrapper around [`toInt()`][method] method.
79+
///
80+
/// [method]: https://doc.qt.io/qt-5/qvariant.html#toInt
81+
pub fn to_int(&self) -> u32 {
82+
cpp!(unsafe [self as "const QVariant*"] -> u32 as "int" {
83+
return self->toInt();
84+
})
85+
}
86+
87+
/// Wrapper around ['typeName()`][method] method.
88+
///
89+
/// [method]: https://doc.qt.io/qt-5/qvariant.html#typeName
90+
pub fn type_name(&self) -> QString {
91+
cpp!(unsafe [self as "const QVariant*"] -> QString as "QString" {
92+
return self->typeName();
93+
})
94+
}
95+
6096
// FIXME: do more wrappers
6197
}
6298

6399
impl fmt::Debug for QVariant {
64100
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65-
f.write_str(self.to_qbytearray().to_string().as_str())
101+
let data = self.to_qstring().to_string();
102+
let qtype = self.type_name().to_string();
103+
if data.len() == 0 {
104+
write!(f, "QVariant({})", qtype.as_str())
105+
} else {
106+
write!(f, "QVariant({}: \"{}\")", qtype.as_str(), data.as_str())
107+
}
66108
}
67109
}
68110

@@ -226,3 +268,29 @@ where
226268
(*a).clone().into()
227269
}
228270
}
271+
272+
#[cfg(test)]
273+
mod tests {
274+
use super::*;
275+
276+
#[test]
277+
fn qvariant_debug_qstring() {
278+
let qv: QVariant = QString::from("Hello, QVariant!").into();
279+
assert_eq!(qv.to_qstring().to_string(), "Hello, QVariant!");
280+
assert_eq!(format!("{:?}", qv), "QVariant(QString: \"Hello, QVariant!\")");
281+
}
282+
283+
#[test]
284+
fn qvariant_debug_bool() {
285+
let qv = QVariant::from(false);
286+
assert_eq!(qv.to_qstring().to_string(), String::from("false"));
287+
assert_eq!(format!("{:?}", qv), "QVariant(bool: \"false\")");
288+
}
289+
290+
#[test]
291+
fn qvariant_debug_int() {
292+
let qv = QVariant::from(313);
293+
assert_eq!(qv.to_int(), 313);
294+
assert_eq!(format!("{:?}", qv), "QVariant(int: \"313\")");
295+
}
296+
}

0 commit comments

Comments
 (0)