使用QML實現浮動桌面搜索框
前段時間接觸了一下QML,深深地被這門強大易用的語言所吸引。
QML的語法類似CSS,可以引入javascript作為邏輯,還能夠和C++對象交互。
QML帶來的好處至少有以下幾點:
- 分工更明確:設計師可以專攻QML制作UI,C++工程師也能專注于自己的本職工作。
- 開發更高效:重新編寫的QML不需要編譯(因為它是一門腳本語言),所以只需要刷新一下你的QML Viewer就可以了。
- 界面更精美:QML開發和網頁開發很相似,所以我們可以比較容易地把一個精美的網頁效果移植到本地程序。
- 風格更一致:Qt本身是一個跨平臺的開發框架,所以我們在Window XP上看到的是一個樣子,Win7上看到的是另一個樣子,到了Ubuntu或者Mac更是變了模樣,使用QML可以屏蔽這些不一致。


我們將這個QML文件命名為TextBox。這個QML文件主要是實現搜索框中的文本框。
輸入以下代碼:
- import Qt 4.7
- FocusScope {
- id: focusScope
- width: 600; height: 40
- focus:true
- BorderImage {
- source: "../images/lineedit-bg.png"
- width: parent.width; height: parent.height
- border { left: 4; top: 4; right: 4; bottom: 4 }
- }
- Text {
- id: typeSomething
- anchors.fill: parent; anchors.leftMargin: 8
- verticalAlignment: Text.AlignVCenter
- text: "\u8BF7\u8F93\u5165\u7F51\u5740"
- color: "gray"
- }
- MouseArea {
- anchors.fill: parent
- onClicked: { focusScope.focus = true; textInput.openSoftwareInputPanel(); }
- }
- TextInput {
- id: textInput
- anchors { left: parent.left; leftMargin: 8; right: clear.left; rightMargin: 8; verticalCenter: parent.verticalCenter }
- focus: true
- font.pixelSize:20
- }
- Image {
- id: clear
- anchors { right: parent.right; rightMargin: 8; verticalCenter: parent.verticalCenter }
- source: "../images/clear.png"
- opacity: 0
- MouseArea {
- anchors.fill: parent
- onClicked: { textInput.text = ''; focusScope.focus = true; textInput.openSoftwareInputPanel(); }
- }
- }
- states: State {
- name: "hasText"; when: textInput.text != ''
- PropertyChanges { target: typeSomething; opacity: 0 }
- PropertyChanges { target: clear; opacity: 1 }
- }
- transitions: [
- Transition {
- from: ""; to: "hasText"
- NumberAnimation { exclude: typeSomething; properties: "opacity" }
- },
- Transition {
- from: "hasText"; to: ""
- NumberAnimation { properties: "opacity" }
- }
- ]
- }
你也許會注意到,在Text的text屬性中,我輸入的是utf編碼的漢字。事實上,想要在QML文件里顯示中文還是有一點小麻煩的,要么就是用utf編碼過的漢字,要么使用Qt翻譯家來轉換。直接在QML中輸入中文將無法顯示。
接著,新建ShadowRectangle文件。該文件實現的是陰影效果。代碼如下:
- import Qt 4.7
- Item {
- property alias color : rectangle.color
- BorderImage {
- anchors.fill: rectangle
- anchors { leftMargin: 0; topMargin: 0; rightMargin: -8; bottomMargin: -8 }
- border { left: 10; top: 10; right: 10; bottom: 10 }
- source: "../images/shadow.png"; smooth: true
- }
- Rectangle { id: rectangle; anchors.fill: parent; radius:5}
- }
最后新建main.qml文件,該文件實現的是整個搜索欄的效果。其代碼如下:
- import Qt 4.7
- Rectangle {
- id: page
- width: 614; height: 54
- color: "#7bffffff"
- radius:5
- MouseArea {
- anchors.fill: parent
- onClicked: page.focus = false;
- }
- ShadowRectangle {
- color: "#434343"
- transformOrigin: "Center"
- opacity: 0.97
- visible: true
- anchors.centerIn: parent; width: 610; height: 50
- }
- TextBox {
- id: search;
- visible: true
- opacity: 1
- anchors.centerIn: parent
- }
- }
QML的代碼通俗易懂,這里就不去解釋每一行代碼的意思了。
好的,下面讓我們把QML制作的搜索欄放置到桌面程序的窗體上。
在你的floatbox.h中添加一個私有變量:
- private:
- QDeclarativeView *ui;
然后在你的floatbox.cpp的構造函數中輸入以下代碼:
- // 使窗體透明而控件不透明
- setWindowFlags(Qt::FramelessWindowHint);
- setAttribute(Qt::WA_TranslucentBackground, true);
- setStyleSheet("background:transparent;");
- ui = new QDeclarativeView;
- ui->setSource(QUrl("qrc:/resources/qml/main.qml"));
- setCentralWidget(ui);
- resize(QSize(630, 60));
細心的你可以發現,我將qml文件加入了Qt的資源系統。這里要說明一點,非常重要:
在QML文件中如果引入了其他文件(包括js文件、圖片文件等),要么都加入Qt的資源系統,要么都不加入,因為Qt的資源文件無法和本地文件相互訪問。
所以,如果你也和我一樣新建了qrc文件,請將qml文件和圖片文件一并加入到資源系統中去。如下圖:
到了這一步,我們的搜索工具欄差不多要完工了,想要運行,千萬不要忘記在pro文件添加declarative模塊。
- QT += core gui declarative
好的,現在你就可以按下ctrl+R欣賞一下成果了。
最后,老規矩,附上源代碼。