附網址:http://qt-project.org/doc/qt-5/qtquick-usecase-styling.htmlhtml
Use Case - Style And Theme Support—— 用例 - 風格和主題支持api
Qt Quick模塊提供的類型並不能獨立地覆蓋用戶界面所須要的全部組件。一個常見的作法是經過Qt Quick的基本模塊開發一套自定義樣式的用戶界面組件。經過可複用組件咱們很容易作到這一點。ui
經過使用可複用組件的方式,你能夠定義該組件在程序中須要呈現的外觀,並直接爲它設計一個風格。而後你能夠使用它來代替那些沒有風格的類型。例如,你能夠建立一個MyText.qml,假設它的屬性已經被你明確設置好,而後你就能夠使用MyText來代替你的應用程序中的全部Text。spa
Example Themed Text —— 主題文本示例.net
Button Definition設計
import QtQuick 2.0
code
Text {
orm
color: "lightsteelblue"
htm
font { family: 'Courier'; pixelSize: 20; bold: true; capitalization: Font.SmallCaps }
blog
}
·
Using the Text
Column {
spacing: 20
MyText { text: 'I am the very model of a modern major general!' }
MyText { text: 'I\'ve information vegetable, animal and mineral.' }
MyText {
width: root.width
wrapMode: Text.WordWrap
text: 'I know the kings of England and I quote the fights historical:'
}
MyText { text: 'From Marathon to Waterloo in order categorical.' }
}
·
因爲MyText.qml中的根項目是一個Text,所以它的行爲相似一個Text項目,而且其屬性能夠被重寫以適合特殊的用途。然而,與Text不一樣的是,當MyText第一次生成時這些屬性值就被明確設定,所以程序默認應用了你的風格。
對於預置風格的用戶界面組件,能夠查看Qt Components add-on(譯者:這裏應該是有一個鏈接的,可是官網上沒有),它提供了一系列組件。要了解系統主題,能夠參考SystemPalette類型文檔。
Example Themed Button —— 主題按鈕示例
Button Definition
import QtQuick 2.0
Rectangle {
id: container
// The caption property is an alias to the text of the Text element, so Button users can set the text
property alias caption: txt.text
// The clicked signal is emitted whenever the button is clicked, so Button users can respond
signal clicked
// The button is set to have rounded corners and a thin black border
radius: 4
border.width: 1
// This button has a fixed size, but it could resize based on the text
width: 160
height: 40
// A SystemPalette is used to get colors from the system settings for the background
SystemPalette { id: sysPalette }
gradient: Gradient {
// The top gradient is darker when 'pressed', all colors come from the system palette
GradientStop { position: 0.0; color: ma.pressed ? sysPalette.dark : sysPalette.light }
GradientStop { position: 1.0; color: sysPalette.button }
}
Text {
id: txt
// This is the default value of the text, but most Button users will set their own with the caption property
text: "Button"
font.bold: true
font.pixelSize: 16
anchors.centerIn: parent
}
MouseArea {
id: ma
anchors.fill: parent
// This re-emits the clicked signal on the root item, so that Button users can respond to it
onClicked: container.clicked()
}
}
·
Using the button
import QtQuick 2.0
Item {
width: 320
height: 480
Rectangle {
color: "#272822"
width: 320
height: 480
}
Column {
width: childrenRect.width
anchors.centerIn: parent
spacing: 8
// Each of these is a Button as styled in Button.qml
Button { caption: "Eeny"; onClicked: console.log("Eeny");}
Button { caption: "Meeny"; onClicked: console.log("Meeny");}
Button { caption: "Miny"; onClicked: console.log("Miny");}
Button { caption: "Mo"; onClicked: console.log("Mo");}
}
}
·
參考有關教程以瞭解更多建立QML定製UI組件的示例。(譯者:好吧這裏也沒有鏈接,我找到有關教程會貼在這裏)