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. [Solved] cannot set property of undefined
Forum Updated to NodeBB v4.3 + New Features

[Solved] cannot set property of undefined

Scheduled Pinned Locked Moved QML and Qt Quick
14 Posts 3 Posters 5.5k 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.
  • P Offline
    P Offline
    petro2033
    wrote on 27 Mar 2014, 13:10 last edited by
    #1

    Hi, i build my project without errors, but when i tried start EXE files, i got this error.
    Cannot set 'focus' of undefined

    @function create_newgame(num)
    {
    sound_main.stop();
    if (num==5)
    {
    var component = Qt.createComponent("Big_game.qml");
    if (component.status == Component.Ready)
    var bg_game = component.createObject(main_Window, {});
    bg_game.focus = true;
    }
    else
    {
    var component = Qt.createComponent("Game.qml");
    if (component.status == Component.Ready)
    var game = component.createObject(main_Window, {});
    game.focus = true; //THIS IS ERROR

    switch (num)
    {
    case 1:
        game.image_number = "qrc:/image/num_1.png";
        break;
    case 2:
        game.image_number = "qrc:/image/num_2.png";
        break;
    case 3:
        game.image_number = "qrc:/image/num_3.png";
        break;
    case 4:
        game.image_number = "qrc:/image/num_4.png";
        break;
    }
    }
    if(num!=4)
        game.multiplier = num;
    else
        game.multiplier = 1;
    

    }@

    1 Reply Last reply
    0
    • X Offline
      X Offline
      Xander84
      wrote on 27 Mar 2014, 13:22 last edited by
      #2

      your game object is outside of the if-scope, do you have experience with programming in general?
      anyway the solution is to put { } around the if-block, like:
      @
      if (component.status == Component.Ready) {
      var game = component.createObject(main_Window, {});
      game.focus = true; //THIS IS ERROR
      }
      // game is out of scope after the } here and will be "undefined"
      @

      in general if you omit the { } after an if or while-loop etc only the first expression will be used in the block, so I always use { } no matter if there is only 1 expression or more, that is some good practice but it's up to you :)

      another tip, instead of you "switch (num)" you could just use a single expression if you want:
      @
      game.image_number = "qrc:/image/num_" + num + ".png";
      @

      1 Reply Last reply
      0
      • P Offline
        P Offline
        petro2033
        wrote on 27 Mar 2014, 13:58 last edited by
        #3

        Thanks, but i have good programming experience. About last tip: it's great that i can do this.
        About this block
        @if (component.status == Component.Ready)
        var game = component.createObject(main_Window, {});
        game.focus = true; //THIS IS ERROR
        @
        I need created object "game", and it should be error when component is not ready, but it's ready! I did as you said, and create object without IF but ERROR remain.
        There are similar questions, but i don't understand solution.
        https://qt-project.org/forums/viewthread/30016

        1 Reply Last reply
        0
        • X Offline
          X Offline
          Xander84
          wrote on 27 Mar 2014, 16:00 last edited by
          #4

          No i didn't say remove the if, just look at the code I provided you forgot the {} around "game.focus = true" :)

          you need to use it like this (again):
          @
          if (component.status == Component.Ready)
          {
          var game = component.createObject(main_Window, {});
          game.focus = true;
          }
          @

          same with your other object "bg_game" off course.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            petro2033
            wrote on 28 Mar 2014, 02:34 last edited by
            #5

            @if (component.status == Component.Ready)
            {
            var game = component.createObject(main_Window, {});
            game.focus = true;
            }@

            @if (component.status == Component.Ready)
            var game = component.createObject(main_Window, {});
            game.focus = true;
            @
            If construction "IF" returned TRUE. I did as you say, ERROR disappeared but whatever didn't work, *It seems that IF returned false. * and component are not ready.
            Why in QT all is ok, but from EXE i have this problem.

            1 Reply Last reply
            0
            • X Offline
              X Offline
              Xander84
              wrote on 28 Mar 2014, 12:40 last edited by
              #6

              well I'm sorry to tell you this but the code is not the same.

              @
              if (component.status == Component.Ready)
              var game = component.createObject(main_Window, {});
              game.focus = true;
              @

              @
              if (component.status == Component.Ready)
              {
              var game = component.createObject(main_Window, {});
              }
              game.focus = true;
              @
              that is why game.focus = true produces and warning because "game" is undefined at that line... seems like nobody else is reading this thread but you tr it yourself if you don't believe me

              1 Reply Last reply
              0
              • P Offline
                P Offline
                petro2033
                wrote on 30 Mar 2014, 04:57 last edited by
                #7

                well I’m sorry to tell you this but the code is the SAME, when IF returned TRUE. Anyway,I tried to do and it does NOT WORK, because var component = Qt.createComponent("Game.qml"); - not created, but in QT all ok, from EXE doesn't work.

                1 Reply Last reply
                0
                • EddyE Offline
                  EddyE Offline
                  Eddy
                  wrote on 30 Mar 2014, 08:31 last edited by
                  #8

                  Hi petro2033,

                  Welcome to Devnet.

                  I suggest you experiment with if blocks and braces and debug in every possible place to see what your variables contain as values.
                  @
                  If (A)
                  B;
                  C;
                  @

                  Is not the same as

                  @
                  If (A)
                  { B;
                  C;
                  }
                  @

                  PS: using capitals is generally considered as shouting to people. Please avoid it on the forum. Xander is just willingly to help you

                  Qt Certified Specialist
                  www.edalsolutions.be

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    petro2033
                    wrote on 30 Mar 2014, 12:16 last edited by
                    #9

                    the same if A returned true....
                    Anyway thanks you, but anyway it doesn't work.

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      petro2033
                      wrote on 30 Mar 2014, 12:19 last edited by
                      #10

                      @a = 5;
                      b = 0
                      If (a == 5)
                      b = b +1;
                      b = b + 1;@
                      Result: b = 2;

                      @a = 5;
                      b = 0
                      If (a == 5)
                      {
                      b = b +1;
                      b = b + 1;
                      }@
                      Result: b = 2.

                      1 Reply Last reply
                      0
                      • EddyE Offline
                        EddyE Offline
                        Eddy
                        wrote on 30 Mar 2014, 13:53 last edited by
                        #11

                        [quote author="petro2033" date="1396181811"]the same if A returned true....
                        Anyway thanks you, but anyway it doesn't work.[/quote]

                        But it's not the same if A doesn't return true, so in a whole it's not the same.

                        Anyway, how is var defined in your program?

                        Qt Certified Specialist
                        www.edalsolutions.be

                        1 Reply Last reply
                        0
                        • X Offline
                          X Offline
                          Xander84
                          wrote on 30 Mar 2014, 13:57 last edited by
                          #12

                          hey of course it is the same if your "if" condition is evaluated to "true", but not if its "false".

                          in your case if the component is not ready there might be a problem in the pother QMl files (you didn't post it here so we can't help you with that):
                          @
                          var component = Qt.createComponent("Game.qml");
                          @
                          if that doesn't create a component either the QMl engine can't find the file or there is an error in the Game.qml, in any case you should see some error in the application console?

                          1 Reply Last reply
                          0
                          • P Offline
                            P Offline
                            petro2033
                            wrote on 31 Mar 2014, 02:16 last edited by
                            #13

                            ty all, but probles was that QtGraphicalEffects needs no dll, but needs QML file from Qt\Qt5.0.2\5.0.2\mingw47_32\qml\QtGraphicalEffects

                            1 Reply Last reply
                            0
                            • EddyE Offline
                              EddyE Offline
                              Eddy
                              wrote on 31 Mar 2014, 08:57 last edited by
                              #14

                              [quote author="petro2033" date="1396232213"]ty all, but probles was that QtGraphicalEffects needs no dll, but needs QML file from Qt\Qt5.0.2\5.0.2\mingw47_32\qml\QtGraphicalEffects[/quote]

                              Does that mean that you figured out the problem? If so, please mark the thread as [Solved] by editing the title of you first post.)

                              Qt Certified Specialist
                              www.edalsolutions.be

                              1 Reply Last reply
                              0

                              1/14

                              27 Mar 2014, 13:10

                              • Login

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