QML can be multi-select the implementation of the ComboBox

Source: Internet
Author: User

As a result of the project needs, you need to design a multi-select ComboBox, looked at the document, found that QML's own ComboBox does not have many choices. Look at the ComboBox implementation, found that pop-up drop-down menu is implemented with the menu, it seems only to rewrite their own, after all, the menu itself is unable to choose more! The code is not much, look directly to realize it!

import qtquick 2.5import qtquick.controls 1.4import qtquick.controls.styles  1.4item{    id:root    implicitwidth: 150     implicitheight: 30    property alias model: lv.model     property var indexs: []    property var m_text:  ""     function removevalue (Arr,val)  {      // Deletes the specified contents of the array in the ARR array, Val content         for  (var i = 0;  i < arr.length; i++)  {             if  (Arr[i] == val)  {                 arr.splice (i, 1)                  return arr;            }         }    }    Button{         id: btn         anchors.fill: parent        text{             anchors.fill: parent             anchors.margins: 5             anchors.rightMargin: 30             text: m_text             horizontalalignment: text.alignhcenter             verticalalignment: text.alignvcenter            font.pointsize: 12             clip: true             elide: Text.ElideMiddle         }        onClicked: {             sv.visible = !sv.visible;         }        Image{             anchors.right: parent.right             anchors.top: parent.top             width: root.height             height:root.height&Nbsp;           source: "Switch to your own drop-down button image"          }    }    Component{         id: m_del        Rectangle{             id:rect             color: "White"              implicitWidth: 150             implicitheight: 30            property  bool isselect: false            text{                 anchors.fill:  parent  &nBsp;             horizontalalignment: text.alignhcenter                 verticalAlignment: Text.AlignVCenter                 font.pointSize: 15                 text: label             }             mousearea{                 anchors.fill: parent                 hoverEnabled: true                 onentered: {                     if (!isselect) {                         rect.color =  "#CDDCFE";                     }                 }                 onExited: {                     if (!isselect) {                         rect.color =  "White"                      }                 }                 onClicked: {                     rect.isselect = ! rect.isselect;                     if (!rect.isselect) {         //Cancel an item                           rect.color =  "White"                          //delete the specified string in the text                          Var arr = m_text.split (",");                         arr = removevalue (arr, Label)                          m_text = arr.join (",");                          //Delete the specified contents in the array                          indexs = removevalue (Indexs,index);                      }                    else{                         //Select a                          rect.color =  "#F9E977"                           if (m_text.length == 0)                          {                              m_text += label;                         }                          else{                             m_text +=  ","  +  label;                         }                         indexs.push (Index)                       }                }             }        }     }    ScrollView{        id:sv         anchors.top:btn.bottom         anchors.left: parent.left        width: parent.width         height: 300         visible: false        listview{             id: lv             delegate:m_del             anchors.fill: parent        }    }}

There is a place and the official ComboBox some difference, is the need to click the button to put the drop-down list, do not know how to achieve click Other location, close the drop-down list, there is a friend told me, thanks!

This article is from the "men do not have a famous" blog, please be sure to keep this source http://gaohaifeng.blog.51cto.com/6432870/1795667

QML can be multi-select the implementation of the ComboBox

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.