2016-07-06 119 views
1

我正在製作一個自定義的ComboBox項目,兩邊有兩個圖標,中間有一個ComboBox。我希望當點擊任何圖標時,會打開ComboBox下拉菜單,但我不知道該如何操作。如何打開ComboBox下拉菜單?

這裏是我的代碼:

// ComboIcon.qml

Rectangle{ 
    color: "#fff" 
    radius: 10 
    property alias iconSource: icon.source 
    property alias comboModel: combo.model 

    Row{ 
     anchors.fill: parent 
     Item{ 
      width: parent.width * 0.2 
      height: parent.height 
      Image{ 
       id: icon 
       width: parent.width * 0.7 
       height: parent.height * 0.7 
       anchors.centerIn: parent 
      } 
      MouseArea{ 
       anchors.fill: parent 
       // onClicked: combo.?? 
      } 
     } 
     ComboBox{ 
      id: combo 
      width: parent.width * 0.65 
      height: parent.height 
      style: ComboBoxStyle{ 
       background:Rectangle { 
        color: "#fff" 
        anchors.fill: parent 
       } 
       label: Text { 
        height: parent.height * 0.7 
        anchors.verticalCenter: parent.verticalCenter 
        verticalAlignment: Text.AlignVCenter 
        horizontalAlignment: Text.AlignHLeft 
        color: "#6186da" 
        font.family: "SansSerif" 
        font.pointSize : 20 
        fontSizeMode: Text.Fit 
        text: control.currentText 
       } 
      } 
     } 
     Item{ 
      width: parent.width * 0.15 
      height: parent.height 
      Image{ 
       width: parent.width * 0.4 
       height: parent.height * 0.4 
       anchors.centerIn: parent 
       fillMode: Image.PreserveAspectFit 
       source: "../assets/images/combo_arrow.png" 
      } 
      MouseArea{ 
       anchors.fill: parent 
       //onClicked: combo.?? 
      } 
     } 
    } 
} 

我想使用類似combo.clicked()combo.focus = true,但它似乎並沒有工作。任何幫助將非常感激, 謝謝。

回答

2

根據消息來源,Combobox有一個內部屬性__popup。由於它是內部的,因此不能保證在不同版本的Qt中保持一致。但是,由於控件1可以被認爲是「完成的」,所以這種財產在未來的版本中將不太可能發生變化。

使用__popup你可以寫這樣的事情:

import QtQuick 2.2 
import QtQuick.Window 2.0 
import QtQuick.Layouts 1.1 
import QtQuick.Controls 1.4 

ApplicationWindow { 
    visible: true 
    width: 300 
    height: 200 

    RowLayout { 
     anchors.fill: parent 

     Image { 
      fillMode: Image.PreserveAspectFit 
      Layout.preferredHeight: 64 
      source: "https://cdn1.iconfinder.com/data/icons/prettyoffice9/128/open-file.png" 

      MouseArea { 
       anchors.fill: parent 
       onClicked: combo.__popup.toggleShow() // <-- showing the popup here! 
      } 
     } 

     ComboBox { 
      id: combo 
      model: 3 
     } 
    } 
} 

最後,類似的方法可以從控制2應遵循的ComboBox其中popup is not internal並且可以通過簡單地改變其visible屬性,即顯示:

combo.popup.visible = true