Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How to set style of button in qml?

    QML and Qt Quick
    4
    4
    5723
    Loading More Posts
    • 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
      Pyroxar last edited by Pyroxar

      I read the documentation about this but my code not work.
      http://doc.qt.io/qt-5/qml-qtquick-controls-styles-buttonstyle.html

      import QtQuick 2.7
      import QtQuick.Controls 2.0
      import QtQuick.Controls.Styles 1.4
      import QtQuick.Controls.Material 2.1
      import QtQuick.Layouts 1.0
      import QtQuick.Window 2.2
      import QtQuick.Layouts 1.3
      
      
      ApplicationWindow {
          visible: true
          width: 360
          height: 640
          title: qsTr("Hello World")
      
          Button {
              text: "A button"
              style: ButtonStyle {
                  background: Rectangle {
                      implicitWidth: 100
                      implicitHeight: 25
                      border.width: control.activeFocus ? 2 : 1
                      border.color: "#888"
                      radius: 4
                      gradient: Gradient {
                          GradientStop { position: 0 ; color: control.pressed ? "#ccc" : "#eee" }
                          GradientStop { position: 1 ; color: control.pressed ? "#aaa" : "#ccc" }
                      }
                  }
              }
          }
      }
      

      I have a error.
      QQmlApplicationEngine failed to load component
      qrc:/main.qml:21 Cannot assign to non-existent property "style"

      What happened?

      1 Reply Last reply Reply Quote 1
      • ?
        A Former User last edited by A Former User

        Hi! You're importing QtQuick.Controls 2. So your Button is from Controls 2. But styles are only for buttons from Controls 1. See Button QML Type, and Customizing Button for the Controls 2 / Button doc pages.

        1 Reply Last reply Reply Quote 4
        • R
          rowild last edited by

          In my opinion this should be made a lot clearer in the various parts of the documentation...

          sierdzio 1 Reply Last reply Reply Quote 1
          • sierdzio
            sierdzio Moderators @rowild last edited by

            @rowild said in How to set style of button in qml?:

            In my opinion this should be made a lot clearer in the various parts of the documentation...

            Yeah, it's a very common issue which requires "insider knowledge".

            Thankfully, once Qt 6 comes out, Controls 1 will vanish completely so it will stop being an issue.

            (Z(:^

            1 Reply Last reply Reply Quote 1
            • First post
              Last post