Qml:信号

简介: Qml:信号
//test_signal.qml
import QtQuick
import QtQuick.Controls
Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")
    Rectangle
    {
        Component.onCompleted:
        {
            print("Rectangle onCompleted")
        }
        Component.onDestruction:
        {
            print("Rectangle onDestruction")
        }
    }
    property Item myItem:Item
    {
        Component.onCompleted:
        {
            print("property Item onCompleted")
        }
        Component.onDestruction:
        {
            print("property Item onDestruction")
        }
    }
    Button
    {
        /*
        canceled()
        clicked()
        doubleClicked()
        pressAndHold()
        pressed()
        released()
        toggled()
        */
        text:"test signal"
        onClicked:
        {
            print("Button onClicked")
        }
        onPressed:
        {
            print("Button onPressed")
        }
    }
    //signal <name>[([<type> <parameter name>[, ...]])]
    //接收信号通知,定义一个名为on<Signal> 函数,其中<Signal>是信号的名称,第一个字母大写
    Button
    {
        y:30
        text:"mySignal"
        signal mySignal
        signal mySignalPara(int index,string str)
        onClicked:
        {
            mySignal()
            mySignalPara(99,"InButonnStr")
        }
        //on<Signal>
        onMySignal:
        {
            print("call onMySignal1 ")
        }
        onMySignalPara:(i,s)=>
        {
            print("call onMySignalPara "+ i+":"+s)
        }
    }
    Rectangle
        {
            x:100
            width: 100
            height: 100
            color:"red"
            property real size: 1.0
            //on<Property>Changed 的形式编写,其中<Property>是属性的名称,第一个字母大写
            onSizeChanged:
            {
                width = size*100
                height = size*100
            }
            Button
            {
                text: "Change size"
                onClicked: parent.size+=0.1
            }
        }
        ///手动将信号连接到方法
    Item
    {
        signal testConnectSig
        function testConnectInitSlot()
        {
            print("call testConnectInitSlot")
        }
        function testConnectSlot()
        {
            print("call testConnectSlot")
        }
        function testConnectSlot2()
        {
            print("call testConnectSlot2")
        }
        Button
        {
            text:"test connect"
            x:250
            onClicked:
            {
                //绑定信号和槽函数
                //可以多次绑定同一个槽函数
                parent.testConnectSig.connect(parent.testConnectSlot)
                parent.testConnectSig.connect(parent.testConnectSlot)
                parent.testConnectSig.connect(parent.testConnectSlot2)
                parent.testConnectSig()
                //同一个槽函数只需要销毁一次
                print("-------------------------------")
                parent.testConnectSig.disconnect(parent.testConnectSlot)
                parent.testConnectSig()
                parent.testConnectSig.disconnect(parent.testConnectSlot2)
            }
        }
        //对象创建
        Component.onCompleted:
        {
            testConnectSig.connect(testConnectInitSlot)
        }
        //对象销毁
        Component.onDestruction:
        {
            testConnectSig.disconnect(testConnectInitSlot)
        }
    }
    Item
    {
        //测试多个类型(QML文件)之间的信号槽传递
        MyType
        {
            id:mytype
            y:100
        }
        Rectangle
        {
            id:rect
            width:200
            height:200
            color:"green"
            x:200
            y:100
            function recvSetSize(w,h)
            {
                print("recvSetSize " + w +":"+h)
                x+= w-width
                width = w;
                height = h
            }
        }
        Component.onCompleted:
        {
            mytype.setSize.connect(rect.recvSetSize)
        }
    }
    Connections
    {
        target:mytype
        function onSetSize(w,h)
        {
            print("Connections "+w+":"+h)
        }
    }
}
//MuType.qml
import QtQuick
import QtQuick.Controls
Item
{
    signal setSize(int w,int h)
    Rectangle
    {
        width:200
        height:200
        color:"blue"
        Button
        {
            text:"test connect signal"
            onClicked:
            {
                print("MyType Button onClicked")
                parent.width += 5
                parent.height += 5
                setSize(parent.width,parent.height)
            }
        }
    }
}


