Qt xml学习之calculator-qml

1.功能说明:制作简易计算器

2.使用技术:qml,scxml

3.项目效果:

4.qml部分:

cpp 复制代码
import Calculator 1.0  //需要引用对应类的队友版本
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 1.4
import QtScxml 5.8  // 引入QScxmlStateMachine

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Calculator{
        id:calculator
        running: true
        EventConnection{
            events: ["Display"] //触发事件名,可自定义
            onOccurred: name.text = event.data.display_res //event是当前触发事件
        }
    }

    Column{
        spacing:10
        //结果区
        Item {
            id: resultZone
            width: 640
            height: 30
            Rectangle{
                color: "#00ff55"
                anchors.fill: parent
                Text {
                    id: name
                    text: qsTr("text")
                    color: "#000000"
                    font.bold:true
                    font.pixelSize:24
                }
            }
        }
        //数字区
        Item{
            width: 640
            height: 320
            Grid{
                columns:3
                Repeater{
                    model: ["1","2","3","4","5","6","7","8","9"]
                    Button{
                        text:modelData
                        height:100
                        onClicked: calculator.submitEvent(eventname)//submitEvent ,QScxmlStateMachine 方法
                        property string eventname: {
                            return "DIGIT." + text
                        }
                    }
                }
            }
        }
        //操作区
        Item{
            id:oper
            width: 640
            height: 50
            Row{
                Repeater{
                    model:["+", "-", "*", "/"]
                    Button{
                        text:modelData;
                        width:120
                        height:40
                        onClicked: calculator.submitEvent(eventname);
                        property string eventname: {
                            switch(text){
                            case "+" :return "OPER.PLUS"
                            case "-" :return "OPER.MINUS"
                            case "*" :return "OPER.STAR"
                            case "/" :return "OPER.DIV"
                            }
                        }
                    }
                }
            }
        }
        //计算'='
        Button{
            text:"="
            onClicked: calculator.submitEvent("EQUALS")
        }
    }

}

5.scxml部分

帮助文档:https://www.w3.org/TR/scxml/







cpp 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" binding="early" xmlns:qt="http://www.qt.io/2015/02/scxml-ext" name="Calculator" qt:editorversion="4.12.2" datamodel="ecmascript" initial="work">
    <qt:editorinfo initialGeometry="48.45;-3.09;-20;-20;40;40"/>
    <state id="work">
        <qt:editorinfo scenegeometry="327.40;189.57;-30.23;54;456.33;372.10" removedInitial="Parallel_1" geometry="327.40;189.57;-357.63;-135.57;456.33;372.10"/>
        <transition type="internal" event="UPDATE_DISPLAY">
            <send event="Display">
                <param name="display_res" expr="short_res==''?res:short_res"/>
            </send>
            <log label="'log'" expr="res"/>
        </transition>
        <state id="Ready">
            <qt:editorinfo scenegeometry="172.79;135.84;112.79;85.84;146.57;100" geometry="-106.05;-23.24;-60;-50;146.57;100"/>
            <onentry>
                <assign location="res" expr="0"/>
                <send event="UPDATE_DISPLAY"/>
                <log label="'in ready'" expr="0"/>
                <assign location="short_res" expr="''"/>
            </onentry>
            <transition type="internal" event="DIGIT" target="init_input">
                <qt:editorinfo movePoint="-30.71;1.54"/>
                <assign location="short_res" expr="''"/>
            </transition>
        </state>
        <state id="init_input">
            <qt:editorinfo scenegeometry="209.21;274.28;93.21;224.28;176;146" geometry="-69.63;115.20;-116;-50;176;146"/>
            <onentry>
                <assign location="short_res" expr="short_res+_event.name.substr(_event.name.lastIndexOf('.')+1)"/>
                <send event="UPDATE_DISPLAY"/>
            </onentry>
            <transition type="internal" event="DIGIT">
                <assign expr="short_res+_event.name.substr(_event.name.lastIndexOf('.')+1)" location="short_res"/>
                <send event="UPDATE_DISPLAY"/>
            </transition>
            <transition type="internal" event="OPER">
                <if cond="'PLUS'==_event.name.substr(_event.name.lastIndexOf('.')+1)">
                    <assign location="short_res" expr="short_res+'+'"/>
                    <elseif cond="'MINUS'==_event.name.substr(_event.name.lastIndexOf('.')+1)"/>
                    <assign expr="short_res+'-'" location="short_res"/>
                    <elseif cond="'STAR'==_event.name.substr(_event.name.lastIndexOf('.')+1)"/>
                    <assign location="short_res" expr="short_res+'*'"/>
                    <elseif cond="'DIV'==_event.name.substr(_event.name.lastIndexOf('.')+1)"/>
                    <assign location="short_res" expr="short_res+'/'"/>
                </if>
                <send event="UPDATE_DISPLAY"/>
            </transition>
            <transition type="internal" event="EQUALS">
                <assign expr="eval(short_res)" location="short_res"/>
                <send event="UPDATE_DISPLAY"/>
            </transition>
        </state>
    </state>
    <datamodel>
        <data id="res"/>
        <data id="short_res"/>
    </datamodel>
</scxml>
相关推荐
Fairy_sevenseven12 分钟前
【二十八】【QT开发应用】模拟WPS Tab
开发语言·qt·wps
蜡笔小新星20 分钟前
Python Kivy库学习路线
开发语言·网络·经验分享·python·学习
攸攸太上33 分钟前
JMeter学习
java·后端·学习·jmeter·微服务
Death20037 分钟前
Qt 中的 QListWidget、QTreeWidget 和 QTableWidget:简化的数据展示控件
c语言·开发语言·c++·qt·c#
niu_sama42 分钟前
基于muduo库函数实现protobuf协议的通信
开发语言·qt
不写八个44 分钟前
Qt教程(001):Qt概述与安装
开发语言·qt
Ljubim.te1 小时前
Linux基于CentOS学习【进程状态】【进程优先级】【调度与切换】【进程挂起】【进程饥饿】
linux·学习·centos
Death2001 小时前
Qt 3D、QtQuick、QtQuick 3D 和 QML 的关系
c语言·c++·qt·3d·c#
yngsqq1 小时前
031集——文本文件按空格分行——C#学习笔记
笔记·学习·c#
zengy52 小时前
Effective C++中文版学习记录(三)
数据结构·c++·学习·stl