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 If statement matching strings in a variable ?
Servers for Qt installer are currently down

QML If statement matching strings in a variable ?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 2 Posters 17.3k 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.
  • A Offline
    A Offline
    AexAcad
    wrote on 31 Dec 2016, 02:44 last edited by
    #1

    I am searching a way to match words within a string message I received from websockets which is stored in a variable using an IF statement. so if the words within a string match to perform an action. I am not sure on how to go about and use an regular expressions in QML but I have tried using the following which does not work:

    var post = "This is a string received from websockets"
    textbox.text = post;
    if (textbox.text[1] === "This" && textbox.text[3] === "string") {
            somebutton.visisble=true;
    }
    

    I've also tried this:

    var post = "This is a string received from websockets"
    textbox.text = post;
    var strfind = textbox.text.indexOf("This")
    var strfind2 = textbox.text.indexOf("String")
    if (textbox.text.indexOf("This") === strfind && textbox.text.indexOf("String") === strfind2) {
            somebutton.visisble=true;
    }
    
    1 Reply Last reply
    0
    • P Offline
      P Offline
      p3c0
      Moderators
      wrote on 31 Dec 2016, 04:04 last edited by
      #2

      @AexAcad
      In your first method is textbox.text an array ? If yes, then use split method to convert string to array.
      The second method should work because the LHS and RHS in the condition are exactly the same. The only problem is see there is visisble. Is that a typo here ? It will be undefined in your code and thus the operation will fail.

      157

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AexAcad
        wrote on 31 Dec 2016, 04:52 last edited by
        #3

        @p3c0

        textbox.text is not an array, and the second condition fails even if the conditions are the same,
        If i use the whole statement like:

        if (textbox.text === "This is a string received from websockets") {
        somebutton.visible = true
        }
        
        

        It works but that's exactly what i am not trying to do is use the whole statement. I am trying use Keywords within the string as I want to use the same method for other strings received which do not always have the exact same keywords. for example the string received can be "This is a message in string format received from websockets" so the above condition will fail as the whole string does not match.

        I have tried this too:

        var post = "This is a string received from websockets"
        textbox.text = post;
        var strfind = textbox.text.indexOf("This")
        console.log(strfind)
        var strfind2 = textbox.text.indexOf("String")
        if (textbox.text.indexOf("This") === strfind) {
        
                somebutton.visisble=true;
        }
        

        strfind returns 0 which is true but the above if statement still fails.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          p3c0
          Moderators
          wrote on 31 Dec 2016, 05:18 last edited by
          #4

          @AexAcad

          textbox.text is not an array, and the second condition fails even if the conditions are the same,

          So if (textbox.text[1] === "This" && textbox.text[3] === "string") { wont work.

          indexOf will return a valid index if the string if found and -1 otherwise so why not check for this condition ?

          157

          A 1 Reply Last reply 31 Dec 2016, 05:28
          0
          • P p3c0
            31 Dec 2016, 05:18

            @AexAcad

            textbox.text is not an array, and the second condition fails even if the conditions are the same,

            So if (textbox.text[1] === "This" && textbox.text[3] === "string") { wont work.

            indexOf will return a valid index if the string if found and -1 otherwise so why not check for this condition ?

            A Offline
            A Offline
            AexAcad
            wrote on 31 Dec 2016, 05:28 last edited by
            #5

            @p3c0

            I got it working thanks!! I used the following logic:

            var post = "This is a string received from websockets"
            textbox.text = post;
            var strfind = textbox.text.indexOf("This")
            console.log(strfind)
            if (textbox.text.indexOf("This") === 0) {
                    somebutton.visisble=true;
            }
            else {
                somebutton.visible=false;
            }
            
            1 Reply Last reply
            0
            • P Offline
              P Offline
              p3c0
              Moderators
              wrote on 31 Dec 2016, 05:34 last edited by
              #6

              @AexAcad
              Great! But beware you are always checking against a fixed index i.e 0. Maybe you can make use of -1 which is returned when string is not found ?

              157

              A 1 Reply Last reply 31 Dec 2016, 05:57
              0
              • P p3c0
                31 Dec 2016, 05:34

                @AexAcad
                Great! But beware you are always checking against a fixed index i.e 0. Maybe you can make use of -1 which is returned when string is not found ?

                A Offline
                A Offline
                AexAcad
                wrote on 31 Dec 2016, 05:57 last edited by
                #7

                @p3c0

                I could use this:

                if (textbox.text.indexOf("Register") != -1 && texbox.text.indexOf("Device") != -1) {
                        somebutton.visisble=true;
                }
                

                But the websocket application I am trying to communicate with is more like a client chat application where if the input string received from websockets contains ("Register") and ("Device") the button would always become visible to the user instead of when I would want only the button to be visible on a string that has a fixed format for example "Register your Device at http://example.com with paring code: xyz"

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  p3c0
                  Moderators
                  wrote on 31 Dec 2016, 06:07 last edited by
                  #8

                  @AexAcad Ok.

                  157

                  1 Reply Last reply
                  0

                  1/8

                  31 Dec 2016, 02:44

                  • Login

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