-
Notifications
You must be signed in to change notification settings - Fork 82
Consider how we can use traits to allow custom types in QVariant #395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
How to put a listmodel into The |
@VelorumS Yup, you'll need to implement QVariantValue for |
I assume it can't be implemented just once for Adding that functionality to the
#[cxx_qt::bridge(cxx_file_stem = "custom_base_class")]
pub mod qobject {
...
unsafe extern "C++" {
include!("custom_base_class.h");
/// CustomBaseClassPtr
type CustomBaseClassPtr<'a> = &'a CustomBaseClass;
/// Return whether the given QVariant can be converted into a CustomBaseClassPtr
#[rust_name = "qvariant_can_convert_custom_base_class_ptr"]
fn qvariantCanConvertCustomBaseClassPtr(variant: &QVariant) -> bool;
/// Construct a QVariant of CustomBaseClassPtr
#[rust_name = "qvariant_construct_custom_base_class_ptr"]
fn qvariantConstructCustomBaseClassPtr(value: &CustomBaseClassPtr) -> QVariant;
/// Retrieve the CustomBaseClassPtr or default from a QVariant
#[rust_name = "qvariant_value_or_default_custom_base_class_ptr"]
fn qvariantValueOrDefaultCustomBaseClassPtr<'a, 'b>(variant: &'a QVariant) -> &'b CustomBaseClass;
}
...
}
unsafe impl cxx::ExternType for &qobject::CustomBaseClass {
type Id = cxx::type_id!("CustomBaseClassPtr");
type Kind = cxx::kind::Trivial;
}
impl cxx_qt_lib::QVariantValue for &qobject::CustomBaseClass {
fn can_convert(variant: &cxx_qt_lib::QVariant) -> bool {
qobject::qvariant_can_convert_custom_base_class_ptr(variant)
}
fn construct(value: &Self) -> cxx_qt_lib::QVariant {
qobject::qvariant_construct_custom_base_class_ptr(value)
}
fn value_or_default(variant: &cxx_qt_lib::QVariant) -> Self {
qobject::qvariant_value_or_default_custom_base_class_ptr(variant)
}
}
#pragma once
#include <QMetaType>
#include <QVariant>
#include "custom_base_class.cxxqt.h"
class CustomBaseClass;
using CustomBaseClassPtr = CustomBaseClass*;
bool qvariantCanConvertCustomBaseClassPtr(const QVariant& variant);
QVariant qvariantConstructCustomBaseClassPtr(const CustomBaseClassPtr& value);
const CustomBaseClass& qvariantValueOrDefaultCustomBaseClassPtr(const QVariant& variant);
#include "custom_base_class.h"
bool qvariantCanConvertCustomBaseClassPtr(const QVariant& variant)
{
return variant.canConvert<CustomBaseClassPtr>();
}
QVariant qvariantConstructCustomBaseClassPtr(const CustomBaseClassPtr& value)
{
return QVariant::fromValue(value);
}
const CustomBaseClass& qvariantValueOrDefaultCustomBaseClassPtr(const QVariant& variant)
{
return *variant.value<CustomBaseClassPtr>();
} |
You should be able to implement it once for a unsafe extern "C++" {
type QAbstractListModel;
...
// use *mut QAbstractListModel as the type for QVariant
} Then for your derived types create a method which casts it into a unsafe extern "C++" {
unsafe fn to_base_class(ptr: *mut CustomBaseClass) -> *mut QAbstractListModel;
} Note that you can use a template in C++ and use unsafe extern "C++" {
#[cxx_name = "templated_conversion_method"]
unsafe fn to_base_class(ptr: *mut CustomBaseClass) -> *mut QAbstractListModel;
} Or you could make it a self on the CustomBaseClass so then it's a member. There are many different ways of doing it :-) Note that in the future we want to have a |
So maybe move this discussion into #562 and detail how it would be useful for you to use the listmodels in QVariant :-) |
At the moment there is an enum of
QVariantType
/QVariantValue
where each type is defined as an enum.Instead consider changing to using a trait like the container types do to allow for custom types to be registered as
QVariant
types.This is important going forward as you might want to return a custom value in a listmodel which returns a
QVariant
in thedata
method.The text was updated successfully, but these errors were encountered: