QML自定义组件

QML自定义组件放入资源里

组件Hello.qml内容,组件名头字母必须大写,组件名就是文件名

import QtQuick 2.0

import "hello.js" as JSCode;

Rectangle{

id:my;

signal myHello(string str); //定义事件事件首字母必须小写

function hello(){

JSCode.hello();

my.myHello("hhh");

}

}

hello.js代码

var ss=1;

function hello(){

console.log("我是组件函数"+ss);

ss++;

}

main.qml

调用

import QtQuick 2.12

import QtQuick.Window 2.12

import QtQuick.Controls 1.4

Window {

id:main;

visible: true;

width: 640;

height: 480;

title: qsTr("Hello World");

 

 

Rectangle{

id:myRect;

anchors.fill: parent;

Button{

anchors.centerIn: parent;

id:myBT;

text:"你好1";

iconSource: "qrc:/img/QQ.png";

onClicked: {

//uuu.hello();

aaa.hello();

}

 

}

Button{

anchors.right:parent.right;

id:myBT2;

text:"你好2";

iconSource: "qrc:/img/QQ.png";

onClicked: {

//uuu.hello();

bbb.hello();

}

 

}

Hello{

id:aaa;

width: 100;

height: 100;

color:"#00ff00";

onMyHello:{ //事件处理 on+事件首字母大写

console.log(str);

}

}

Hello{

id:bbb;

x:aaa.width+20;

width: 100;

height: 100;

color:"#ff0000";

}

}

Component.onCompleted: {

console.log("QML Window\'s c++ type -",main );

console.log("QML Rectangle\'s c++ type -",myRect );

console.log("QML Button\'s c++ type -",myBT );

 

}

}

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值