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. ButtonStyle in sepperate QML File

ButtonStyle in sepperate QML File

Scheduled Pinned Locked Moved QML and Qt Quick
qmlstylesqtquickqtquick control
7 Posts 4 Posters 5.9k 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.
  • H Offline
    H Offline
    hpollak
    wrote on 14 Jun 2015, 09:10 last edited by
    #1

    I try to use ButtonStyle in my Application, but i want to but the style in a sepperate QML-File so the Code look like

    ...
    import "qrc:/styles/qml/Styles"
    ...
    
    Button {
                   id: prjSave
                   x: 4 * projectRec.tabWidth
                   anchors.top: projectState.bottom
                   anchors.topMargin: 5
                   iconSource: "qrc:/icons/resources/document-save-as-3.png"
                   text: "&Save Project.."
    
                   style: MyButtonStyle {}
    
               }
    

    MyButtonStyle.qml:

    import QtQuick 2.4
    import QtQuick.Controls 1.3
    import QtQuick.Controls.Styles 1.3
    
    ButtonStyle {
        background: Rectangle {
            implicitWidth: 100
            implicitHeight: 25
            border.width: control.activeFocus ? 2 : 1
            border.color: "#888"
            radius: 4
            color: "#1f1f1f"
    
        }
    }
    

    I like to change the Text-color of the Button within the Style,

    But when i add

        label: Label {
            color: "#9f9f9f"
        }
    

    To the Style.qml the Button shows no Icon, no Text it still shows the Background.

    Is there a way to change only the textcolor of a button within a Style.qml?

    P 1 Reply Last reply 15 Jun 2015, 05:21
    0
    • H hpollak
      14 Jun 2015, 09:10

      I try to use ButtonStyle in my Application, but i want to but the style in a sepperate QML-File so the Code look like

      ...
      import "qrc:/styles/qml/Styles"
      ...
      
      Button {
                     id: prjSave
                     x: 4 * projectRec.tabWidth
                     anchors.top: projectState.bottom
                     anchors.topMargin: 5
                     iconSource: "qrc:/icons/resources/document-save-as-3.png"
                     text: "&Save Project.."
      
                     style: MyButtonStyle {}
      
                 }
      

      MyButtonStyle.qml:

      import QtQuick 2.4
      import QtQuick.Controls 1.3
      import QtQuick.Controls.Styles 1.3
      
      ButtonStyle {
          background: Rectangle {
              implicitWidth: 100
              implicitHeight: 25
              border.width: control.activeFocus ? 2 : 1
              border.color: "#888"
              radius: 4
              color: "#1f1f1f"
      
          }
      }
      

      I like to change the Text-color of the Button within the Style,

      But when i add

          label: Label {
              color: "#9f9f9f"
          }
      

      To the Style.qml the Button shows no Icon, no Text it still shows the Background.

      Is there a way to change only the textcolor of a button within a Style.qml?

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 15 Jun 2015, 05:21 last edited by
      #2

      Hi @hpollak,
      Are you adding label inside ButtonStyle component in MyButtonStyle.qml ? How does MyButtonStyle.qml look like after adding label ?

      157

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JijuCyriacManjooran
        wrote on 15 Jun 2015, 05:21 last edited by
        #3

        the Label instance does not know what text to show . You need to bind it with the button's text.

        label: Label {
        color: "#9f9f9f"
        text: control.text
        }

        1 Reply Last reply
        0
        • R Offline
          R Offline
          RamK
          wrote on 15 Jun 2015, 07:59 last edited by p3c0
          #4

          If you make use of custom style using ButtonStyle , you will be able to access Button's properties using ButtonStyle.control property.

          To text to be get displayed with custom style it you need to map with control.text.

          In your button code you are setting Button.iconSource also ... which will not be visible if you set custom button style.
          [ By setting custom button style we are going to lose default button platform style]

          To have Icon and Text you need to combine them and set to ButtonStyle.label component.

          Rectangle{
          
              id: rootRect
              width: 400
              height:400
              Button {
                  id: myButton
                  width:200
                  height:40
                  x: 50;y:50
                  iconSource: "images/arrow.png"
                  text: "&button Text"
          
                  style: ButtonStyle{}
          
              }
          }
          

          ButtonStyle in separate file

          import QtQuick 2.4
          import QtQuick.Controls 1.3
          import QtQuick.Controls.Styles 1.3
          
          ButtonStyle {
              id:mybuttonStyle
              background: Rectangle {
                  implicitWidth: 100
                  implicitHeight: 25
                  border.width: control.activeFocus ? 2 : 1
                  border.color: "red"
                  radius: 4
                  color: "lightblue"
              }
              label: Component{
                  id:labelCompoent
                  Row{
                      anchors.left: parent.left
                      anchors.leftMargin: (parent.width - (text.width + image.width))/2
                      anchors.top: parent.top
                      anchors.topMargin: 2
                      spacing: 0
                      Image{ id:image ;source: control.iconSource}
                      Label{
                          id:text
                          height: 30
                          width:100
                          horizontalAlignment:Text.AlignHCenter
                          verticalAlignment: Text.AlignVCenter
          
                          color: "magenta"
                          text: control.text
                      }
                  }
              }
          }
          
          P 1 Reply Last reply 15 Jun 2015, 08:33
          1
          • R RamK
            15 Jun 2015, 07:59

            If you make use of custom style using ButtonStyle , you will be able to access Button's properties using ButtonStyle.control property.

            To text to be get displayed with custom style it you need to map with control.text.

            In your button code you are setting Button.iconSource also ... which will not be visible if you set custom button style.
            [ By setting custom button style we are going to lose default button platform style]

            To have Icon and Text you need to combine them and set to ButtonStyle.label component.

            Rectangle{
            
                id: rootRect
                width: 400
                height:400
                Button {
                    id: myButton
                    width:200
                    height:40
                    x: 50;y:50
                    iconSource: "images/arrow.png"
                    text: "&button Text"
            
                    style: ButtonStyle{}
            
                }
            }
            

            ButtonStyle in separate file

            import QtQuick 2.4
            import QtQuick.Controls 1.3
            import QtQuick.Controls.Styles 1.3
            
            ButtonStyle {
                id:mybuttonStyle
                background: Rectangle {
                    implicitWidth: 100
                    implicitHeight: 25
                    border.width: control.activeFocus ? 2 : 1
                    border.color: "red"
                    radius: 4
                    color: "lightblue"
                }
                label: Component{
                    id:labelCompoent
                    Row{
                        anchors.left: parent.left
                        anchors.leftMargin: (parent.width - (text.width + image.width))/2
                        anchors.top: parent.top
                        anchors.topMargin: 2
                        spacing: 0
                        Image{ id:image ;source: control.iconSource}
                        Label{
                            id:text
                            height: 30
                            width:100
                            horizontalAlignment:Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
            
                            color: "magenta"
                            text: control.text
                        }
                    }
                }
            }
            
            P Offline
            P Offline
            p3c0
            Moderators
            wrote on 15 Jun 2015, 08:33 last edited by p3c0
            #5

            @RamK Surround you code with ``` (3 backticks). I have done it for you now.

            157

            R 1 Reply Last reply 15 Jun 2015, 08:42
            0
            • P p3c0
              15 Jun 2015, 08:33

              @RamK Surround you code with ``` (3 backticks). I have done it for you now.

              R Offline
              R Offline
              RamK
              wrote on 15 Jun 2015, 08:42 last edited by
              #6

              @p3c0 Thanks for updation.

              1 Reply Last reply
              0
              • H Offline
                H Offline
                hpollak
                wrote on 15 Jun 2015, 16:02 last edited by
                #7

                Thanks - The code from RamK is the solution.

                control. ... is the crux.

                Best regards
                Harry

                1 Reply Last reply
                0

                5/7

                15 Jun 2015, 08:33

                • Login

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