Qt quick 按鈕控件及其樣式設置

在編寫qml文檔如果想要使用Button控件就必須在文件頭部添加:import QtQuick.Controls 1.4

如果想要設置Button的樣式則需要在文件頭部添加:import QtQuick.Controls.Styles 1.4

import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4

Window {
    visible: true
    width: 320
    height: 400

    color: "green"
    Button {
        id: openFile
        text: "打開"
        width: 100
        height: 40
        //anchors作爲當前控件Button的錨點,可以表示控件的上下邊的y左右邊的x
        //Button左邊邊界的值爲Window左邊邊界的值
        anchors.left: parent.left
        //邊界相隔距離從0變成了10,相當於Button向左平移了10
        anchors.leftMargin: 10
        anchors.top: parent.top
        anchors.topMargin: 10;

    }

    Button {
        id: quit
        text: "退出"
        width: 100
        height: 40
        anchors.right: parent.right
        anchors.rightMargin: 10
        anchors.bottom: openFile.bottom

        style: ButtonStyle {
            //進行對Button的樣式設置
            background: Rectangle {

                //設置圓角
                radius: 5;
                color: "red"
            }
        }
        //點擊按鈕則退出程序
        onClicked: {
            Qt.quit()
        }
    }
}
//特別的,信號與槽在Qt Quick中的使用:如發出sendMsg信號,則響應的槽函數爲onSendMsg();

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章