How do i use non-QObject classes from QtCore like QStandardPaths? #1331
Replies: 1 comment
-
|
Hey, so as Then recently CXX has added support for defining static member functions via the So with this new support it'd likely look something like #[cxx::bridge]
mod ffi {
unsafe extern "C++" {
include!("cxx-qt-lib/qstring.h");
type QString = cxx_qt_lib::QString;
}
unsafe extern "C++" {
include!(<QtCore/QStandardPaths>);
type QStandardPaths;
#[Self = "QStandardPaths"] // <- the Self here means that it is a static method
fn displayName(type: QStandardPathsStandardLocation) -> QString;
}
// defining the enum is slightly fun as it's within the QStandardPaths rather than a namespace
// we define an extern enum https://cxx.rs/shared.html#extern-enums
#[repr(i32)]
#[derive(Copy, Clone, Debug, ...)] // <- choose your derives
enum QStandardPathsStandardLocation {
DesktopLocation,
DocumentsLocation,
... // <- match these to the Qt ones
}
unsafe extern "C++" {
include!("myheader.h"); // <- have a header in your include path where we define the below
type QStandardPathsStandardLocation; // <- this then points to the alias in C++
}
}For the enum to work you'd need a small bit of C++ in the // As CXX cannot reach the enum inside the class we define an alias
using QStandardPathsStandardLocation = QStandardPaths::StandardLocation;I have not tested this but hopefully this gives you an idea, for the enum parts consider looking at what has been done in cxx-qt-lib for other similar enums. Normally it's a matter of getting your C++ into types and structure that can be represented in CXX, which sometimes means adding extra C++ code to bridge between 😅 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I cannot for the life of me figure out how to link QStandardPaths so that it can be called from rust. The class has static methods that I'd like to call.
Beta Was this translation helpful? Give feedback.
All reactions