GithubHelp home page GithubHelp logo

maxmommersteeg / qmlnet Goto Github PK

View Code? Open in Web Editor NEW

This project forked from qmlnet/qmlnet

0.0 3.0 0.0 18.04 MB

Qml.Net - Qt/QML integration/support for .NET

Home Page: https://qmlnet.github.io/

License: MIT License

Batchfile 0.11% PowerShell 0.01% Shell 0.32% C# 70.87% QMake 0.60% C++ 26.93% C 0.84% JavaScript 0.02% QML 0.29%

qmlnet's Introduction

Qml.Net

A Qml integration with .NET

Qml.Net Build status Build status Gitter chat Donate

Supported platforms/runtimes:

  • Runtimes:
    • .NET Framework
    • .NET Core
    • Mono
  • Operating systems
    • Linux
    • OSX
    • Windows

Documentation

https://qmlnet.github.io/

Getting started

dotnet add package Qml.Net

Windows

dotnet add package Qml.Net.WindowsBinaries

OSX

dotnet add package Qml.Net.OSXBinaries

Linux

dotnet add package Qml.Net.LinuxBinaries

Checkout the examples for some inspiration.

Quick overview

Define a .NET type (POCO)

[Signal("customSignal", NetVariantType.String)] // You can define signals that Qml can listen to.
public class QmlType
{
    /// <summary>
    /// Properties are exposed to Qml.
    /// </summary>
    [NotifySignal("stringPropertyChanged")] // For Qml binding/MVVM.
    public string StringProperty { get; set; }

    /// <summary>
    /// Methods can return .NET types.
    /// The returned type can be invoked from Qml (properties/methods/events/etc).
    /// </summary>
    /// <returns></returns>
    public QmlType CreateNetObject()
    {
        return new QmlType();
    }

    /// <summary>
    /// Qml can pass .NET types to .NET methods.
    /// </summary>
    /// <param name="parameter"></param>
    public void TestMethod(QmlType parameter)
    {
    }
    
    /// <summary>
    /// Async methods can be invoked with continuations happening on Qt's main thread.
    /// </summary>
    public async Task<string> TestAsync()
    {
        // On the UI thread
        await Task.Run(() =>
        {
            // On the background thread
        });
        // On the UI thread
        return "async result!"
    }
    
    /// <summary>
    /// .NET can activate signals to send notifications to Qml.
    /// </summary>
    public void ActivateCustomSignal(string message)
    {
        this.ActivateSignal("customSignal", message)
    }
}

Register your new type with Qml.

using (var app = new QGuiApplication(args))
{
    using (var engine = new QQmlApplicationEngine())
    {
        // Register our new type to be used in Qml
        QQmlApplicationEngine.RegisterType<QmlType>("test", 1, 1);
        engine.Load("main.qml");
        return app.Exec();
    }
}

Use the .NET type in Qml

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import test 1.1

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    QmlType {
      id: test
      Component.onCompleted: function() {
          // We can read/set properties
          console.log(test.stringProperty)
          test.stringPropertyChanged.connect(function() {
              console.log("The property was changed!")
          })
          test.stringProperty = "New value!"
          
          // We can return .NET types (even ones not registered with Qml)
          var netObject = test.createNetObject();
          
          // All properties/methods/signals can be invoked on "netObject"
          // We can also pass the .NET object back to .NET
          netObject.testMethod(netObject)
          
          // We can invoke async tasks that have continuation on the UI thread
          var task = netObject.testAsync()
          // And we can await the task
          Net.await(task, function(result) {
              // With the result!
              console.log(result)
          })
          
          // We can trigger signals from .NET
          test.customSignal.connect(function(message) {
              console.log("message: " + message)
          })
          test.activateCustomSignal("test message!")
      }
      function testHandler(message) {
          console.log("Message - " + message)
      }
    }
}

Currently implemented

  • Support for all the basic Qml types and the back-and-forth between them (DateTime, string, etc).
  • Reading/setting properties on .NET objects.
  • Invoking methods on .NET obejcts.
  • Declaring and activating signals on .NET objects.
  • async and await with support for awaiting and getting the result from Qml.
  • Passing dynamic javascript objects to .NET as dynamic.
  • Custom V8 type that looks like an array, but wraps a .NET IList<T> instance, for modification of list in Qml, and performance.
  • Dynamically compiled delegates for increased performance.

Not implemented (but planned)

  • Compiling Qml resource files and bundling them within .NET.
  • Passing QObject types to .NET with support for interacting with signals/slots/properties on them.
  • .NET Events to signals
  • Qml debugger for VS and VS Code.

qmlnet's People

Contributors

pauldotknopf avatar devmil avatar maxmommersteeg avatar triplewhy avatar afillebrown avatar jamesdavila avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.