Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML Loader loads components faster than property assignment
Forum Updated to NodeBB v4.3 + New Features

QML Loader loads components faster than property assignment

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 3 Posters 578 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hamzan Martinez
    wrote on 20 Feb 2021, 10:04 last edited by Hamzan Martinez
    #1

    Hello QT users,

    I have been going through a problem that has been bugging me for two weeks now, and I cannot find a solution to this problem.

    I have my main window, which contains a loader. The loader loads a QML file that has a model in it, and when in the delegate of that model a button is pressed, I want to pass the model object directly to the loader. Then, using the same loader, I want to load another page that uses that passed model object and access its roles using the page.

    But whenever I do that, while the loader is set to asynchronous, it does not load. It complains that the model object is null. But, if I disable asynchronous, it works, but it still complains that the object is null. It's as though it loads it so fast, that the objects such as text that are bound to the model object are not loaded, but a few milliseconds later, it loads.

    Here is a modified version of my original code:

    main.qml

    ApplicationWindow {
    id: root
    property alias pageloader: testLoader;
    
         Loader {
         id: testLoader
         property QtObject modelData;
         asynchronous: true
         source: "./test.qml"
         }
    }
    

    test.qml:

    Item {
        id: test
    
         Repeater {
            model: testmodel
    
           delegate: {
                button {
                    id: testbtn
                    onClicked: {
                       root.pageloader.modelData = model;
                       root.pageloader.source = "./test2.qml"
                   }
                }
           }
         }
    }
    

    test2.qml

    Item {
         id: test2
    
         text {
            root.pageloader.modelData.title;
         }
    }
    

    TypeError: Cannot read property 'title' of null

    If I were to use the item property of the loader, it remains the same. The problem persists.

    Anyone has any idea how I can circumvent this issue? I tried passing the model object to the mainwindow (root), but the issue remains the same.

    The model I am using is a custom model, using QAbstractListModel. It works fine, no errors there. The loader is my only problem.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 20 Feb 2021, 10:17 last edited by
      #2

      Guard the calls to pageLoader:

      Text {
              text: root.pageloader? root.pageloader.modelData.title : ""
           }
      

      Ugly, but works.

      (Z(:^

      1 Reply Last reply
      1
      • J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 22 Feb 2021, 06:28 last edited by J.Hilk
        #3

        @Hamzan-Martinez Do not delete the topic, when someone took the time and effort to provide you with a working answer!


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        3

        1/3

        20 Feb 2021, 10:04

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved