当前位置: 首页 > news >正文

【模型评测】主流编程大模型QML编程横向对比

主流编程大模型QML编程横向对比

今天晚上闲来无事,拉了一堆排行榜上的模型做了一个小测试,本次测试一共邀请了下面的这些模型进行同台竞技

Claude sonnet 4,Claude sonnet 4.5,Gemini2.5Pro,GPT5Codex,GPT5Mini,Qwen3Max,KimiK2

所有的都是一次提问,提示词都是使用QML创建一个计算器

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
Kimi2 生成了很多东西,但是Bug也一大堆,我手动修复了一下发现界面其实也还可以,不过没有鼠标悬浮的动效,但是也有键盘快捷键
在这里插入图片描述

额外内容

虽然 Claude Sonnet 4.5 看起来比 Claude Sonnet 4 美观性上差了一点,但是我点进代码之后发现实际上非常惊人,Sonnet 4 一共有 510 行,而 Sonnet 4.5 只有 181 行,Sonnet 4.5 在编程的时候真的会用到 QML 的特性让我非常震惊,他定义了一个自定义的组件CalcButton,以此大幅减少了代码量,而其他的模型,包括 Qwen3, Kimi K2, GLM 4.6, Sonnet 4都还是在用 Button,所以代码量飙升

总结

虽然 GLM 4.6 号称是对其了 Claude sonnet 4,但是在 QML 领域可能是缺乏训练数据,实际效果很明显比不上 Claude sonnet 4,除此之外大部分模型的功能都能正常实现,但是界面比较美观的只有 Claude 家的模型和 Kimi K2,不过 Kimi K2 功能有问题,没法运行。

期待未来国产模型越来越强,也期待年底的 GLM 5 能够达到或者超越 Sonnet 4

生成代码一览

