1. 程式人生 > >QML設定視窗背景

QML設定視窗背景

剛開始接觸QML:

先上圖了:

上程式碼:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2

ApplicationWindow {
    title: qsTr("Hello World")
    width: 640
    height: 480
    visible: true

    Image {
        id: rocket
        fillMode: Image.TileHorizontally
        smooth: true
        source: 'images/background.png'
    }

    menuBar: MenuBar {
        Menu {
            title: qsTr("&File")
            MenuItem {
                text: qsTr("&Open")
                onTriggered: messageDialog.show(qsTr("Open action triggered"));
            }
            MenuItem {
                text: qsTr("E&xit")
                onTriggered: Qt.quit();
            }
        }
    }

    MainForm {
        anchors.fill: parent
        button1.onClicked: messageDialog.show(qsTr("Button 1 pressed"))
        button2.onClicked: messageDialog.show(qsTr("Button 2 pressed"))
        button3.onClicked: messageDialog.show(qsTr("Button 3 pressed"))
    }

    MessageDialog {
        id: messageDialog
        title: qsTr("May I have your attention, please?")

        function show(caption) {
            messageDialog.text = caption;
            messageDialog.open();
        }
    }
}

關於背景圖片的路徑問題:

本地圖片:是要加file:///, 否則會報錯, 無法開啟圖片.

載入到資源裡面的檔案, 才可以像這樣直接引用.

最後上一副QCreate的工程圖: