日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

QML笔记-自定义控件的2种点击方式(推荐第二种)

發(fā)布時(shí)間:2025/3/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QML笔记-自定义控件的2种点击方式(推荐第二种) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

?

?

基本概念

代碼及演示


?

基本概念

自定義的時(shí)候,可以使用信號(hào)進(jìn)行響應(yīng),也可以使用自帶的onClicked進(jìn)行操作。

?

?

?

代碼及演示

運(yùn)行截圖都如下:

程序結(jié)構(gòu)都如下:

第一種方法:
main.qml

import QtQuick 2.9 import QtQuick.Window 2.2Window {visible: truewidth: 640height: 480title: qsTr("Hello World")Column{MButton{buttonText: "Button1"}MButton{buttonText: "Button2"}}}

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()}}} }

?

總結(jié)

以上是生活随笔為你收集整理的QML笔记-自定义控件的2种点击方式(推荐第二种)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。