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]How can I customize ComboBox?
Forum Updated to NodeBB v4.3 + New Features

[solved]How can I customize ComboBox?

Scheduled Pinned Locked Moved QML and Qt Quick
comboboxcustomizeqmlqtquick
22 Posts 2 Posters 15.1k Views 2 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 p3c0
    5 Jun 2015, 05:09

    @beidaochuan That is because alias'es work with Component's properties and not with Components. More info here. The example should explain.
    Put your code after ``` (3 backticks) and end with the same. I have done it for you now.

    B Offline
    B Offline
    beidaochuan
    wrote on 6 Jun 2015, 15:09 last edited by beidaochuan 6 Jun 2015, 15:11
    #21

    @p3c0
    I modified the source as follows and I think it works well.

    //IComboBox
    import QtQuick 2.3
    import QtQuick.Controls 1.3
    import QtQuick.Controls.Styles 1.3
    
    ComboBox {
        id: root
        width: 200
        height: 50
    
        property alias comboBoxModel: root.model
        signal indexChanged()
        property alias currentIndex: root.currentIndex
        property alias currentText: root.currentText
    
        property Component comboBoxStyleBackground: Component { Rectangle{} }
        property Component dropDownMenuStyleFrame: Component { Rectangle{} }
    
        function setComboBoxStyleBackground(background) {
            comboBoxStyleBackground = background
        }
    
        function setDropDownMenuStyleFrame(frame) {
            dropDownMenuStyleFrame = frame
        }
    
        model: ListModel {
            id: cbItems
            ListElement { text: "" }
        }
    
        style: ComboBoxStyle {
            id: comboBoxStyle
            background: comboBoxStyleBackground
            label: Text {
                color: "black"
                width: root.width
                height: root.height
                text: control.currentText
            }
    
            __dropDownStyle: MenuStyle {
                id: dropDownMenuStyle
                frame: dropDownMenuStyleFrame
                itemDelegate.label: Text {
                    width:root.width - 50
                    height: root.height
                    color: styleData.selected ? "blue" : "black"
                    text: styleData.text
                }
    
                itemDelegate.background: Rectangle {
                    z: 1
                    opacity: 0.5
                    color: styleData.selected ? "darkGray" : "transparent"
                }
            }
        }
    
        onCurrentIndexChanged: {
            root.indexChanged()
        }
    }
    
    //MyComboBox
    import QtQuick 2.3
    import QtQuick.Controls 1.3
    import QtQuick.Controls.Styles 1.3
    
    IComboBox {
        id: root
        width: 200
        height: 50
    
        property int m_userLevel: 1
        comboBoxModel: ListModel {
            id: cbItems
            ListElement { text: "" }
            ListElement { text: "" }
            ListElement { text: "" }
            ListElement { text: "" }
        }
    
    
        Component {
            id: comboBoxStyleBackground
            IUserLevelImage {
                anchors.fill: parent
                userLevel: m_userLevel
            }
        }
    
    
        Component {
            id: dropDownMenuStyleFrame
            IUserLevelImage1 {
            }
        }
        onIndexChanged: {
            m_userLevel = currentIndex + 1
        }
        Component.onCompleted: {
            setComboBoxStyleBackground(comboBoxStyleBackground)
            setDropDownMenuStyleFrame(dropDownMenuStyleFrame)
        }
    }
    
    P 1 Reply Last reply 7 Jun 2015, 04:22
    0
    • B beidaochuan
      6 Jun 2015, 15:09

      @p3c0
      I modified the source as follows and I think it works well.

      //IComboBox
      import QtQuick 2.3
      import QtQuick.Controls 1.3
      import QtQuick.Controls.Styles 1.3
      
      ComboBox {
          id: root
          width: 200
          height: 50
      
          property alias comboBoxModel: root.model
          signal indexChanged()
          property alias currentIndex: root.currentIndex
          property alias currentText: root.currentText
      
          property Component comboBoxStyleBackground: Component { Rectangle{} }
          property Component dropDownMenuStyleFrame: Component { Rectangle{} }
      
          function setComboBoxStyleBackground(background) {
              comboBoxStyleBackground = background
          }
      
          function setDropDownMenuStyleFrame(frame) {
              dropDownMenuStyleFrame = frame
          }
      
          model: ListModel {
              id: cbItems
              ListElement { text: "" }
          }
      
          style: ComboBoxStyle {
              id: comboBoxStyle
              background: comboBoxStyleBackground
              label: Text {
                  color: "black"
                  width: root.width
                  height: root.height
                  text: control.currentText
              }
      
              __dropDownStyle: MenuStyle {
                  id: dropDownMenuStyle
                  frame: dropDownMenuStyleFrame
                  itemDelegate.label: Text {
                      width:root.width - 50
                      height: root.height
                      color: styleData.selected ? "blue" : "black"
                      text: styleData.text
                  }
      
                  itemDelegate.background: Rectangle {
                      z: 1
                      opacity: 0.5
                      color: styleData.selected ? "darkGray" : "transparent"
                  }
              }
          }
      
          onCurrentIndexChanged: {
              root.indexChanged()
          }
      }
      
      //MyComboBox
      import QtQuick 2.3
      import QtQuick.Controls 1.3
      import QtQuick.Controls.Styles 1.3
      
      IComboBox {
          id: root
          width: 200
          height: 50
      
          property int m_userLevel: 1
          comboBoxModel: ListModel {
              id: cbItems
              ListElement { text: "" }
              ListElement { text: "" }
              ListElement { text: "" }
              ListElement { text: "" }
          }
      
      
          Component {
              id: comboBoxStyleBackground
              IUserLevelImage {
                  anchors.fill: parent
                  userLevel: m_userLevel
              }
          }
      
      
          Component {
              id: dropDownMenuStyleFrame
              IUserLevelImage1 {
              }
          }
          onIndexChanged: {
              m_userLevel = currentIndex + 1
          }
          Component.onCompleted: {
              setComboBoxStyleBackground(comboBoxStyleBackground)
              setDropDownMenuStyleFrame(dropDownMenuStyleFrame)
          }
      }
      
      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 7 Jun 2015, 04:22 last edited by
      #22

      @beidaochuan That's good :)
      If you are done, please mark the post as solved.

      157

      1 Reply Last reply
      0

      21/22

      6 Jun 2015, 15:09

      • Login

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