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. Promise() as text in Label
Qt 6.11 is out! See what's new in the release blog

Promise() as text in Label

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 560 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.
  • S Offline
    S Offline
    shokarta
    wrote on last edited by shokarta
    #1

    hello,

    considering:

    Repeater {
      count: 999
    
      function calculateDate(input) {
          var result = something_this_takes_too_much_of_the_time * input;
      }
    
      Label {
        text: return (new Promise(resolve =>   calculateDate(somevalue)   )).then(text => text)
      }
    }
    

    as far as await function does not work in QML, Promise is good alternative not to block GUI when calculateDate() is calculating and text should be slowly populating one by one

    • this is at least my udnerstanding of Promise(), never used it before

    unfortunatelly, this does not work, it gives me error:

    Unable to assign QJSValue to QString
    

    when I try:

    return (new Promise(resolve =>   calculateDate()   )).then(text => text.toString())
    
    return (new Promise(resolve =>   calculateDate().toString()   )).then(text => text)
    

    but:

    return (new Promise(resolve =>   calculateDate()   )).then(text => text).toString()
    

    only display text as "[object Promise]"

    so how do I achive what I am looking for?

    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      You don't. That's not how promises work. then doesn't return a value, it takes a function that will be eventually called and returns the promise itself (the QJSValue or [object Promise] you see).
      Also you need to call resolve when writing a promise to indicate it is finished : (resolve => resolve(calculateDate)));

      More info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises

      What you could try doing is declaring a new Binding and use the fact that it can change a property, something like the following:

      // PromiseBinding
      import QtQml
      
      Binding {
           property var promise
           onPromiseChanged: promise.then(v => value = v)
      }
      

      Usage would be like this:

      Label {
          PromiseBinding on text {
              promise: new Promise (resolve => resolve(calculateDate().toString()))
         }
      }
      

      Disclaimer: I haven't tried that.

      1 Reply Last reply
      3

      • Login

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