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. Using a function to generate a string for text:
Forum Updated to NodeBB v4.3 + New Features

Using a function to generate a string for text:

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 209 Views
  • 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.
  • SeDiS Offline
    SeDiS Offline
    SeDi
    wrote on last edited by
    #1

    Hi,

    I would like to let a function directly feed a text property, like:

    Text {
           text: function textSource() {
                  switch (lockKey.mode) {
                         case first: return "First option";
                         case second: return "Second option";
                         case third: return "Third option";
                         default: return "Default option";
                  }            
           }
    }
    
    

    But I get the following error message:

    Unable to assign a function to a property of any type other than var.
    

    while, on the other hand,

    Text {
           function textSource() {
                  switch (lockKey.mode) {
                         case first: return "First option";
                         case second: return "Second option";
                         case third: return "Third option";
                         default: return "Default option";
                  }
           }
           text: textSource()
    }
    

    does work, indeed. I've tried to trick around with

    text: String()+function textSource() { ...}
    

    but this leads to an on-screen text output of:

    "function textSource() {native code}"
    

    Is there a syntax trick that I am missing? Or is there any reasoning behind that restriction?

    1 Reply Last reply
    0
    • SeDiS Offline
      SeDiS Offline
      SeDi
      wrote on last edited by
      #2

      Replying to myself. The simple solution is not to use "function", but to just write the JS code in curly brackets. Didn't know that's even legal...

                      text:  {
                          switch (lockKey.mode) {
                              case first: return "First option";
                              case second: return "Second option";
                              case third: return "Third option";
                              default: return "Default option";                   }
                      }
      

      Sorry for the noise. I'll leave this for those searching for the error message (as I did).

      1 Reply Last reply
      0

      • Login

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