QML笔记-自定义控件的2种点击方式(推荐第二种)
生活随笔
收集整理的這篇文章主要介紹了
QML笔记-自定义控件的2种点击方式(推荐第二种)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
?
?
基本概念
代碼及演示
?
基本概念
自定義的時候,可以使用信號進行響應,也可以使用自帶的onClicked進行操作。
?
?
?
代碼及演示
運行截圖都如下:
程序結構都如下:
第一種方法:
main.qml
MButton.qml
import QtQuick 2.0Item {id: rootIdproperty alias buttonText: buttonTextId.textwidth: containerRectId.widthheight: containerRectId.heightRectangle {id: containerRectIdwidth: buttonTextId.implicitWidth + 20height: buttonTextId.implicitHeight + 20color: "red"border{ color: "blue"; width: 3}Text {id: buttonTextIdtext: qsTr("Button")anchors.centerIn: parent}MouseArea{anchors.fill: parentonClicked: {console.log("Clicked on:" + buttonTextId.text)}}} }第二種方法:
main.qml
import QtQuick 2.9 import QtQuick.Window 2.2Window {visible: truewidth: 640height: 480title: qsTr("Hello World")Column{MButton{buttonText: "Button1"onButtonClicked: {console.log("Clicked on: Button1")}}MButton{buttonText: "Button2"onButtonClicked: {console.log("Clicked on: Button2")}}}}MButton.qml
import QtQuick 2.0Item {id: rootIdproperty alias buttonText: buttonTextId.textwidth: containerRectId.widthheight: containerRectId.heightsignal buttonClickedRectangle {id: containerRectIdwidth: buttonTextId.implicitWidth + 20height: buttonTextId.implicitHeight + 20color: "red"border{ color: "blue"; width: 3}Text {id: buttonTextIdtext: qsTr("Button")anchors.centerIn: parent}MouseArea{anchors.fill: parentonClicked: {rootId.buttonClicked()}}} }?
總結
以上是生活随笔為你收集整理的QML笔记-自定义控件的2种点击方式(推荐第二种)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++笔记-DLL的编写与调用
- 下一篇: Nginx文档阅读笔记-Reverse