Sonnet 4.5

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15ApplicationWindow {visible: truewidth: 320height: 480title: "计算器"property string displayText: "0"property real firstNumber: 0property string currentOperator: ""property bool startNewNumber: trueRectangle {anchors.fill: parentcolor: "#f0f0f0"ColumnLayout {anchors.fill: parentanchors.margins: 10spacing: 10// 显示屏Rectangle {Layout.fillWidth: trueLayout.preferredHeight: 80color: "white"border.color: "#cccccc"border.width: 2radius: 5Text {anchors.fill: parentanchors.margins: 10text: displayTextfont.pixelSize: 32horizontalAlignment: Text.AlignRightverticalAlignment: Text.AlignVCenterelide: Text.ElideLeft}}// 按钮网格GridLayout {Layout.fillWidth: trueLayout.fillHeight: truecolumns: 4rowSpacing: 8columnSpacing: 8// 第一行:C, ÷, ×, -CalcButton { text: "C"; color: "#ff9999"; onClicked: clear() }CalcButton { text: "÷"; color: "#ffd699"; onClicked: operatorClicked("÷") }CalcButton { text: "×"; color: "#ffd699"; onClicked: operatorClicked("×") }CalcButton { text: "-"; color: "#ffd699"; onClicked: operatorClicked("-") }// 第二行:7, 8, 9, +CalcButton { text: "7"; onClicked: numberClicked("7") }CalcButton { text: "8"; onClicked: numberClicked("8") }CalcButton { text: "9"; onClicked: numberClicked("9") }CalcButton { text: "+"; color: "#ffd699"; Layout.rowSpan: 2; onClicked: operatorClicked("+") }// 第三行:4, 5, 6CalcButton { text: "4"; onClicked: numberClicked("4") }CalcButton { text: "5"; onClicked: numberClicked("5") }CalcButton { text: "6"; onClicked: numberClicked("6") }// 第四行:1, 2, 3, =CalcButton { text: "1"; onClicked: numberClicked("1") }CalcButton { text: "2"; onClicked: numberClicked("2") }CalcButton { text: "3"; onClicked: numberClicked("3") }CalcButton { text: "="; color: "#99ccff"; Layout.rowSpan: 2; onClicked: calculate() }// 第五行:0, .CalcButton { text: "0"; Layout.columnSpan: 2; onClicked: numberClicked("0") }CalcButton { text: "."; onClicked: numberClicked(".") }}}}// 数字按钮点击处理function numberClicked(num) {if (startNewNumber) {if (num === ".") {displayText = "0."} else {displayText = num}startNewNumber = false} else {if (num === "." && displayText.indexOf(".") !== -1) {return // 已有小数点,不再添加}displayText = displayText === "0" && num !== "." ? num : displayText + num}}// 运算符按钮点击处理function operatorClicked(op) {if (currentOperator !== "" && !startNewNumber) {calculate()}firstNumber = parseFloat(displayText)currentOperator = opstartNewNumber = true}// 计算结果function calculate() {if (currentOperator === "") returnvar secondNumber = parseFloat(displayText)var result = 0switch (currentOperator) {case "+":result = firstNumber + secondNumberbreakcase "-":result = firstNumber - secondNumberbreakcase "×":result = firstNumber * secondNumberbreakcase "÷":if (secondNumber === 0) {displayText = "错误"clear()return}result = firstNumber / secondNumberbreak}// 格式化结果(去除不必要的小数位)displayText = result.toString()if (displayText.indexOf(".") !== -1) {var parts = displayText.split(".")if (parts[1].length > 8) {displayText = result.toFixed(8).replace(/\.?0+$/, "")}}currentOperator = ""startNewNumber = true}// 清除function clear() {displayText = "0"firstNumber = 0currentOperator = ""startNewNumber = true}// 自定义按钮组件component CalcButton: Button {property color color: "#e0e0e0"Layout.fillWidth: trueLayout.fillHeight: truebackground: Rectangle {color: parent.pressed ? Qt.darker(parent.color, 1.2) : parent.colorradius: 5border.color: "#999999"border.width: 1}contentItem: Text {text: parent.textfont.pixelSize: 24font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCentercolor: "#333333"}}
}

Sonnet 4

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15ApplicationWindow {id: windowvisible: truewidth: 320height: 500title: "计算器"property string currentNumber: "0"property string previousNumber: ""property string operator: ""property bool waitingForOperand: falseRectangle {anchors.fill: parentcolor: "#2b2b2b"Column {anchors.fill: parentanchors.margins: 10spacing: 10// 显示屏Rectangle {width: parent.widthheight: 80color: "#1a1a1a"border.color: "#404040"border.width: 1radius: 8Text {id: displayanchors.right: parent.rightanchors.verticalCenter: parent.verticalCenteranchors.rightMargin: 15text: currentNumbercolor: "white"font.pixelSize: 28font.bold: true}}// 按钮网格GridLayout {width: parent.widthheight: parent.height - 100columns: 4rowSpacing: 8columnSpacing: 8// 第一行:C, ±, %, ÷Button {Layout.fillWidth: trueLayout.fillHeight: truetext: "C"onClicked: clear()background: Rectangle {color: parent.pressed ? "#666666" : "#808080"radius: 8}contentItem: Text {text: parent.textcolor: "black"font.pixelSize: 20font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}Button {Layout.fillWidth: trueLayout.fillHeight: truetext: "±"onClicked: toggleSign()background: Rectangle {color: parent.pressed ? "#666666" : "#808080"radius: 8}contentItem: Text {text: parent.textcolor: "black"font.pixelSize: 20font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}Button {Layout.fillWidth: trueLayout.fillHeight: truetext: "%"onClicked: percentage()background: Rectangle {color: parent.pressed ? "#666666" : "#808080"radius: 8}contentItem: Text {text: parent.textcolor: "black"font.pixelSize: 20font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}Button {Layout.fillWidth: trueLayout.fillHeight: truetext: "÷"onClicked: setOperator("/")background: Rectangle {color: parent.pressed ? "#cc6600" : "#ff9500"radius: 8}contentItem: Text {text: parent.textcolor: "white"font.pixelSize: 24font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}// 第二行:7, 8, 9, ×Button {Layout.fillWidth: trueLayout.fillHeight: truetext: "7"onClicked: inputNumber("7")background: Rectangle {color: parent.pressed ? "#666666" : "#404040"radius: 8}contentItem: Text {text: parent.textcolor: "white"font.pixelSize: 24font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}Button {Layout.fillWidth: trueLayout.fillHeight: truetext: "8"onClicked: inputNumber("8")background: Rectangle {color: parent.pressed ? "#666666" : "#404040"radius: 8}contentItem: Text {text: parent.textcolor: "white"font.pixelSize: 24font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}Button {Layout.fillWidth: trueLayout.fillHeight: truetext: "9"onClicked: inputNumber("9")background: Rectangle {color: parent.pressed ? "#666666" : "#404040"radius: 8}contentItem: Text {text: parent.textcolor: "white"font.pixelSize: 24font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}Button {Layout.fillWidth: trueLayout.fillHeight: truetext: "×"onClicked: setOperator("*")background: Rectangle {color: parent.pressed ? "#cc6600" : "#ff9500"radius: 8}contentItem: Text {text: parent.textcolor: "white"font.pixelSize: 24font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}// 第三行:4, 5, 6, -Button {Layout.fillWidth: trueLayout.fillHeight: truetext: "4"onClicked: inputNumber("4")background: Rectangle {color: parent.pressed ? "#666666" : "#404040"radius: 8}contentItem: Text {text: parent.textcolor: "white"font.pixelSize: 24font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}Button {Layout.fillWidth: trueLayout.fillHeight: truetext: "5"onClicked: inputNumber("5")background: Rectangle {color: parent.pressed ? "#666666" : "#404040"radius: 8}contentItem: Text {text: parent.textcolor: "white"font.pixelSize: 24font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}Button {Layout.fillWidth: trueLayout.fillHeight: truetext: "6"onClicked: inputNumber("6")background: Rectangle {color: parent.pressed ? "#666666" : "#404040"radius: 8}contentItem: Text {text: parent.textcolor: "white"font.pixelSize: 24font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}Button {Layout.fillWidth: trueLayout.fillHeight: truetext: "-"onClicked: setOperator("-")background: Rectangle {color: parent.pressed ? "#cc6600" : "#ff9500"radius: 8}contentItem: Text {text: parent.textcolor: "white"font.pixelSize: 28font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}// 第四行:1, 2, 3, +Button {Layout.fillWidth: trueLayout.fillHeight: truetext: "1"onClicked: inputNumber("1")background: Rectangle {color: parent.pressed ? "#666666" : "#404040"radius: 8}contentItem: Text {text: parent.textcolor: "white"font.pixelSize: 24font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}Button {Layout.fillWidth: trueLayout.fillHeight: truetext: "2"onClicked: inputNumber("2")background: Rectangle {color: parent.pressed ? "#666666" : "#404040"radius: 8}contentItem: Text {text: parent.textcolor: "white"font.pixelSize: 24font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}Button {Layout.fillWidth: trueLayout.fillHeight: truetext: "3"onClicked: inputNumber("3")background: Rectangle {color: parent.pressed ? "#666666" : "#404040"radius: 8}contentItem: Text {text: parent.textcolor: "white"font.pixelSize: 24font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}Button {Layout.fillWidth: trueLayout.fillHeight: truetext: "+"onClicked: setOperator("+")background: Rectangle {color: parent.pressed ? "#cc6600" : "#ff9500"radius: 8}contentItem: Text {text: parent.textcolor: "white"font.pixelSize: 24font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}// 第五行:0 (占两列), ., =Button {Layout.fillWidth: trueLayout.fillHeight: trueLayout.columnSpan: 2text: "0"onClicked: inputNumber("0")background: Rectangle {color: parent.pressed ? "#666666" : "#404040"radius: 8}contentItem: Text {text: parent.textcolor: "white"font.pixelSize: 24font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}Button {Layout.fillWidth: trueLayout.fillHeight: truetext: "."onClicked: inputDecimal()background: Rectangle {color: parent.pressed ? "#666666" : "#404040"radius: 8}contentItem: Text {text: parent.textcolor: "white"font.pixelSize: 24font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}Button {Layout.fillWidth: trueLayout.fillHeight: truetext: "="onClicked: calculate()background: Rectangle {color: parent.pressed ? "#cc6600" : "#ff9500"radius: 8}contentItem: Text {text: parent.textcolor: "white"font.pixelSize: 24font.bold: truehorizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenter}}}}}// 计算器功能函数function inputNumber(num) {if (waitingForOperand) {currentNumber = numwaitingForOperand = false} else {currentNumber = currentNumber === "0" ? num : currentNumber + num}}function inputDecimal() {if (waitingForOperand) {currentNumber = "0."waitingForOperand = false} else if (currentNumber.indexOf(".") === -1) {currentNumber += "."}}function clear() {currentNumber = "0"previousNumber = ""operator = ""waitingForOperand = false}function toggleSign() {if (currentNumber !== "0") {currentNumber = currentNumber.charAt(0) === "-" ? currentNumber.slice(1) : "-" + currentNumber}}function percentage() {currentNumber = (parseFloat(currentNumber) / 100).toString()}function setOperator(nextOperator) {var inputValue = parseFloat(currentNumber)if (previousNumber === "") {previousNumber = currentNumber} else if (operator !== "") {var prevValue = parseFloat(previousNumber)var result = performCalculation(prevValue, inputValue, operator)currentNumber = result.toString()previousNumber = currentNumber}waitingForOperand = trueoperator = nextOperator}function calculate() {var inputValue = parseFloat(currentNumber)if (previousNumber !== "" && operator !== "") {var prevValue = parseFloat(previousNumber)var result = performCalculation(prevValue, inputValue, operator)currentNumber = result.toString()previousNumber = ""operator = ""waitingForOperand = true}}function performCalculation(firstOperand, secondOperand, operator) {switch (operator) {case "+":return firstOperand + secondOperandcase "-":return firstOperand - secondOperandcase "*":return firstOperand * secondOperandcase "/":if (secondOperand === 0) {return "错误"}return firstOperand / secondOperanddefault:return secondOperand}}
}

Gemini 2.5 Pro

import QtQuick
import QtQuick.Controls
import QtQuick.LayoutsWindow {id: rootwidth: 320height: 480visible: truetitle: qsTr("Calculator")color: "#f0f0f0"// Function to perform calculationfunction calculate() {try {// A simple validation to prevent unsafe eval, not exhaustiveif (/^[0-9+\-*/.() ]+$/.test(display.text)) {var result = eval(display.text);display.text = String(result);} else {display.text = "Error";}} catch (e) {display.text = "Error";}}ColumnLayout {anchors.fill: parentanchors.margins: 10spacing: 5// DisplayText {id: displayLayout.fillWidth: trueLayout.preferredHeight: 80text: "0"font.pixelSize: 48horizontalAlignment: Text.AlignRightverticalAlignment: Text.AlignVCenterbackground: Rectangle {color: "white"border.color: "gray"radius: 5}clip: true}// ButtonsGridLayout {Layout.fillWidth: trueLayout.fillHeight: truecolumns: 4readonly property var buttons: ["C", "(", ")", "/","7", "8", "9", "*","4", "5", "6", "-","1", "2", "3", "+","0", ".", "=", ""]Repeater {model: parent.buttonsdelegate: Button {text: modelDatafont.pixelSize: 24Layout.fillWidth: trueLayout.fillHeight: truevisible: text !== ""background: Rectangle {color: parent.down ? "#d0d0d0" : "#e0e0e0"border.color: "gray"radius: 5}onClicked: {if (text === "C") {display.text = "0";} else if (text === "=") {calculate();} else {if (display.text === "0" || display.text === "Error") {display.text = text;} else {display.text += text;}}}// Special styling for operator buttonsComponent.onCompleted: {if ("/*-+".includes(text)) {background.color = "#f0ad4e";}if (text === "=") {background.color = "#5cb85c";}if (text === "C") {background.color = "#d9534f";}}}}}}
}

GLM 4.6

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15ApplicationWindow {id: rootvisible: truewidth: 300height: 400title: qsTr("QML 计算器")// =================================================================// 1. 状态和逻辑 (之前在 CalculatorLogic.qml 中的内容)// =================================================================// --- 状态属性 ---// 显示在屏幕上的文本property string displayText: "0"// 存储前一个操作数property string previousValue: ""// 存储当前操作符 (+, -, *, /)property string currentOperator: ""// 标记是否正在等待输入新的操作数property bool waitingForOperand: false// --- 逻辑函数 ---// 处理数字按钮 (0-9)function digitPressed(digit) {if (waitingForOperand) {displayText = digit;waitingForOperand = false;} else {displayText = displayText === "0" ? digit : displayText + digit;}}// 处理操作符按钮 (+, -, *, /)function operatorPressed(op) {const currentValue = parseFloat(displayText);if (previousValue !== "" && currentOperator !== "" && !waitingForOperand) {// 如果已经有操作符和前一个值,先进行计算calculate();}previousValue = displayText;currentOperator = op;waitingForOperand = true;}// 处理等号按钮 (=)function equalsPressed() {if (currentOperator === "") return; // 没有操作符,不计算calculate();currentOperator = ""; // 计算后清除操作符}// 处理清除按钮 (C)function clearPressed() {displayText = "0";previousValue = "";currentOperator = "";waitingForOperand = false;}// 处理小数点按钮 (.)function pointPressed() {if (waitingForOperand) {displayText = "0.";waitingForOperand = false;} else if (displayText.indexOf('.') === -1) {displayText += ".";}}// 处理正负号切换 (+/-)function signPressed() {displayText = (parseFloat(displayText) * -1).toString();}// --- 内部计算函数 ---function calculate() {const prev = parseFloat(previousValue);const current = parseFloat(displayText);let result = 0;switch (currentOperator) {case '+':result = prev + current;break;case '-':result = prev - current;break;case '*':result = prev * current;break;case '/':if (current === 0) {displayText = "Error";waitingForOperand = true;return;}result = prev / current;break;}// 处理浮点数精度问题,并转换为字符串displayText = Number.isInteger(result) ? result.toString() : result.toFixed(8).replace(/\.?0+$/, "");waitingForOperand = true;}// =================================================================// 2. 用户界面 (之前在 main.qml 中的内容)// =================================================================// 使用垂直布局来组织显示屏和按钮区域ColumnLayout {anchors.fill: parentanchors.margins: 10spacing: 10// 显示屏TextField {id: displayLayout.fillWidth: trueLayout.preferredHeight: 60// 直接绑定到根对象的 displayText 属性text: root.displayTexthorizontalAlignment: TextInput.AlignRightfont.pixelSize: 30font.bold: truereadOnly: truebackground: Rectangle {color: "#f0f0f0"border.color: "#d0d0d0"}}// 按钮区域,使用网格布局GridLayout {id: gridLayout.fillWidth: trueLayout.fillHeight: truecolumns: 4rowSpacing: 10columnSpacing: 10// 第一行Button { text: "C"; Layout.columnSpan: 2; font.pixelSize: 22; onClicked: root.clearPressed(); background: Rectangle { color: "#ff9500"; border.color: "#e68500"; radius: 5; } }Button { text: "+/-"; font.pixelSize: 22; onClicked: root.signPressed(); background: Rectangle { color: "#f5f5f5"; border.color: "#b0b0b0"; radius: 5; } }Button { text: "/"; font.pixelSize: 22; onClicked: root.operatorPressed(text); background: Rectangle { color: "#ff9500"; border.color: "#e68500"; radius: 5; } }// 第二行Button { text: "7"; font.pixelSize: 22; onClicked: root.digitPressed(text); background: Rectangle { color: "#f5f5f5"; border.color: "#b0b0b0"; radius: 5; } }Button { text: "8"; font.pixelSize: 22; onClicked: root.digitPressed(text); background: Rectangle { color: "#f5f5f5"; border.color: "#b0b0b0"; radius: 5; } }Button { text: "9"; font.pixelSize: 22; onClicked: root.digitPressed(text); background: Rectangle { color: "#f5f5f5"; border.color: "#b0b0b0"; radius: 5; } }Button { text: "*"; font.pixelSize: 22; onClicked: root.operatorPressed(text); background: Rectangle { color: "#ff9500"; border.color: "#e68500"; radius: 5; } }// 第三行Button { text: "4"; font.pixelSize: 22; onClicked: root.digitPressed(text); background: Rectangle { color: "#f5f5f5"; border.color: "#b0b0b0"; radius: 5; } }Button { text: "5"; font.pixelSize: 22; onClicked: root.digitPressed(text); background: Rectangle { color: "#f5f5f5"; border.color: "#b0b0b0"; radius: 5; } }Button { text: "6"; font.pixelSize: 22; onClicked: root.digitPressed(text); background: Rectangle { color: "#f5f5f5"; border.color: "#b0b0b0"; radius: 5; } }Button { text: "-"; font.pixelSize: 22; onClicked: root.operatorPressed(text); background: Rectangle { color: "#ff9500"; border.color: "#e68500"; radius: 5; } }// 第四行Button { text: "1"; font.pixelSize: 22; onClicked: root.digitPressed(text); background: Rectangle { color: "#f5f5f5"; border.color: "#b0b0b0"; radius: 5; } }Button { text: "2"; font.pixelSize: 22; onClicked: root.digitPressed(text); background: Rectangle { color: "#f5f5f5"; border.color: "#b0b0b0"; radius: 5; } }Button { text: "3"; font.pixelSize: 22; onClicked: root.digitPressed(text); background: Rectangle { color: "#f5f5f5"; border.color: "#b0b0b0"; radius: 5; } }Button { text: "+"; font.pixelSize: 22; onClicked: root.operatorPressed(text); background: Rectangle { color: "#ff9500"; border.color: "#e68500"; radius: 5; } }// 第五行Button { text: "0"; Layout.columnSpan: 2; font.pixelSize: 22; onClicked: root.digitPressed(text); background: Rectangle { color: "#f5f5f5"; border.color: "#b0b0b0"; radius: 5; } }Button { text: "."; font.pixelSize: 22; onClicked: root.pointPressed(); background: Rectangle { color: "#f5f5f5"; border.color: "#b0b0b0"; radius: 5; } }Button { text: "="; font.pixelSize: 22; onClicked: root.equalsPressed(); background: Rectangle { color: "#ff9500"; border.color: "#e68500"; radius: 5; } }}}
}

GPT5 Codex

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15ApplicationWindow {width: 320height: 480visible: truetitle: qsTr("Calculator")property string displayText: "0"property double previousValue: 0property string pendingOperator: ""property bool resetDisplay: falsefunction resetError() {if (displayText === "Error")displayText = "0"}function handleNumber(digit) {resetError()if (resetDisplay || displayText === "0") {displayText = digit} else {displayText += digit}resetDisplay = false}function handleDecimal() {resetError()if (resetDisplay) {displayText = "0."resetDisplay = falsereturn}if (displayText.indexOf(".") === -1)displayText += "."}function handleToggleSign() {resetError()if (displayText === "0")returnif (displayText.charAt(0) === "-")displayText = displayText.slice(1)elsedisplayText = "-" + displayText}function handleBackspace() {resetError()if (resetDisplay) {displayText = "0"resetDisplay = falsereturn}if (displayText.length <= 1 || (displayText.length === 2 && displayText.startsWith("-"))) {displayText = "0"} else {displayText = displayText.slice(0, -1)}}function applyOperator(op) {resetError()var current = parseFloat(displayText)if (pendingOperator !== "")executePending(current)elsepreviousValue = currentpendingOperator = opresetDisplay = true}function executePending(current) {switch (pendingOperator) {case "+":previousValue += currentbreakcase "-":previousValue -= currentbreakcase "×":previousValue *= currentbreakcase "÷":if (current === 0) {displayText = "Error"previousValue = 0pendingOperator = ""resetDisplay = truereturn}previousValue /= currentbreakdefault:previousValue = current}displayText = String(previousValue)}function handleEquals() {if (pendingOperator === "" || displayText === "Error")returnexecutePending(parseFloat(displayText))pendingOperator = ""resetDisplay = true}function handleClear() {displayText = "0"previousValue = 0pendingOperator = ""resetDisplay = false}ColumnLayout {anchors.fill: parentanchors.margins: 16spacing: 12Rectangle {Layout.fillWidth: trueLayout.preferredHeight: 80color: "#202020"radius: 6Text {anchors.fill: parentanchors.margins: 12horizontalAlignment: Text.AlignRightverticalAlignment: Text.AlignVCenterfont.pixelSize: 36color: "#ffffff"text: displayText}}ColumnLayout {Layout.fillWidth: trueLayout.fillHeight: truespacing: 8RowLayout {Layout.fillWidth: truespacing: 8Button { text: "C"; Layout.fillWidth: true; onClicked: handleClear() }Button { text: "÷"; Layout.fillWidth: true; onClicked: applyOperator("÷") }Button { text: "×"; Layout.fillWidth: true; onClicked: applyOperator("×") }Button { text: "←"; Layout.fillWidth: true; onClicked: handleBackspace() }}RowLayout {Layout.fillWidth: truespacing: 8Button { text: "7"; Layout.fillWidth: true; onClicked: handleNumber("7") }Button { text: "8"; Layout.fillWidth: true; onClicked: handleNumber("8") }Button { text: "9"; Layout.fillWidth: true; onClicked: handleNumber("9") }Button { text: "-"; Layout.fillWidth: true; onClicked: applyOperator("-") }}RowLayout {Layout.fillWidth: truespacing: 8Button { text: "4"; Layout.fillWidth: true; onClicked: handleNumber("4") }Button { text: "5"; Layout.fillWidth: true; onClicked: handleNumber("5") }Button { text: "6"; Layout.fillWidth: true; onClicked: handleNumber("6") }Button { text: "+"; Layout.fillWidth: true; onClicked: applyOperator("+") }}RowLayout {Layout.fillWidth: truespacing: 8Button { text: "1"; Layout.fillWidth: true; onClicked: handleNumber("1") }Button { text: "2"; Layout.fillWidth: true; onClicked: handleNumber("2") }Button { text: "3"; Layout.fillWidth: true; onClicked: handleNumber("3") }Button { text: "="; Layout.fillWidth: true; onClicked: handleEquals() }}RowLayout {Layout.fillWidth: truespacing: 8Button { text: "+/-"; Layout.fillWidth: true; onClicked: handleToggleSign() }Button { text: "0"; Layout.fillWidth: true; onClicked: handleNumber("0") }Button { text: "."; Layout.fillWidth: true; onClicked: handleDecimal() }Button { text: "="; Layout.fillWidth: true; onClicked: handleEquals() }}}}
}

GPT5 mini

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15ApplicationWindow {id: winwidth: 360height: 520visible: truetitle: qsTr("计算器 GPT5Mini")property string expr: ""property string displayText: expr === "" ? "0" : exprColumnLayout {anchors.fill: parentanchors.margins: 12spacing: 12Rectangle {id: displayBgLayout.fillWidth: trueheight: 100color: "#222"radius: 8Text {id: displayanchors.right: parent.rightanchors.rightMargin: 12anchors.verticalCenter: parent.verticalCentertext: win.displayTextfont.pixelSize: 32color: "#fff"horizontalAlignment: Text.AlignRightverticalAlignment: Text.AlignVCenterelide: Text.ElideLeft}}// 按钮区GridLayout {id: gridLayout.fillWidth: trueLayout.fillHeight: truecolumns: 4rowSpacing: 8columnSpacing: 8// 按钮标签顺序property var keys: [{ label: "C", value: "C" }, { label: "⌫", value: "⌫" }, { label: "/", value: "/" }, { label: "*", value: "*" },{ label: "7", value: "7" }, { label: "8", value: "8" }, { label: "9", value: "9" }, { label: "-", value: "-" },{ label: "4", value: "4" }, { label: "5", value: "5" }, { label: "6", value: "6" }, { label: "+", value: "+" },{ label: "1", value: "1" }, { label: "2", value: "2" }, { label: "3", value: "3" }, { label: "=", value: "=" },{ label: "0", value: "0", columnSpan: 2 }, { label: ".", value: "." }, { label: "=", value: "=" }]Repeater {model: keys.lengthButton {Layout.preferredHeight: 60Layout.preferredWidth: ((grid.width - (grid.columnSpacing * (grid.columns - 1))) / grid.columns) * Layout.columnSpantext: keys[index].label// 处理 0 占两列的视觉效果Layout.columnSpan: keys[index].columnSpan || 1onClicked: {var key = keys[index].value;if (key === "C") {win.clearAll();} else if (key === "⌫") {win.backspace();} else if (key === "=") {win.compute();} else {win.append(key);}}}}}}function isOperator(ch) {return ch === "+" || ch === "-" || ch === "*" || ch === "/";}function append(text) {// 数字或点或运算符处理if (text === ".") {// 防止在当前数字中重复小数点var lastOp = -1;for (var i = expr.length - 1; i >= 0; --i) {if (isOperator(expr[i])) { lastOp = i; break; }}var current = expr.slice(lastOp + 1);if (current.indexOf(".") !== -1) return;if (current === "") expr += "0";expr += ".";} else if (isOperator(text)) {if (expr === "") {// 允许以负号开始if (text === "-") expr = "-";return;}var last = expr[expr.length - 1];if (isOperator(last)) {// 连续运算符则替换(允许 - 作为负号)if (text === "-" && last !== "-") {expr += "-";} else {expr = expr.slice(0, -1) + text;}} else {expr += text;}} else { // 数字expr += text;}displayText = expr === "" ? "0" : expr;}function backspace() {if (expr.length > 0) {expr = expr.slice(0, -1);displayText = expr === "" ? "0" : expr;}}function clearAll() {expr = "";displayText = "0";}function compute() {if (expr === "") return;// 避免末尾是运算符var last = expr[expr.length - 1];if (isOperator(last)) expr = expr.slice(0, -1);try {// 使用 Function 安全地计算表达式var result = Function('"use strict"; return (' + expr + ')')();if (result === Infinity || result === -Infinity || isNaN(result)) {displayText = "错误";expr = "";return;}// 格式化:去掉末尾多余的 0if (typeof result === "number") {// 保留最多 10 位小数,去掉尾部 0var fixed = parseFloat(result.toFixed(10)).toString();displayText = fixed;expr = fixed;} else {displayText = String(result);expr = displayText;}} catch (e) {displayText = "错误";expr = "";}}
}

Kimi K2

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15ApplicationWindow {id: rootvisible: truewidth: 320; height: 480minimumWidth: 280; minimumHeight: 420title: qsTr("QML 计算器")color: "#1e1e1e"property string expr: ""          // 当前表达式property bool justResult: false   // 刚算完结果,下次输入清空// 主布局ColumnLayout {anchors.fill: parentanchors.margins: 8spacing: 6// 显示屏Rectangle {Layout.fillWidth: trueLayout.preferredHeight: root.height * 0.28color: "#2d2d30"radius: 10border.color: "#444"border.width: 1Text {id: displayanchors {right: parent.right; rightMargin: 12verticalCenter: parent.verticalCenter}text: root.expr === "" ? "0" : root.exprfont.pixelSize: Math.max(28, root.width / 8)color: "#f0f0f0"}}// 按钮区GridLayout {Layout.fillWidth: trueLayout.fillHeight: truecolumns: 4rowSpacing: 6; columnSpacing: 6// 按钮数据:文字、角色、颜色readonly property var data: [// row 0{txt:"AC", role:"clear", bg:"#3c3c3c"},{txt:"+/-",role:"sign", bg:"#3c3c3c"},{txt:"%", role:"percent",bg:"#3c3c3c"},{txt:"÷", role:"op",    bg:"#ff9500"},// row 1{txt:"7", role:"num"},{txt:"8", role:"num"},{txt:"9", role:"num"},{txt:"×", role:"op",    bg:"#ff9500"},// row 2{txt:"4", role:"num"},{txt:"5", role:"num"},{txt:"6", role:"num"},{txt:"-", role:"op",    bg:"#ff9500"},// row 3{txt:"1", role:"num"},{txt:"2", role:"num"},{txt:"3", role:"num"},{txt:"+", role:"op",    bg:"#ff9500"},// row 4{txt:"0", role:"num", wide:true},{txt:".", role:"dot"},{txt:"=", role:"eq",    bg:"#ff9500"}]Repeater {model: parent.datadelegate: Rectangle {color: modelData.bg || "#505050"radius: 8Layout.fillHeight: trueLayout.fillWidth: trueLayout.columnSpan: modelData.wide ? 2 : 1border.width: 0.5border.color: Qt.darker(color, 1.2)Text {anchors.centerIn: parenttext: modelData.txtfont { pixelSize: root.width / 12; bold: true }color: "white"}MouseArea {anchors.fill: parentonClicked: handle(modelData)onPressed: parent.scale = 0.92onReleased: parent.scale = 1.0}}}}}// 统一按键分发function handle(btn) {const r = btn.role, t = btn.txtif (r === "num")   append(t)else if (r === "op") appendOp(t)else if (r === "dot") appendDot()else if (r === "eq")  calculate()else if (r === "clear") { expr = ""; justResult = false }else if (r === "sign")  toggleSign()else if (r === "percent") percent()}function append(ch) {if (justResult) { expr = ""; justResult = false }expr += ch}function appendOp(op) {if (expr === "") returnconst last = expr.slice(-1)if ("+-×÷".indexOf(last) !== -1) expr = expr.slice(0, -1)expr += opjustResult = false}function appendDot() {if (justResult) { expr = "0."; justResult = false; return }// 防止重复小数点const tokens = expr.split(/[\+\-\×÷]/)const cur = tokens[tokens.length - 1]if (cur.indexOf(".") === -1) expr += "."}function toggleSign() {if (expr === "") returnconst m = expr.match(/(.*)([\+\-\×÷])([^\+\-\×÷]*)$/)if (m) {const num = parseFloat(m[3])expr = m[1] + m[2] + (num * -1).toString()} else {expr = (parseFloat(expr) * -1).toString()}}function percent() {if (expr === "") returnconst last = parseFloat(expr.split(/[\+\-\×÷]/).pop())expr = expr.slice(0, -last.toString().length) + (last * 0.01).toString()}function calculate() {if (expr === "") return// 替换符号 → JS 可识别let js = expr.replace("×", "*").replace("÷", "/")try {const res = Number(eval(js).toFixed(8))  // 防浮点误差expr = res.toString()justResult = true} catch(e) {expr = "Error"justResult = true}}// 键盘支持Keys.onPressed: {const k = event.keyif (k >= Qt.Key_0 && k <= Qt.Key_9) append((k - Qt.Key_0).toString())else if (k === Qt.Key_Plus)  appendOp("+")else if (k === Qt.Key_Minus) appendOp("-")else if (k === Qt.Key_Asterisk || k === Qt.Key_multiply) appendOp("×")else if (k === Qt.Key_Slash) appendOp("÷")else if (k === Qt.Key_Period || k === Qt.Key_Comma) appendDot()else if (k === Qt.Key_Return || k === Qt.Key_Equal) calculate()else if (k === Qt.Key_Escape) { expr = ""; justResult = false }else if (k === Qt.Key_Backspace) {if (!justResult && expr.length) expr = expr.slice(0, -1)}}focus: true
}

Qwen3 Max

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15ApplicationWindow {id: windowvisible: truewidth: 300height: 450title: "QML Calculator"property string currentInput: "0"property real lastNumber: 0property string operator: ""property bool waitingForOperand: falsefunction clear() {currentInput = "0"lastNumber = 0operator = ""waitingForOperand = false}function inputDigit(digit) {if (waitingForOperand) {currentInput = "0"waitingForOperand = false}if (currentInput === "0") {currentInput = digit} else {currentInput += digit}}function inputOperator(op) {if (operator !== "") {calculate()} else {lastNumber = parseFloat(currentInput)}operator = opwaitingForOperand = true}function calculate() {if (operator === "") returnvar current = parseFloat(currentInput)var resultswitch (operator) {case "+":result = lastNumber + currentbreakcase "-":result = lastNumber - currentbreakcase "*":result = lastNumber * currentbreakcase "/":if (current === 0) {currentInput = "Error"operator = ""waitingForOperand = truereturn}result = lastNumber / currentbreakdefault:return}currentInput = (Math.round(result * 1e10) / 1e10).toString()operator = ""waitingForOperand = true}function backspace() {if (waitingForOperand || currentInput === "0" || currentInput === "Error") returncurrentInput = currentInput.slice(0, -1)if (currentInput === "" || currentInput === "-") {currentInput = "0"}}ColumnLayout {anchors.fill: parentanchors.margins: 10spacing: 10// 显示屏Rectangle {Layout.fillWidth: trueheight: 80radius: 10color: "#f0f0f0"border.color: "#ccc"border.width: 1Text {anchors.centerIn: parenttext: currentInputfont.pixelSize: 32horizontalAlignment: Text.AlignRightverticalAlignment: Text.AlignVCenterelide: Text.ElideRightwidth: parent.width - 20}}// 按钮网格GridLayout {Layout.fillWidth: trueLayout.fillHeight: truerows: 5columns: 4columnSpacing: 8rowSpacing: 8// 第一行Button {text: "C"; font.pixelSize: 20onClicked: clear()}Button {text: "←"; font.pixelSize: 20onClicked: backspace()}Button {text: "÷"; font.pixelSize: 20onClicked: inputOperator("/")}Button {text: "×"; font.pixelSize: 20onClicked: inputOperator("*")}// 第二行Button {text: "7"; font.pixelSize: 20onClicked: inputDigit("7")}Button {text: "8"; font.pixelSize: 20onClicked: inputDigit("8")}Button {text: "9"; font.pixelSize: 20onClicked: inputDigit("9")}Button {text: "−"; font.pixelSize: 20onClicked: inputOperator("-")}// 第三行Button {text: "4"; font.pixelSize: 20onClicked: inputDigit("4")}Button {text: "5"; font.pixelSize: 20onClicked: inputDigit("5")}Button {text: "6"; font.pixelSize: 20onClicked: inputDigit("6")}Button {text: "+"; font.pixelSize: 20onClicked: inputOperator("+")}// 第四行Button {text: "1"; font.pixelSize: 20onClicked: inputDigit("1")}Button {text: "2"; font.pixelSize: 20onClicked: inputDigit("2")}Button {text: "3"; font.pixelSize: 20onClicked: inputDigit("3")}Button {Layout.rowSpan: 2text: "="; font.pixelSize: 20onClicked: calculate()}// 第五行Button {text: "0"; font.pixelSize: 20onClicked: inputDigit("0")}Button {text: "."; font.pixelSize: 20onClicked: {if (waitingForOperand) {currentInput = "0"waitingForOperand = false}if (!currentInput.includes(".")) {currentInput += "."}}}// 空占位(因为 = 占两行,这里第5行第3列留空)Item { }}}
}
http://www.dtcms.com/a/533167.html

相关文章:

  • 网站怎么做团购什么是网络营销网络营销与电商营销有什么区别
  • Go语言:常量设置的注意事项
  • 网络营销导向企业网站建设的一般原则包括徐州网站排名系统
  • 基本魔法语言分支和循环 (二) (C语言)
  • 根目录下两个网站怎么做域名解析科技进步是国防强大的重要的保证
  • 微网站建设c品牌网站设计流程
  • 有哪些cua模型 Computer-Using Agent
  • 网站建设方案模板高校物流信息网站
  • 网工综合知识总结
  • 科技前沿七日谈:从AI普惠到硬件创新,技术正重塑产业边界
  • 初识AES
  • (五)图文结合-详解BLE连接原理及过程
  • 资产管理公司网站建设费用怎么入账电子商务行业发展趋势及前景
  • 机器学习日报05
  • 成都公园城市建设局网站seo诊断分析工具
  • 算法基础 典型题 数学(基础)
  • 网站开发运作wordpress数据库字典
  • 博州住房和城乡建设部网站wordpress开发教程
  • 邢台123网站模板百度推广官方投诉电话
  • 找网站开发人员wordpress ftp附件
  • Xshell 总是在最前端显示
  • 湖北省建设厅官方网站文件网站建设方案书写
  • 网站的成功案例wordpress 登陆白屏
  • 网站开发代码语言网站蜘蛛抓取
  • 斯坦福大学 | CS336 | 从零开始构建语言模型 | Spring 2025 | 笔记 | Lecture 6: Kernels,Triton
  • 【第十九周】自然语言处理的学习笔记04
  • 巴中住房和城乡建设局网站wordpress.exe
  • 7-SpringCloud-服务网关 Gateway-高级特性 Route
  • wordpress建站文本教程公司门户网站是什么
  • 淘宝商品详情 API 在品牌假货识别与维权中的技术应用与实践