Hi

I'm sorry for the burst of emails, and I'm not sure if this should be a new thread or not.

Another issue we are having with qmllint is adding type information for objects added to the global object in C++. We've used this to provide custom log and assert functions, but obviously the linter doesn't know about them. If they are added to qml/jsroot.qmltypes (along similar lines to the 'Console' type) then the linter does know about the type.

Is there a way to register types in the global property 'externally' ? I've tried a few variations of using the '-i' option in the linter, but without success.

Many thanks
Matthew Fincham



On 2023/08/30 15:24, Matthew Fincham via Interest wrote:
Hi Ulf

Thank you for your response.

-- QtLocation --
Thanks for catching this. See https://codereview.qt-project.org/c/qt/qtlocation/+/500504 for a fix.

That's great, thank you.


-- 'as' operator --
This should work. Type assertions (aka "as cast") are documented in https://doc.qt.io/qt-6/qtqml-javascript-hostenvironment.html . What kind of object does the loader produce here? Can you please open a bug report with a minimal example?

A bug report has been added: https://bugreports.qt.io/browse/QTBUG-116614


-- Enums in gadgets --
So, what you want to do is expose MyType as a namespace instead:

namespace MyTypeNamespace {
    Q_NAMESPACE
    QML_FOREIGN_NAMESPACE(MyType)
    QML_NAMED_ELEMENT(MyType)
}

You cannot have reference semantics on a value type. A type with reference semantics (aka an "object type") needs to be derived from QObject and have a Q_OBJECT macro. The engine plays somewhat loose with that and allows you to still access enums from such broken types. We should amend the documentation to warn about this problem and also generate a run time warning from the engine.

The example gadget was not the best. The gadget is used as an actual value type in qml. A fuller example (as I have it) would be:

C++:

    struct TestGadget
    {
        Q_GADGET
        QML_ELEMENT

        Q_PROPERTY(int value MEMBER value)

    public:
        enum class GadgetEnum
        {
            OptionA,
            OptionB,
            OptionC,
        };
        Q_ENUM(GadgetEnum)

    public:
        static void registerType();

    public:
        int value = 0;
    };


QML: (apart from where it is used as a data type) example usage would be

    // reference an instance of the gadget
    ... someGadgetValue.value ...

    // use the enum
    console.log(TestGadget.GadgetEnum.OptionB)


I've tried various permutations of the namespace macros you described, but can't get it right. Firstly, is this possible, and if it is, what is the canonical way of doing this? As it is it works, but you have said it is actually a broken type.

As a side note - a Google search for "qml q_gadget enum" has these as the top two results (at least for me) which both recommend something along the lines of the above. - https://stackoverflow.com/questions/66855134/how-to-access-a-q-enum-declared-in-c-class-from-qml
- https://qml.guide/enums-in-qt-qml

I know they are not Qt documentation. Just pointing it out - it would be nice if the Qt could somehow get a canonical example to the top of that search.

Thanks again for the feedback

Kind regards
Matthew


_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest
_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest

Reply via email to