2016-09-15 71 views
1

我試圖以自定義在組合框的標籤,但它似乎並沒有工作,我有以下代碼:ComboBoxStyle QML QT標籤不工作

ComboBox { 
    id:tableName 
    editable: true 
    currentIndex: 0 
    model: eventModel.getTables() 
    anchors.left: parent.left; anchors.leftMargin: 20 
    y: parent.height/10 
    width: buttonWidth 
    height: 70 
    style: ComboBoxStyle { 
     id:comboStyle 
     background: Rectangle { 
      id: rectCategory 
      width: control.width 
      height: control.height 
      color: "#FFECECEC" 
      Text { 
       anchors.verticalCenter: parent.verticalCenter 
       anchors.right: parent.right 
       anchors.rightMargin: 2 
       font.pointSize: 15 
       font.family: "sans serif" 
       color: control.hovered?"#FF6BC1E5":"#FF404040" 
       text:"˅" 
      } 
     } 

     label: Label{ 
       Rectangle{anchors.fill: tableName; color:"red"} 
       anchors.right: parent.right 
       anchors.rightMargin:10 
       font.pointSize: 12 
       font.family: "sans serif" 
       color: "black" 
       text:control.currentText 
     } 


     // drop-down customization here 
     property Component __dropDownStyle: MenuStyle { 
      __maxPopupHeight: 600 

      __menuItemType: "comboboxitem" 

      frame: Rectangle {    // background 
       color: "#FFACACAC" 
      } 

      itemDelegate.label: Text { 
       verticalAlignment: Text.AlignVCenter 
       horizontalAlignment: Text.AlignHCenter 
       font.pointSize: 12 
       font.family: "sans serif" 
       color: styleData.selected ? "#FF6BC1E5" : "#FF404040" 
       text: styleData.text 
      } 

      itemDelegate.background: Rectangle { // selection of an item 
       color: styleData.selected ? "#FF404040" : "#FFECECEC" 
      } 

      __scrollerStyle: ScrollViewStyle { } 
     } 


    } 

下拉定製似乎工作精緻漂亮,但標籤保持不變,這是輸出I得到:

enter image description here

感謝jpnurmi,我發現,工作風格:

ComboBoxStyle { 
     id:comboStyle 
     textColor:"blue" 
     font{ 
      pointSize:15 
      family:"sans serif" 
     } 

     background: Rectangle { 
      id: rectCategory 
      width: control.width 
      height: control.height 
      color: "#FFECECEC" 

      Image { 
       anchors.verticalCenter: parent.verticalCenter 
       anchors.right: parent.right 
       anchors.rightMargin: 5 
       source:control.hovered?"images/select2.png": 
             "images/select.png" 
      } 
     } 

     __editor: 
      Rectangle { 
      anchors.fill: parent 
      color: "#00000000" 
     } 



     // drop-down customization here 
     property Component __dropDownStyle: MenuStyle { 
      __maxPopupHeight: 600 

      __menuItemType: "comboboxitem" 

      frame: Rectangle {    // background 
       color: "#FFACACAC" 
      } 
      itemDelegate.label: Text { 
       verticalAlignment: Text.AlignVCenter 
       horizontalAlignment: Text.AlignHCenter 
       font.pointSize: 12 
       font.family: "sans serif" 
       color: styleData.selected ? "#FF6BC1E5" : "#FF404040" 
       text: styleData.text 
      } 
      itemDelegate.background: Rectangle { // selection of an item 
       color: styleData.selected ? "#FF404040" : "#FFECECEC" 
      } 
      __scrollerStyle: ScrollViewStyle { } 
     } 


    } 

回答

1

只讀標籤用於不可編輯ComboBox。試試這個:

style: ComboBoxStyle { 
     font.pointSize: 12 
     font.family: "sans serif" 
     textColor: "black" 
     background: Rectangle { 
      ... 
     } 

     __editor: Rectangle { 
      color: "red" 
     } 

     ... 
    } 
+0

的__editor部分作品不錯的感謝,而是試圖分配字體時,我得到的錯誤字體不的hve任何成員M17 – heczaco

+0

您有哪些版本的Qt?看起來ComboBoxStyle :: font是在QtQuick.Controls.Styles 1.3中引入的。如果您導入版本1.3,是否會收到「不可用」錯誤? – jpnurmi

+0

我找到了解決方法,謝謝,我會發布代碼,感謝您的幫助,我正在開發Qt 5-7 – heczaco