跨平台C++开发工具Qt示例教程:如何开发计算器应用程序(上)

Qt(发音为“ cute”,而不是“ cu-tee”)是一个跨平台框架,通常用作图形工具包,它不仅创建CLI应用程序中非常有用。而且它也可以在三种主要的台式机操作系统以及移动操作系统(如Symbian,Nokia Belle,Meego Harmattan,MeeGo或BB10)以及嵌入式设备,Android(Necessitas)和iOS的端口上运行。现在我们为你提供了免费的试用版。赶快点击下载Qt最新试用版>>

点击获取更多文章教程

Qt quick示例-计算器

跨平台C++开发工具Qt示例教程:如何开发计算器应用程序(上)

Calqlatr演示了各种QML和Qt Quick功能,例如显示自定义组件以及使用动画在应用程序视图中移动组件。应用程序逻辑是用JavaScript实现的,外观是用QML实现的。

运行示例

要从Qt Creator运行示例,请打开“ welcome”模式,然后从“ example中选择example

显示自定义组件

在Calqlatr应用程序中,我们使用以下自定义类型,它们分别在单独的.qml文件中定义:

  • Button.qml
  • Display.qml
  • NumberPad.qml

为了使用自定义类型,我们在主要的QML文件calqlatr.qml中添加了一个import语句,该语句将导入名为content类型所在位置的文件夹:

import  "content"

然后,我们可以通过将组件类型添加到任何QML文件中来显示自定义组件。例如,我们在calqlatr.qml中使用NumberPad类型来创建计算器的数字键盘。我们将类型放置在Item QML类型中,该类型是Qt Quick中所有可视项的基本类型:

    Item {        {        id:  pad        width:  180        NumberPad { { id:  numPad;  y:  10;  anchors.horizontalCenter: horizontalCenter: parent.horizontalCenter }}    }}

此外,我们在类型中使用Button NumberPad类型来创建计算器按钮。Button.qml指定按钮的基本属性,我们可以为NumberPad.qml中的每个按钮实例修改按钮。对于数字按钮和分隔符按钮,我们还使用text在Button.qml中定义的属性别名来指定text属性。

对于操作员按钮,我们还使用属性别名指定另一种颜色(绿色)color,并将操作员属性设置为true。我们在执行计算的函数中使用operator属性。

我们将按钮放置在Grid QML类型中,以将它们放置在 格中:

Grid {    {    columns:  3    columnSpacing:  32    rowSpacing:  16    signal buttonPressed    Button { { text:  "7" }    }    Button { { text:  "8" }    }    Button { { text:  "9" }    }    Button { { text:  "4" }    }    Button { { text:  "5" }    }    Button { { text:  "6" }    }    Button { { text:  "1" }    }    Button { { text:  "2" }    }    Button { { text:  "3" }    }    Button { { text:  "0" }    }    Button { { text:  ".";  dimmable:  true }    }    Button { { text:  " " }    }    Button { { text:  "±";  color:  "#6da43d";  operator:  true;  dimmable:  true }    }    Button { { text:  ";  color:  "#6da43d";  operator:  true;  dimmable:  true }    }    Button { { text:  "+";  color:  "#6da43d";  operator:  true;  dimmable:  true }    }    Button { { text:  "√";  color:  "#6da43d";  operator:  true;  dimmable:  true }    }    Button { { text:  "÷";  color:  "#6da43d";  operator:  true;  dimmable:  true }    }    Button { { text:  "×";  color:  "#6da43d";  operator:  true;  dimmable:  true }    }    Button { { text:  "C";  color:  "#6da43d";  operator:  true }    }    Button { { text:  " ";  color:  "#6da43d";  operator:  true }    }    Button { { text:  "=";  color:  "#6da43d";  operator:  true;  dimmable:  true }}}}

一些按钮也具有dimmable属性集,这意味着只要计算器引擎不接受该按钮的输入,就可以在视觉上禁用(变暗)它们。例如,平方根运算符的按钮显示为负值。

未完待续……下篇文章中我们将讲解计算器开发中动画组件的使用。

本篇文章中的内容你都学会了吗果这篇文章没能满足你的需求、点击获取更多文章教程!现在立刻下载Qt免费试用吧!更多Qt类开发工具QtitanRibbonQtitanChartQtitanNavigationQtitanDockingQtitanDataGrid在线订购现直降1000元,欢迎咨询在线客服获取更多优惠>>

跨平台C++开发工具Qt示例教程:如何开发计算器应用程序(上)
标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年8月12日
下一篇 2020年8月12日

相关推荐

发表回复

登录后才能评论