news 2026/6/15 13:20:46

Qt Demo(4) 之 Quick实现考试成绩录入与查询系统

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Qt Demo(4) 之 Quick实现考试成绩录入与查询系统

Qt Demo(4) 之 Quick实现考试成绩录入与查询系统

  • 效果如下:

1. 新建项目

  • 创建
  • 项目结构

2. 具体实现

  • 主函数:
#include<QGuiApplication>#include<QQmlApplicationEngine>intmain(intargc,char*argv[]){QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);QGuiApplicationapp(argc,argv);QQmlApplicationEngine engine;constQUrlurl(QStringLiteral("qrc:/main.qml"));QObject::connect(&engine,&QQmlApplicationEngine::objectCreated,&app,[url](QObject*obj,constQUrl&objUrl){if(!obj&&url==objUrl)QCoreApplication::exit(-1);},Qt::QueuedConnection);engine.load(url);returnapp.exec();}
  • qml:
importQtQuick2.0importQtQuick.Controls2.0importQtQuick.Layouts1.0importQtQuick.Window2.0ApplicationWindow{visible:truewidth:800height:600title:qsTr("学生成绩录入与查询系统")color:"#f0f0f0"// --- 数据模型部分 ---// 存储所有数据的原始模型ListModel{id:sourceModel// 预置一些测试数据ListElement{name:"张三";sid:"1001";subject:"数学";score:90}ListElement{name:"李四";sid:"1002";subject:"数学";score:85}ListElement{name:"王五";sid:"1003";subject:"英语";score:88}}// 用于显示的模型(可能是全部数据,也可能是筛选后的数据)ListModel{id:displayModel}// 初始化时将 sourceModel 复制到 displayModelComponent.onCompleted:{refreshDisplayModel()}// --- 逻辑函数部分 ---// 刷新显示模型(将 sourceModel 的所有数据复制到 displayModel)functionrefreshDisplayModel(){displayModel.clear()for(vari=0;i<sourceModel.count;i++){varitem=sourceModel.get(i)displayModel.append(item)}}// 添加成绩functionaddGrade(){varnameVal=nameInput.text.trim()varsidVal=sidInput.text.trim()varsubjectVal=subjectInput.text.trim()varscoreVal=scoreInput.text.trim()if(!nameVal||!sidVal||!subjectVal||!scoreVal){showMsg("请填写完整信息!")return}varscoreNum=parseFloat(scoreVal)if(isNaN(scoreNum)||scoreNum<0||scoreNum>100){showMsg("分数必须是 0-100 之间的数字!")return}// 添加到原始数据sourceModel.append({"name":nameVal,"sid":sidVal,"subject":subjectVal,"score":scoreNum})// 重新刷新显示(如果有搜索词,这里简单处理为重置搜索)searchInput.text=""refreshDisplayModel()showMsg("添加成功!")clearInputs()}// 查询功能functionsearchGrade(){varkeyword=searchInput.text.trim().toLowerCase()displayModel.clear()if(keyword===""){refreshDisplayModel()return}for(vari=0;i<sourceModel.count;i++){varitem=sourceModel.get(i)// 模糊匹配:学号或姓名包含关键字if(item.sid.toLowerCase().indexOf(keyword)!==-1||item.name.toLowerCase().indexOf(keyword)!==-1){displayModel.append(item)}}if(displayModel.count===0){showMsg("未找到匹配记录")}}functionclearInputs(){nameInput.text=""sidInput.text=""subjectInput.text=""scoreInput.text=""}functionshowMsg(msg){messageText.text=msg messagePopup.open()}// --- 界面布局部分 ---ColumnLayout{anchors.fill:parent anchors.margins:20spacing:20// 1. 标题栏Label{text:"成绩管理系统"font.pixelSize:24font.bold:truecolor:"#333"Layout.alignment:Qt.AlignHCenter}// 2. 录入区域GroupBox{title:"成绩录入"Layout.fillWidth:truefont.pixelSize:16ColumnLayout{spacing:10anchors.fill:parent RowLayout{spacing:10Label{text:"姓名:";width:50}TextField{id:nameInput;placeholderText:"请输入姓名";Layout.fillWidth:true}Label{text:"学号:";width:50}TextField{id:sidInput;placeholderText:"请输入学号";Layout.fillWidth:true}}RowLayout{spacing:10Label{text:"科目:";width:50}TextField{id:subjectInput;placeholderText:"请输入科目";Layout.fillWidth:true}Label{text:"分数:";width:50}TextField{id:scoreInput;placeholderText:"0-100";Layout.fillWidth:true}Button{text:"添加记录"highlighted:trueonClicked:addGrade()}}}}// 3. 查询区域RowLayout{spacing:10Layout.fillWidth:trueTextField{id:searchInput Layout.fillWidth:trueplaceholderText:"输入姓名或学号进行搜索..."}Button{text:"查询"onClicked:searchGrade()}Button{text:"显示全部"onClicked:{searchInput.text=""refreshDisplayModel()}}}// 4. 数据表格 (ListView模拟)Rectangle{Layout.fillWidth:trueLayout.fillHeight:trueborder.color:"#ccc"border.width:1color:"white"Column{anchors.fill:parentspacing:1// 表头Row{width:parent.widthheight:40spacing:1Rectangle{color:"#e0e0e0";width:parent.width*0.25;height:parent.height;Label{anchors.centerIn:parent;text:"姓名";font.bold:true}}Rectangle{color:"#e0e0e0";width:parent.width*0.25;height:parent.height;Label{anchors.centerIn:parent;text:"学号";font.bold:true}}Rectangle{color:"#e0e0e0";width:parent.width*0.25;height:parent.height;Label{anchors.centerIn:parent;text:"科目";font.bold:true}}Rectangle{color:"#e0e0e0";width:parent.width*0.25;height:parent.height;Label{anchors.centerIn:parent;text:"分数";font.bold:true}}}// 表格内容ListView{id:listViewwidth:parent.widthheight:parent.height-40// 减去表头高度model:displayModelclip:truespacing:1delegate:Row{width:listView.widthheight:40spacing:1// 隔行变色property bool isEven:index%2===0property color rowColor:isEven?"#f9f9f9":"#ffffff"Rectangle{color:rowColor;width:parent.width*0.25;height:parent.height;Label{anchors.centerIn:parent;text:model.name}}Rectangle{color:rowColor;width:parent.width*0.25;height:parent.height;Label{anchors.centerIn:parent;text:model.sid}}Rectangle{color:rowColor;width:parent.width*0.25;height:parent.height;Label{anchors.centerIn:parent;text:model.subject}}Rectangle{color:rowColor;width:parent.width*0.25;height:parent.height;Label{anchors.centerIn:parent;text:model.score}}}}}}}// 简单的提示弹窗Popup{id:messagePopup anchors.centerIn:parentwidth:200height:80modal:truefocus:trueclosePolicy:Popup.NoAutoClosebackground:Rectangle{color:"#333";radius:5}Label{id:messageText anchors.centerIn:parentcolor:"white"font.pixelSize:14}Timer{interval:1500running:messagePopup.visibleonTriggered:messagePopup.close()}}}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/15 11:45:57

Qwen2.5-0.5B极速响应:本地AI对话工具实测

Qwen2.5-0.5B极速响应&#xff1a;本地AI对话工具实测 1. 开篇&#xff1a;轻量级AI助手的魅力 你有没有想过在自己的电脑上运行一个智能对话AI&#xff1f;不需要昂贵的云端服务&#xff0c;不需要担心隐私泄露&#xff0c;就像在本地安装一个软件那样简单。今天我要分享的Q…

作者头像 李华
网站建设 2026/6/15 11:45:42

告别手动整理:YOLO X Layout自动分析文档布局

告别手动整理&#xff1a;YOLO X Layout自动分析文档布局 还在为处理海量文档而头疼吗&#xff1f;无论是扫描的合同、PDF报告还是电子书&#xff0c;手动识别和整理其中的文字、表格、图片&#xff0c;不仅耗时费力&#xff0c;还容易出错。想象一下&#xff0c;如果能有一个…

作者头像 李华
网站建设 2026/6/15 11:47:32

CLAP模型实战案例分享:基于自然语言Prompt的野生动物声纹识别应用

CLAP模型实战案例分享&#xff1a;基于自然语言Prompt的野生动物声纹识别应用 1. 为什么野生动物监测需要“听懂”声音&#xff1f; 在云南西双版纳的密林深处&#xff0c;红外相机拍到了一只云豹&#xff0c;但镜头外——几十米开外的树冠上&#xff0c;一种罕见的角鸮正发出…

作者头像 李华
网站建设 2026/6/15 11:21:20

DeepSeek-OCR-2效果展示:竖排繁体中文古籍PDF高精度识别与断句还原

DeepSeek-OCR-2效果展示&#xff1a;竖排繁体中文古籍PDF高精度识别与断句还原 你有没有试过打开一本清代刻本的PDF&#xff0c;满屏竖排繁体、夹批小字、朱砂圈点&#xff0c;连页眉都写着“光绪壬寅年重刊”&#xff1f;想把里面的内容转成可编辑文本&#xff0c;结果OCR一跑…

作者头像 李华
网站建设 2026/6/15 11:45:32

ccmusic-database音乐分类:多模态融合技术探索

ccmusic-database音乐分类&#xff1a;多模态融合技术探索 当AI不仅能"听"音乐&#xff0c;还能"读"懂歌词&#xff0c;音乐分类会迎来怎样的突破&#xff1f; 1. 多模态音乐理解的机遇与挑战 音乐从来不只是声音的集合。一段完整的音乐体验&#xff0c;包…

作者头像 李华
网站建设 2026/6/15 12:21:38

如何用Python彻底革新COMSOL仿真流程?MPh自动化框架全解析

如何用Python彻底革新COMSOL仿真流程&#xff1f;MPh自动化框架全解析 【免费下载链接】MPh Pythonic scripting interface for Comsol Multiphysics 项目地址: https://gitcode.com/gh_mirrors/mp/MPh 在工程仿真领域&#xff0c;重复的参数调整、繁琐的结果导出、易错…

作者头像 李华