Skip to content

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

Closed
ahayzen-kdab opened this issue Dec 16, 2022 · 5 comments · Fixed by #396
Closed

Consider how we can use traits to allow custom types in QVariant #395

ahayzen-kdab opened this issue Dec 16, 2022 · 5 comments · Fixed by #396
Assignees
Labels
⬆️ feature New feature or request
Milestone

Comments

@ahayzen-kdab
Copy link
Collaborator

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 the data method.

@ahayzen-kdab ahayzen-kdab added the ⬆️ feature New feature or request label Dec 16, 2022
@ahayzen-kdab ahayzen-kdab added this to the 0.5 milestone Dec 16, 2022
@ahayzen-kdab ahayzen-kdab self-assigned this Dec 16, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 16, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 16, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 16, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 16, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 19, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 19, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 20, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 20, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 20, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 20, 2022
przempore pushed a commit to przempore/cxx-qt that referenced this issue Mar 15, 2023
@VelorumS
Copy link

VelorumS commented Dec 1, 2023

How to put a listmodel into QVariant to return it from data to have nested listmodels?

The QVariant::fromValue<QAbstractListModel*>. It doesn't look like one of the cases with the value types.

@VelorumS
Copy link

VelorumS commented Dec 2, 2023

I assume it can't be implemented just once for QAbstractListModel* or even QObject* and used for all the models?

Adding that functionality to the CustomBaseClass* which is derived from QAbstractListModel in the example code:

custom_base_class.rs:

#[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)
    }
}

custom_base_class.h:

#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);

custom_base_class.cpp:

#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>();
}

@ahayzen-kdab
Copy link
Collaborator Author

You should be able to implement it once for a QAbstractListModel* maybe something like

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 QAbstractListModel* like

unsafe extern "C++" {
   unsafe fn to_base_class(ptr: *mut CustomBaseClass) -> *mut QAbstractListModel;
}

Note that you can use a template in C++ and use cxx_name to reuse it for all the base classes, like

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 QObject trait (see #562 ) which are implemented on types. Then you would have an as_qobject() or as_qobject_ptr() method on the type and we could already have QObject* or QObject as a supported type in the QVariant, then this would work for your usecase as well i think. (And maybe for common types like QAbastractListModel we could have some traits for those too). But something to be explored over the 0.7 / 0.8 cycle.

@ahayzen-kdab
Copy link
Collaborator Author

So maybe move this discussion into #562 and detail how it would be useful for you to use the listmodels in QVariant :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⬆️ feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants