2017-03-11 36 views
0

改變style的一個組件,似乎取代全部默認樣式的功能。有沒有辦法只改變一個功能?QT QML如何改變一個功能,比如按鈕樣式

例如,假設我想要一個紅色按鈕;

import QtQuick 2.7 
import QtQuick.Controls 1.4 
import QtQuick.Controls.Styles 1.4 

ApplicationWindow 
{ 
    visible: true 
    width: 640 
    height: 480 

    Button 
    { 
     height: 200 
     width: 200 
     text: "Press me" 
     style: ButtonStyle 
     { 
      // changes background but also throws away everything else 
      // in standard button style 
      background: Rectangle { color: "red" } 
     } 
    } 
} 

重新定義ButtonStylebackground工作正常,改變按鈕的顏色,但隨後系統默認ButtonStyle內的一切都沒了。例如,邊框和點擊突出顯示。

如何更改一個功能並保留其餘功能?

對不起,如果這之前已經問過。

感謝,

更新

上述問題對於控制1,但對於控制2.下面是控制2.

import QtQuick 2.7 
import QtQuick.Controls 2.1 

ApplicationWindow 
{ 
    visible: true 
    width: 640 
    height: 480 

    Button 
    { 
     height: 200 
     width: 200 
     text: "Press me" 

     // changes background but also throws away everything else 
     // in standard button style 
     background: Rectangle { color: "red" } 
    } 
} 

回答

1

前言相同的示例代碼中存在同樣的問題
QtQuick.Controls 1.4的目的是,提供控制與原生外觀和感覺。如果你想擁有更易於調節的控制器,並且不需要原生外觀,那麼應該考慮更新更快的QtQuick.Controls 2.0

主要
你的願望是什麼 - AFAIK - 不可能的,因爲默認樣式包括了兩個Rectangles和一個Image其中Image似乎是最重要的。您可以在這個位置找到在MinGW的 -package圖像:

的Qt \ Qt5.7.0 \ 5.7 \ mingw53_32 \ QML \ QtQuick \控制\樣式\基地\圖像

要訪問控制的對象,你在這裏找到他們:

Button { 
     id: but2 
     x: 50 
     onClicked: console.log(this.children[1].item.children[0].item.children[0], this.children[1].item.children[0].item.children[1], this.children[1].item.children[0].item.children[2]) 
    } 

所以,最簡單的解決辦法,流行在我的心裏是使用Colorize

import QtQuick 2.0 
import QtQuick.Window 2.0 
import QtQuick.Controls 1.4 
import QtQuick.Controls.Styles 1.4 
import QtGraphicalEffects 1.0 
Window { 
    width: 1024 
    height: 800 
    visible: true 

    Button { 
     id: but 
     text: 'test' 
    } 
    Colorize { 
     source: but 
     anchors.fill: but 

     // values from the documentation example. 
     hue: 0.0 
     saturation: 0.5 
     lightness: -0.2 
    } 
} 

或者更一般的:爲了調整樣式,去着色器。

QtQuick.Controls 2.0
有你有另一種情況:在background一個Rectangle不僅僅是Component。但是如果你不指定你自己,那麼只能在Button之後創建。

Button { 
    id: but1 
    background.color: 'red' 
} 

是不可能的,因爲背景在您嘗試分配顏色時沒有實例化。
您可以使用Component.onCompleted處理程序來做到這一點:

Button { 
    id: but1 
    Component.onCompleted: background.color = 'red' 
} 

當然你覆蓋原有風格的Binding,雖然在Button是beeing 按下

Button { 
    id: but1 
    Component.onCompleted: background.color = Qt.binding(function() { return (but1.pressed ? 'red' : 'green') } 
} 
處理色變

將再次啓用顏色更改,但您不會獲得原始顏色。 你可能會想出來retrive的一部開拓創新的色彩:

Button { 
    id: but1 
    onPressed: console.log(this.background.color) 
    onReleased: console.log(this.background.color) 
} 

這將輸出你這兩個國家對pressed的顏色。但也許還有更多!所以最簡單的解決方案是使用有條件的Binding,因爲這樣:

Button { 
    id: but1 
} 

Binding { 
    when: but1.pressed 
    target: but1 
    property: 'background.color' 
    value: 'red' 
} 
+0

bugger。抱歉是的,我應該使用Controls2.Button。我改變了這一點,但它除了'Label' - >'contentItem'外都是一樣的。然後我有同樣的問題,只要我重寫,說'背景'。我喜歡關於着色器和Colorize的想法。我認爲這是添加樣式比改變這些屬性更好的方法。 FWIW,我認爲如果你不能以某種方式繼承原件,那麼這些屬性是不好的設計。謝謝。我會繼續提出你的建議。 –

+1

請更新您的問題以反映您實際使用的內容。他們是兩個非常不同的樣式API。 – Mitch

+0

雖然我已經更新了答案以解決QtQuick.Controls 2.x的一些可能性,但更新問題以匹配問題是很好的,因此它可以幫助其他人。進一步請不要忘記接受答案,如果它包含你在找什麼。同樣請這樣做,對於其他問題的所有答案,因爲這樣做既:對花時間回答的人有禮貌,並幫助人們找到答案,如果他們遇到同樣的問題。 – derM