Skip to content
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

Consider how to support enums through to QML #34

Closed
3 tasks done
ahayzen-kdab opened this issue Dec 9, 2021 · 7 comments
Closed
3 tasks done

Consider how to support enums through to QML #34

ahayzen-kdab opened this issue Dec 9, 2021 · 7 comments
Assignees
Labels
⬆️ feature New feature or request 🥳🎉 1.0 This issue is part of stabilization for 1.0 release

Comments

@ahayzen-kdab
Copy link
Collaborator

ahayzen-kdab commented Dec 9, 2021

We need to support enums to allow for complex objects like QAbstractListModel etc.

  • Enums on QObjects from Rust
  • Enums in a namespace from Rust
  • Existing enums (via CXX)
@ahayzen-kdab
Copy link
Collaborator Author

Now you can define an enum in CXX, then register that enum to QML manually in C++.

@Be-ing
Copy link
Contributor

Be-ing commented Feb 10, 2023

Now you can define an enum in CXX, then register that enum to QML manually in C++.

Right, CXX takes care of most of the work already. But it would be helpful to be able to add a #[q_enum(QObjectClassName)] annotation to a cxx shared enum type and have our C++ generator add a Q_ENUM(enum_name) macro to the generated C++.

@ahayzen-kdab
Copy link
Collaborator Author

I need to try this again but i almost got it working before using Q_ENUM_NS from C++ as a workaround until we do add support with an attribute to shared enums. (eg #402 will hopefully have helped with that)

ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Jul 27, 2023
@ahayzen-kdab ahayzen-kdab added 🥳🎉 1.0 This issue is part of stabilization for 1.0 release and removed 🙋 good first issue Good for newcomers labels Jul 27, 2023
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Jul 28, 2023
@LeonMatthesKDAB
Copy link
Collaborator

We should be able to support both Q_ENUM, as well as Q_ENUM_NS using CXX Extern enums.

e.g.:

#pragma once

#include <cstdint>

struct MyObject {
  enum class MyEnum : std::int8_t { A, B };
};

namespace rust {
namespace MyObject {
using MyEnum = ::MyObject::MyEnum;
}
} // namespace rust
#[cxx::bridge]
pub mod ffi {

    #[cxx_name = "MyEnum"]
    #[namespace = "rust::MyObject"]
    enum MyEnum {
        A,
        B,
    }

    unsafe extern "C++" {
        include!("src/myobject.h");

        #[cxx_name = "MyEnum"]
        #[namespace = "rust::MyObject"]
        type MyEnum;
    }
}

Then we would support these two kinds of declarations:

#[qenum]
#[namespace="MyNamespace"]
enum MyEnum { ... }
#[qenum(MyObject)]
enum MyEnum { ... }

extern "RustQt" {
    type MyObject = ...;
}

@LeonMatthesKDAB
Copy link
Collaborator

It turns out we can use QML_ELEMENT with Q_NAMESPACE to expose a Q_ENUM_NS it to QML.

See: https://doc.qt.io/qt-6/qqmlengine.html#QML_ELEMENT

LeonMatthesKDAB added a commit to LeonMatthesKDAB/cxx-qt that referenced this issue Aug 16, 2023
This is the initial support for KDAB#34.

It only supports `Q_ENUM` within a given `Q_OBJECT` class.
`Q_ENUM_NS` is not (yet) supported.
Documentation will be added in a follow-up commit.
LeonMatthesKDAB added a commit to LeonMatthesKDAB/cxx-qt that referenced this issue Aug 16, 2023
This is the initial support for KDAB#34.

It only supports `Q_ENUM` within a given `Q_OBJECT` class.
`Q_ENUM_NS` is not (yet) supported.
Documentation will be added in a follow-up commit.
LeonMatthesKDAB added a commit to LeonMatthesKDAB/cxx-qt that referenced this issue Aug 16, 2023
This is the initial support for KDAB#34.

It only supports `Q_ENUM` within a given `Q_OBJECT` class.
`Q_ENUM_NS` is not (yet) supported.
Documentation will be added in a follow-up commit.
LeonMatthesKDAB added a commit to LeonMatthesKDAB/cxx-qt that referenced this issue Aug 18, 2023
This is the initial support for KDAB#34.

It only supports `Q_ENUM` within a given `Q_OBJECT` class.
`Q_ENUM_NS` is not (yet) supported.
Documentation will be added in a follow-up commit.
LeonMatthesKDAB added a commit to LeonMatthesKDAB/cxx-qt that referenced this issue Aug 18, 2023
This is the initial support for KDAB#34.

It only supports `Q_ENUM` within a given `Q_OBJECT` class.
`Q_ENUM_NS` is not (yet) supported.
Documentation will be added in a follow-up commit.
LeonMatthesKDAB added a commit to LeonMatthesKDAB/cxx-qt that referenced this issue Aug 18, 2023
This is the initial support for KDAB#34.

It only supports `Q_ENUM` within a given `Q_OBJECT` class.
`Q_ENUM_NS` is not (yet) supported.
Documentation will be added in a follow-up commit.
ahayzen-kdab pushed a commit that referenced this issue Aug 18, 2023
This is the initial support for #34.

It only supports `Q_ENUM` within a given `Q_OBJECT` class.
`Q_ENUM_NS` is not (yet) supported.
Documentation will be added in a follow-up commit.
@LeonMatthesKDAB
Copy link
Collaborator

Done

@ahayzen-kdab
Copy link
Collaborator Author

This is done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⬆️ feature New feature or request 🥳🎉 1.0 This issue is part of stabilization for 1.0 release
Projects
None yet
Development

No branches or pull requests

3 participants