相关文章
|
移动开发 JavaScript Oracle
Oracle根据汉字取拼音首字母的function
Oracle根据汉字取拼音首字母的function
10355 0
|
4月前
|
数据采集 人工智能 自然语言处理
45条AI引用源实测:农产品品牌被AI搜索发现的3层机制
本文基于2026年5月豆包AI引擎45条实测引用源,揭示农产品品牌被AI发现的三大机制:平台地图、内容改写、信任资产。数据显示中文平台占71%,CSDN独占34%。文章提供90天可复现执行路径,聚焦机制与数据,不承诺投放效果。
230 1
|
3月前
|
人工智能 编解码 自然语言处理
阿里云Token Plan个人版详细介绍:支持文本、多模态模型,适配主流AI编程和智能体工具
阿里云百炼Token Plan个人版是面向个人开发者的AI大模型订阅服务,以Credits统一计量全模态模型与内置工具消耗,设Lite、Standard、Pro三档套餐,限时39元/月起,采用7天窗口限额机制,适配主流AI编程与智能体工具。中高档套餐附赠独立计量的Harness权益,配套夜间五折、弹性用量包与额度重置能力,可帮助开发者按需选型,以可控成本实现全链路AI开发。
|
5月前
|
自然语言处理 安全 数据处理
Claude Code删掉80%系统提示词,老金的长文解读,信息很密!
系统提示词越写越长,为什么效果反而可能变差?这篇用Claude Code删掉80%的案例,讲清哪些规则该删、该搬。
|
11月前
|
关系型数据库 MySQL BI
Quick BI使用案例09:柱图中用颜色直观区分各区域订单数量区间
本文以区域订单数智能分段打标为例,通过lod_fixed函数创建计算字段,实现柱图按数量区间(如“1500以下”“1500–2500”)自动着色,直观呈现数据分布,助力高效洞察。
|
安全 C++ Windows
window更新系统或备份系统时,错误代码0x80070002是什么意思?
0x80070002是Windows常见错误,提示“系统找不到指定文件或路径”。多由更新失败、文件缺失或路径错误引起。可通过重启更新服务、修复系统文件(sfc/DISM)、检查快捷方式目标或使用DirectX修复工具解决。
1424 9
|
SQL 运维 关系型数据库
com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Out of range value for column 'balance' at row 1
本文通过实际案例解析MySQL中余额字段因超出decimal(10,2)范围触发的数据截断错误,分析报错原因、复现问题并提出字段扩容至decimal(20,2)的解决方案,总结了金融系统中数值字段设计不当带来的风险及预防措施。
|
8月前
|
供应链 JavaScript 数据可视化
工厂版ERP系统源码,企业级
工厂版ERP源码系统,基于SpringBoot+Vue开发,集成生产、订单、采购、库存、财务等模块。支持多工厂、条码扫码、MRP分析、柔性排产与质量追溯,实现精细化、可视化管理。
357 0
|
11月前
|
SQL 人工智能 分布式计算
轻松体验MaxCompute SQL AI:用SQL调用大模型
本报告通过三步实践演示如何用SQL调用大模型:领取MaxCompute资源、创建项目与测试表、使用AI_TEXT_COMPLETE函数实现文本摘要和情感分析。全程无需复杂代码,仅用标准SQL即可在数据环境中直接调用AI能力,显著降低应用门槛,提升分析效率,验证了SQL+AI融合处理文本的可行性。
336 8
|
11月前
|
人工智能 自然语言处理 运维
业内首发泛娱乐底座大模型!元象开源XVERSE-Ent中英双模型,单卡部署超低门槛
元象开源首款聚焦泛娱乐场景的大模型XVERSE-Ent,含中英双版本,专精角色一致性、长剧情理解与多元语境适配,支持轻量化部署,助力开发者低成本打造AI社交、游戏与创意内容应用。
1007 3