[GH-ISSUE #164] Pass window size to Main.qml programmatically #104

Closed
opened 2026-05-05 11:04:50 -06:00 by gitea-mirror · 2 comments
Owner

Originally created by @WingedToaster on GitHub (Sep 16, 2019).
Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/164

Is there a way to pass in the size of the window to Main.qml from the .Net Core json config file instead of hardcoding into the Main.qml?

Originally created by @WingedToaster on GitHub (Sep 16, 2019). Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/164 Is there a way to pass in the size of the window to Main.qml from the .Net Core json config file instead of hardcoding into the Main.qml?
Author
Owner

@pauldotknopf commented on GitHub (Sep 17, 2019):

You can set a property on the QQmlApplicationEngine. I believe the method to set the value is "SetContextProperty". Any value set there will "just be available" in qml as a named variable.

<!-- gh-comment-id:532021070 --> @pauldotknopf commented on GitHub (Sep 17, 2019): You can set a property on the QQmlApplicationEngine. I believe the method to set the value is "SetContextProperty". Any value set there will "just be available" in qml as a named variable.
Author
Owner

@WingedToaster commented on GitHub (Sep 17, 2019):

Yes, works like a champ, thanks!

config.json

{
  ...
  "window": {
    "width": 480,
    "height": 272
  },
  ...
}

Main.qml

ApplicationWindow {
    id: window
    width: windowWidth
    height: windowHeight
    ...
}

Program

        public static int Main(string[] args)
        {

            IConfiguration _configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("config.json", true, true)
                .Build();

            ...

            using (var app = new QGuiApplication(args))
            {
                using (var engine = new QQmlApplicationEngine())
                {
                    ...

                    engine.SetContextProperty("windowWidth", int.Parse(_configuration["window:width"]));
                    engine.SetContextProperty("windowHeight", int.Parse(_configuration["window:height"]));

                    engine.Load("Main.qml");
                    return app.Exec();
                }
            }
        }
<!-- gh-comment-id:532054547 --> @WingedToaster commented on GitHub (Sep 17, 2019): Yes, works like a champ, thanks! **config.json** ``` { ... "window": { "width": 480, "height": 272 }, ... } ``` **Main.qml** ``` ApplicationWindow { id: window width: windowWidth height: windowHeight ... } ``` **Program** ``` public static int Main(string[] args) { IConfiguration _configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("config.json", true, true) .Build(); ... using (var app = new QGuiApplication(args)) { using (var engine = new QQmlApplicationEngine()) { ... engine.SetContextProperty("windowWidth", int.Parse(_configuration["window:width"])); engine.SetContextProperty("windowHeight", int.Parse(_configuration["window:height"])); engine.Load("Main.qml"); return app.Exec(); } } } ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/qmlnet#104
No description provided.