On this page

Using C& Models with QML Views

Filtering code generation sources using C# attributes

It's possible to adjust which types and type members are included and excluded from code generation by using the attributes described below.

Ignore types

The simplest way of filtering out a type, regardless of where it is defined - either in the current module or in an external assembly - is by listing that type in a [Qt.IgnoreType] assembly-level attribute.

[assembly: Qt.IgnoreType(typeof(List<int>), typeof(List<string>))]

// This class will be included
public class Foo
{
    // These 2 methods will be excluded due to ignored parameter types
    public void Bar(List<int> arg) { }
    public void Bar(List<string> arg) { }

    // This method will be included
    public void Bar(List<double> arg) { }
}

Generic types

For generic types, for example, List<T> where T is a type variable, the typeof() facility is not available. To ignore a generic type and all of its specializations, the string form of the generic type must instead be used.

[assembly: Qt.IgnoreType("System.Collections.Generic.List`1")]

// This class will be included
public class Foo
{
    // These 3 methods will be excluded due to ignored parameter types
    public void Bar(List<int> arg) { }
    public void Bar(List<string> arg) { }
    public void Bar(List<double> arg) { }
}

Ignore type definition

It's possible to exclude a type defined in the current project's code by adding a [Qt.Ignore] attribute to the type declaration.

// This class will be excluded
[Qt.Ignore]
public class Foo
{ }

Ignore type member definition

Similarly, a member of a type defined in code can also be excluded from code generation using a [Qt.Ignore] attribute.

// This class will be included
public class Foo
{
    // This property will be excluded
    [Qt.Ignore]
    public int Bar { get; set; }
}

Ignore type hierarchy

The [Qt.IgnoreType] attribute can also be used to filter out entire type hierarchies, that is any type that derives from the root type listed in the attribute. This is achieved by specifying Inherited = true in the attribute instantiation.

[assembly: Qt.IgnoreType("System.Collections.Generic.Dictionary`2", Inherited = true)]

// This class will be excluded as it derives from an ignored type hierarchy
public class Foo : Dictionary<int, string>
{
    ...
}

Include sub-type of ignored type hierarchy

It's still possible to include types deriving from an ignored type hierarchy. These must be opted-in to the code generation by using the [Qt.Include] attribute.

[assembly: Qt.IgnoreType(typeof(Foo), Inherited = true)]

// This class will be ignored (ignored base type)
public class Foo
{ }

// This class will be ignored (derived type of ignored base type)
public class Oof : Foo
{ }

// This class will be included (opted-in by attribute)
[Qt.Include]
public class Bar : Foo
{ }

Accessing C# types from QML Views

Once the desired C# types have been selected for code generation, they can be accessed from QML Views.

For example, given the following C# type:

using Qt.DotNet.Utils;

namespace MyProject
{
public class MyClass
    {
        public int MyProperty { get; set; }
    }
}

To access C# types from QML, instantiate them in QML and access their members via a . operator. The member names match the C# names, but are started in lowercase:

import QtQuick
// ...
Rectangle {
    width: 400; height: 400

    MyClass {
        id: myClassInstance
    }

    Text {
        text: "MyProperty value is " + myClassInstance.myProperty
        anchors.centerIn: parent
    }
}

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.