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. Button backgound non-existent property!
QtWS25 Last Chance

Button backgound non-existent property!

Scheduled Pinned Locked Moved Solved QML and Qt Quick
backgroundcolorbuttonqt quick 2.0
6 Posts 3 Posters 3.6k 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.
  • T Offline
    T Offline
    tansgumus
    wrote on 1 Aug 2017, 04:36 last edited by
    #1

    Using QtQuick.Controls 2.0 I want to change the backgound color of Button but Qt says to me

    Cannot assign to non-existent property "color"
    

    Why I can't use background.color: "red" while I can use button.background.color = "red"?!

        Button {
            id: button
            x: 220
            y: 281
            width: 200
            height: 40
            text: qsTr("Button")
            // ERROR!!!
            //background.color: "red"
        }
        
        Component.onCompleted: {
            // WORKS!
            button.background.color = "red"
        }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 1 Aug 2017, 04:56 last edited by
      #2

      Background is not an attached property, so setting up bindings like object.property: something will not work.

      You can either use Binding QML type, or define the background yourself:

      Button {
        background: Rectangle {
          color: "red"
        }
      }
      

      (Z(:^

      T 2 Replies Last reply 1 Aug 2017, 05:10
      1
      • S sierdzio
        1 Aug 2017, 04:56

        Background is not an attached property, so setting up bindings like object.property: something will not work.

        You can either use Binding QML type, or define the background yourself:

        Button {
          background: Rectangle {
            color: "red"
          }
        }
        
        T Offline
        T Offline
        tansgumus
        wrote on 1 Aug 2017, 05:10 last edited by
        #3

        @sierdzio Thank you for quick response

        May I ask you why this works although Qt Creator unable to recognize it in auto completion!

        Component.onCompleted: {
                // WORKS!
                button.background.color = "red"
            }
        
        1 Reply Last reply
        0
        • S sierdzio
          1 Aug 2017, 04:56

          Background is not an attached property, so setting up bindings like object.property: something will not work.

          You can either use Binding QML type, or define the background yourself:

          Button {
            background: Rectangle {
              color: "red"
            }
          }
          
          T Offline
          T Offline
          tansgumus
          wrote on 1 Aug 2017, 05:19 last edited by
          #4

          Sorry @sierdzio but I asked this question because I want to change Button background color on runtime like this

              Button {
                  id: button
                  x: 220
                  y: 281
                  width: 200
                  height: 40
                  text: qsTr("Button")
              }
          
              Component.onCompleted: {
                  // WORKS!
                  //        button.background.color = "red"
                  // WRONG
                  background = Rectangle { color: "red" }
              }
          
          
          E 1 Reply Last reply 1 Aug 2017, 10:34
          0
          • S Offline
            S Offline
            sierdzio
            Moderators
            wrote on 1 Aug 2017, 05:28 last edited by
            #5

            It's better to use declarative approach and do it via binding, but yes, changing it via JavaScript in a function is possible, too.

            Why Qt Creator did not auto-complete it? Well, probably it's engine does not understand it well enough. What you did in Component.onCompleted is perfectly legal, though. Don't worry about Qt Creator :-)

            BTW. Also, consider using Controls' styles: link.

            (Z(:^

            1 Reply Last reply
            1
            • T tansgumus
              1 Aug 2017, 05:19

              Sorry @sierdzio but I asked this question because I want to change Button background color on runtime like this

                  Button {
                      id: button
                      x: 220
                      y: 281
                      width: 200
                      height: 40
                      text: qsTr("Button")
                  }
              
                  Component.onCompleted: {
                      // WORKS!
                      //        button.background.color = "red"
                      // WRONG
                      background = Rectangle { color: "red" }
                  }
              
              
              E Offline
              E Offline
              Eeli K
              wrote on 1 Aug 2017, 10:34 last edited by
              #6

              @tansgumus said in Button backgound non-existent property!:

                  Component.onCompleted: {
                      // WORKS!
                      //        button.background.color = "red"
                      // WRONG
                      background = Rectangle { color: "red" }
                  }
              
              

              Because Component.onCompleted is a javascript function and you can use only js syntax inside it. Rectangle { color: "red" } is QML specific syntax, js doesn't understand it. You can create a component dynamically (see the QML docs) by giving the component as a string to Qt.createQmlObject().

              If you use button.background.color = "red" in Component.onCompleted it retains all other properties of the backround; I have found it to be a convenient way to change only one or two properties without messing others. Using other approaches, i.e. defining the whole component either dynamically or declaratively, overwrites everything which may or may not be desirable.

              1 Reply Last reply
              1

              1/6

              1 Aug 2017, 04:36

              • Login

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