From d1441fab9a1be464a365d68bd9ea116116bbcf07 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Wed, 7 Feb 2024 09:30:01 +0100 Subject: [PATCH 1/5] Fix Qt linkage on macOS when Qt was configured with -no-framework Detect the presents of QtCore.framework and link it as framework, otherwise link it "regular unix style". --- qttypes/build.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/qttypes/build.rs b/qttypes/build.rs index 1a6e0f6..a14aacf 100644 --- a/qttypes/build.rs +++ b/qttypes/build.rs @@ -197,9 +197,12 @@ fn main() { println!("cargo:FOUND=1"); println!("cargo:COMPILE_FLAGS={}", flags.join(";")); - let macos_lib_search = if cargo_target_os == "macos" { "=framework" } else { "" }; + let use_macos_frameworks = + cargo_target_os == "macos" && Path::new(&qt_library_path).join("QtCore.framework").exists(); + + let macos_lib_search = if use_macos_frameworks { "=framework" } else { "" }; let vers_suffix = - if cargo_target_os == "macos" { "".to_string() } else { qt_version.major.to_string() }; + if use_macos_frameworks { "".to_string() } else { qt_version.major.to_string() }; // Windows debug suffix exclusively from MSVC land let debug = std::env::var("DEBUG").ok().map_or(false, |s| s == "true"); From 728220c0035ca6c85a99ce2fe5db8bb654ce5492 Mon Sep 17 00:00:00 2001 From: Matti Viljanen Date: Sun, 25 Feb 2024 22:01:10 +0200 Subject: [PATCH 2/5] 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 --- qttypes/src/qtcore/qlist/qvariantlist.rs | 18 ++++++ qttypes/src/qtcore/qvariant.rs | 72 +++++++++++++++++++++++- 2 files changed, 88 insertions(+), 2 deletions(-) diff --git a/qttypes/src/qtcore/qlist/qvariantlist.rs b/qttypes/src/qtcore/qlist/qvariantlist.rs index b207009..1681a6d 100644 --- a/qttypes/src/qtcore/qlist/qvariantlist.rs +++ b/qttypes/src/qtcore/qlist/qvariantlist.rs @@ -91,6 +91,12 @@ impl IndexMut for QVariantList { } } +impl std::fmt::Debug for QVariantList { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_map().entries(self.into_iter().enumerate()).finish() + } +} + impl<'a> IntoIterator for &'a QVariantList { type Item = &'a QVariant; type IntoIter = QListIterator<'a, QVariantList, QVariant>; @@ -160,4 +166,16 @@ mod tests { assert_eq!(qs2.to_string(), "hello"); assert_eq!(qba4.to_string(), "hello"); } + + #[test] + fn qvariantlist_debug() { + let mut list = QVariantList::default(); + list.push(42.into()); + list.push(QString::from("String!").into()); + list.push(QByteArray::from("Bytearray!").into()); + assert_eq!( + format!("{:?}", list), + "{0: QVariant(int: \"42\"), 1: QVariant(QString: \"String!\"), 2: QVariant(QByteArray: \"Bytearray!\")}" + ); + } } diff --git a/qttypes/src/qtcore/qvariant.rs b/qttypes/src/qtcore/qvariant.rs index e6bd064..434391f 100644 --- a/qttypes/src/qtcore/qvariant.rs +++ b/qttypes/src/qtcore/qvariant.rs @@ -1,7 +1,7 @@ use std::fmt; use crate::{ - cpp, cpp_class, QByteArray, QDate, QDateTime, QString, QStringList, QTime, QUrl, QVariantList, + cpp, cpp_class, QByteArray, QDate, QDateTime, QString, QStringList, QTime, QUrl, QVariantList, QVariantMap }; cpp_class!( @@ -57,12 +57,54 @@ impl QVariant { }) } + /// Wrapper around [`toMap()`][method] method. + /// + /// [method]: https://doc.qt.io/qt-5/qvariant.html#toMap + pub fn to_qvariantmap(&self) -> QVariantMap { + cpp!(unsafe [self as "const QVariant*"] -> QVariantMap as "QVariantMap" { + return self->toMap(); + }) + } + + /// Wrapper around [`toString()`][method] method. + /// + /// [method]: https://doc.qt.io/qt-5/qvariant.html#toString + pub fn to_qstring(&self) -> QString { + cpp!(unsafe [self as "const QVariant*"] -> QString as "QString" { + return self->toString(); + }) + } + + /// Wrapper around [`toInt()`][method] method. + /// + /// [method]: https://doc.qt.io/qt-5/qvariant.html#toInt + pub fn to_int(&self) -> u32 { + cpp!(unsafe [self as "const QVariant*"] -> u32 as "int" { + return self->toInt(); + }) + } + + /// Wrapper around ['typeName()`][method] method. + /// + /// [method]: https://doc.qt.io/qt-5/qvariant.html#typeName + pub fn type_name(&self) -> QString { + cpp!(unsafe [self as "const QVariant*"] -> QString as "QString" { + return self->typeName(); + }) + } + // FIXME: do more wrappers } impl fmt::Debug for QVariant { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(self.to_qbytearray().to_string().as_str()) + let data = self.to_qstring().to_string(); + let qtype = self.type_name().to_string(); + if data.len() == 0 { + write!(f, "QVariant({})", qtype.as_str()) + } else { + write!(f, "QVariant({}: \"{}\")", qtype.as_str(), data.as_str()) + } } } @@ -226,3 +268,29 @@ where (*a).clone().into() } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn qvariant_debug_qstring() { + let qv: QVariant = QString::from("Hello, QVariant!").into(); + assert_eq!(qv.to_qstring().to_string(), "Hello, QVariant!"); + assert_eq!(format!("{:?}", qv), "QVariant(QString: \"Hello, QVariant!\")"); + } + + #[test] + fn qvariant_debug_bool() { + let qv = QVariant::from(false); + assert_eq!(qv.to_qstring().to_string(), String::from("false")); + assert_eq!(format!("{:?}", qv), "QVariant(bool: \"false\")"); + } + + #[test] + fn qvariant_debug_int() { + let qv = QVariant::from(313); + assert_eq!(qv.to_int(), 313); + assert_eq!(format!("{:?}", qv), "QVariant(int: \"313\")"); + } +} \ No newline at end of file From 4b6566c7d9518d9bd879c3cc9782c4b0544278c9 Mon Sep 17 00:00:00 2001 From: Dimitris Apostolou Date: Wed, 28 Feb 2024 21:06:20 +0200 Subject: [PATCH 3/5] Fix feature = "cargo-clippy" deprecation --- qmetaobject/src/lib.rs | 6 +++--- qmetaobject/src/scenegraph.rs | 2 +- qmetaobject_impl/src/lib.rs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/qmetaobject/src/lib.rs b/qmetaobject/src/lib.rs index 3f462f9..9906ad5 100644 --- a/qmetaobject/src/lib.rs +++ b/qmetaobject/src/lib.rs @@ -151,8 +151,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #![recursion_limit = "10240"] -#![cfg_attr(feature = "cargo-clippy", allow(clippy::needless_pass_by_value))] // Too many of that for qt types. (FIXME) -#![cfg_attr(feature = "cargo-clippy", allow(clippy::cognitive_complexity))] +#![allow(clippy::needless_pass_by_value)] // Too many of that for qt types. (FIXME) +#![allow(clippy::cognitive_complexity)] #[doc(hidden)] pub use qmetaobject_impl::{qrc_internal, SimpleListItem}; @@ -511,7 +511,7 @@ impl<'pin, T: QObject + ?Sized + 'pin> QObjectPinned<'pin, T> { /// Borrow the object // FIXME: there are too many cases for which we want reentrance after borrowing //pub fn borrow(&self) -> std::cell::Ref { self.0.borrow() } - #[cfg_attr(feature = "cargo-clippy", allow(clippy::should_implement_trait))] + #[allow(clippy::should_implement_trait)] pub fn borrow(&self) -> &T { unsafe { &*self.0.as_ptr() } } diff --git a/qmetaobject/src/scenegraph.rs b/qmetaobject/src/scenegraph.rs index a405c42..3b157b2 100644 --- a/qmetaobject/src/scenegraph.rs +++ b/qmetaobject/src/scenegraph.rs @@ -74,7 +74,7 @@ cpp! {{ /// for [`SGNode::update_static`]. /// /// Do not reimplement -#[cfg_attr(feature = "cargo-clippy", allow(clippy::len_without_is_empty))] +#[allow(clippy::len_without_is_empty)] pub trait UpdateNodeFnTuple { fn len(&self) -> u64; unsafe fn update_fn(&self, i: u64, _: *mut c_void) -> *mut c_void; diff --git a/qmetaobject_impl/src/lib.rs b/qmetaobject_impl/src/lib.rs index 36fc87a..1c99120 100644 --- a/qmetaobject_impl/src/lib.rs +++ b/qmetaobject_impl/src/lib.rs @@ -19,8 +19,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. //! This crates implement the custom derive used by the `qmetaobject` crate. #![recursion_limit = "256"] -#![cfg_attr(feature = "cargo-clippy", allow(clippy::unreadable_literal))] // Because we copy-paste constants from Qt -#![cfg_attr(feature = "cargo-clippy", allow(clippy::cognitive_complexity))] +#![allow(clippy::unreadable_literal)] // Because we copy-paste constants from Qt +#![allow(clippy::cognitive_complexity)] use proc_macro::TokenStream; use quote::{quote, ToTokens}; From 84badb6067fd6fa00bb7a98ea044eb0138386886 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 5 Aug 2024 11:16:59 +0200 Subject: [PATCH 4/5] Comment out more panicking tests They no longer work in nightly rust as the cpp! macro use extern "c" which is not supposed to unwind --- qmetaobject/tests/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qmetaobject/tests/tests.rs b/qmetaobject/tests/tests.rs index 2fa88ec..9be4725 100644 --- a/qmetaobject/tests/tests.rs +++ b/qmetaobject/tests/tests.rs @@ -748,7 +748,6 @@ fn panic_when_moved_setter() { let my_obj = StupidObject::default(); do_test(my_obj, "Item { function doTest() { _obj.prop_y = 45; } }"); } -*/ #[test] #[should_panic(expected = "There can only be one QmlEngine in the process")] @@ -757,6 +756,7 @@ fn two_engines() { let _a = QmlEngine::new(); let _b = QmlEngine::new(); } +*/ #[derive(QEnum)] #[repr(u8)] From b08463a2e83ced7bfeecef4a02f5812dc6be02c2 Mon Sep 17 00:00:00 2001 From: Ruben De Smet Date: Sun, 4 Aug 2024 11:50:02 +0200 Subject: [PATCH 5/5] impl From for QVariant --- qttypes/src/qtcore/qvariant.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/qttypes/src/qtcore/qvariant.rs b/qttypes/src/qtcore/qvariant.rs index 434391f..c9c9060 100644 --- a/qttypes/src/qtcore/qvariant.rs +++ b/qttypes/src/qtcore/qvariant.rs @@ -1,7 +1,8 @@ use std::fmt; use crate::{ - cpp, cpp_class, QByteArray, QDate, QDateTime, QString, QStringList, QTime, QUrl, QVariantList, QVariantMap + cpp, cpp_class, QByteArray, QDate, QDateTime, QString, QStringList, QTime, QUrl, QVariantList, + QVariantMap, }; cpp_class!( @@ -118,6 +119,16 @@ impl From for QVariant { }) } } +impl From for QVariant { + /// Wrapper around [`QVariant(const QMap &)`][ctor] constructor. + /// + /// [ctor]: https://doc.qt.io/qt-5/qvariant.html#QVariant-22 + fn from(a: QVariantMap) -> QVariant { + cpp!(unsafe [a as "QVariantMap"] -> QVariant as "QVariant" { + return QVariant(a); + }) + } +} impl From for QVariant { /// Wrapper around [`QVariant(const QByteArray &)`][ctor] constructor. /// @@ -293,4 +304,4 @@ mod tests { assert_eq!(qv.to_int(), 313); assert_eq!(format!("{:?}", qv), "QVariant(int: \"313\")"); } -} \ No newline at end of file +}