2012-03-09 76 views
7

简单的事情,但无法找到它。我想要一个简单的下拉选择框和几个选择。 Like in HTMLQt QML下拉列表,如HTML中的

<select> 
<option>1</option> 
<option>2</option> 
</select> 

QML的代码是什么?

+4

作为,因为它可能看起来,存在QML没有“内置”组合框奇怪。您必须使用其他基元自己构建它。虽然有很多例子。 – Koying 2012-03-09 14:13:30

+0

确实很奇怪:S – Mathlight 2012-03-09 14:24:33

回答

14

这里有一个简单的例子,也许可以作为一个起点:

import QtQuick 1.0 

Rectangle { 
    width:400; 
    height: 400; 

    Rectangle { 
      id:comboBox 
      property variant items: ["Item 1", "Item 2", "Item 3"] 
      property alias selectedItem: chosenItemText.text; 
      property alias selectedIndex: listView.currentIndex; 
      signal comboClicked; 
      width: 100; 
      height: 30; 
      z: 100; 
      smooth:true; 

      Rectangle { 
       id:chosenItem 
       radius:4; 
       width:parent.width; 
       height:comboBox.height; 
       color: "lightsteelblue" 
       smooth:true; 
       Text { 
        anchors.top: parent.top; 
        anchors.left: parent.left; 
        anchors.margins: 8; 
        id:chosenItemText 
        text:comboBox.items[0]; 
        font.family: "Arial" 
        font.pointSize: 14; 
        smooth:true 
       } 

       MouseArea { 
        anchors.fill: parent; 
        onClicked: { 
         comboBox.state = comboBox.state==="dropDown"?"":"dropDown" 
        } 
       } 
      } 

      Rectangle { 
       id:dropDown 
       width:comboBox.width; 
       height:0; 
       clip:true; 
       radius:4; 
       anchors.top: chosenItem.bottom; 
       anchors.margins: 2; 
       color: "lightgray" 

       ListView { 
        id:listView 
        height:500; 
        model: comboBox.items 
        currentIndex: 0 
        delegate: Item{ 
         width:comboBox.width; 
         height: comboBox.height; 

         Text { 
          text: modelData 
          anchors.top: parent.top; 
          anchors.left: parent.left; 
          anchors.margins: 5; 

         } 
         MouseArea { 
          anchors.fill: parent; 
          onClicked: { 
           comboBox.state = "" 
           var prevSelection = chosenItemText.text 
           chosenItemText.text = modelData 
           if(chosenItemText.text != prevSelection){ 
            comboBox.comboClicked(); 
           } 
           listView.currentIndex = index; 
          } 
         } 
        } 
       } 
      } 

      Component { 
       id: highlight 
       Rectangle { 
        width:comboBox.width; 
        height:comboBox.height; 
        color: "red"; 
        radius: 4 
       } 
      } 

      states: State { 
       name: "dropDown"; 
       PropertyChanges { target: dropDown; height:40*comboBox.items.length } 
      } 

      transitions: Transition { 
       NumberAnimation { target: dropDown; properties: "height"; easing.type: Easing.OutExpo; duration: 1000 } 
      } 
     } 
    } 
+0

只有一个奇怪的事情,我有几个选项(此刻5)。当我选择选项3时,我不再看到1和2:S我必须改变什么? – Mathlight 2012-03-09 14:39:42

+0

srr,没有很好的阅读:P我不得不删除的行listView.currentIndex = index;的鼠标区域。非常感谢这段代码:P – Mathlight 2012-03-09 14:43:13

+0

如何从包含此代码的文件外访问selectedIndex? – 2013-04-08 21:06:23

7

对于新用户来说,有一个建于ComboBox在Qt5.3 QtQuick.ControlsComboBox - Reference 。从文档

例子:

import QtQuick 2.2 
import QtQuick.Controls 1.2 

ComboBox { 
    id: combo 
    editable: true 
    model: ListModel { 
    id: model 
    ListElement { text: "Banana"; color: "Yellow" } 
    ListElement { text: "Apple"; color: "Green" } 
    ListElement { text: "Coconut"; color: "Brown" } 
} 
onAccepted: { 
    if (combo.find(currentText) === -1) { 
    model.append({text: editText}) 
    currentIndex = combo.find(editText) 
    } 
} 
} 

注:我不得不将它张贴作为一个答案,因为文字是注释太长。

1

我一直在使用与ComboBoxStyle(有限的定制能力)和完全自定义的实现方法,但他们有一个很大的局限性与focus管理和z-index管理。

我已经完成了ComboBox的实现,它由2部分组成:实际放置在某处的头部和动态创建的下拉部件。后者由覆盖所有内容(并拦截鼠标活动)的Item以及精心定位在标题下的下拉列表组成。

代码是非常巨大的,以包括在这里,所以你可以看到细节in my blogpost with all the code

+0

虽然它是无耻的博客广告,虽然你似乎已经回答了我的问题。我不再使用qt,所以我无法测试你的代码,但它看起来很有希望。但我认为,这可以帮助未来有人解决这个问题。因此,我不会标记你的答案,因为它有潜力;-) – Mathlight 2017-11-22 22:44:39

+0

@Mathlight示例代码是相当大的包括在这里。在我的回答中解释了可以解决您的问题的想法,因此如果您拥有QML技能,您就可以了解我所写的内容(无需转到我的无耻标榜的博客)。 – Ribtoks 2017-11-23 09:32:42