news 2026/2/15 2:26:31

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/2/11 1:28:29

Qwen3-Reranker-4B入门必看:如何用Qwen3-Reranker-4B增强LlamaIndex检索质量

Qwen3-Reranker-4B入门必看&#xff1a;如何用Qwen3-Reranker-4B增强LlamaIndex检索质量 在构建高质量RAG&#xff08;检索增强生成&#xff09;系统时&#xff0c;光靠基础向量检索往往不够——相似度分数容易受词频、长度和语义粒度影响&#xff0c;导致关键文档排在后面。这…

作者头像 李华
网站建设 2026/2/12 15:49:09

ChatGLM3-6B生产环境部署:支持万字长文处理的办公助手

ChatGLM3-6B生产环境部署&#xff1a;支持万字长文处理的办公助手 1. 为什么你需要一个“能记住万字”的本地办公助手&#xff1f; 你有没有遇到过这些场景&#xff1a; 把一份20页的产品需求文档粘贴进对话框&#xff0c;结果模型只读了前几百字就开始胡说&#xff1f;写代…

作者头像 李华
网站建设 2026/2/11 1:27:58

鸣潮游戏性能优化完全指南:系统化解决方案

鸣潮游戏性能优化完全指南&#xff1a;系统化解决方案 【免费下载链接】WaveTools &#x1f9f0;鸣潮工具箱 项目地址: https://gitcode.com/gh_mirrors/wa/WaveTools 现象诊断&#xff1a;识别性能瓶颈 在鸣潮游戏体验过程中&#xff0c;玩家可能会遇到多种性能问题&a…

作者头像 李华
网站建设 2026/2/12 12:09:12

Pi0具身智能小白教程:浏览器即可玩的机器人模拟器

Pi0具身智能小白教程&#xff1a;浏览器即可玩的机器人模拟器 本文约3800字&#xff0c;阅读时间约15分钟&#xff0c;包含详细步骤和代码示例 1. 引言&#xff1a;什么是Pi0具身智能&#xff1f; 想象一下&#xff0c;你只需要在浏览器中输入一句话&#xff0c;比如"把吐…

作者头像 李华
网站建设 2026/2/11 1:27:33

零基础玩转GME-Qwen2-VL-2B:图文检索匹配实战指南

零基础玩转GME-Qwen2-VL-2B&#xff1a;图文检索匹配实战指南 你是不是遇到过这样的场景&#xff1a;手里有一张图片&#xff0c;需要从一堆文字描述中找到最匹配的那一个&#xff1f;比如电商平台需要为商品图片自动匹配最合适的标题&#xff0c;或者内容审核需要检查图片和文…

作者头像 李华