trying to wrap qsettings in rust

This commit is contained in:
Chris Cochrun 2023-01-27 09:58:16 -06:00
parent 137ba3d4f6
commit f1def0bce9
2 changed files with 58 additions and 0 deletions

24
src/cpp/qsettingscxx.h Normal file
View file

@ -0,0 +1,24 @@
#pragma once
#include <QSettings>
#include "rust/cxx.h"
class QSettingsCXX : public QSettings
{
public:
explicit QSettingsCXX(QObject* parent = nullptr)
: QSettings(parent)
{
}
// Can't define in CXX as they are protected
// so crate public methods that are proxied
void setValue(QString key, QString value)
{
QSettings::setValue(key, value);
}
void sync() { QSettings::sync(); }
};

34
src/rust/settings.rs Normal file
View file

@ -0,0 +1,34 @@
#[cxx_qt::bridge]
mod settings {
unsafe extern "C++" {
include!("qsettingscxx.h");
include!("cxx-qt-lib/qstring.h");
type QString = cxx_qt_lib::QString;
}
#[derive(Clone)]
#[cxx_qt::qobject(base = "QSettingsCXX")]
pub struct Settings {
#[qproperty]
name: QString,
#[qproperty]
kind: QString,
}
impl Default for Settings {
fn default() -> Self {
Self {
name: QString::from(""),
kind: QString::from(""),
}
}
}
impl qobject::Settings {
#[qinvokable]
pub fn activate(self: Pin<&mut Self>) {
println!("{}", self.name());
}
}
}