用QT实现桌面时钟设计.doc

合集下载

qt数字时钟课程设计

qt数字时钟课程设计

qt数字时钟课程设计一、课程目标知识目标:1. 理解Qt编程环境的基本操作,掌握Qt中关于时间处理的基本类和方法。

2. 学会使用Qt工具箱中的控件,特别是QTimer和QTime,理解它们在数字时钟制作中的作用。

3. 掌握数字时钟的核心算法,能够将时间转换为数字格式,并在屏幕上正确显示。

技能目标:1. 能够独立设计并编写一个简单的Qt数字时钟程序,具备实现类似小型项目的能力。

2. 培养学生的问题解决能力,通过调试程序,解决开发过程中遇到的问题。

3. 增强学生的项目实践能力,学会如何规划项目,分配时间和资源。

情感态度价值观目标:1. 激发学生对计算机编程的兴趣,培养对信息技术学科的热情。

2. 培养学生的团队协作意识,通过小组讨论和合作,共同完成项目。

3. 引导学生认识到科技改变生活的重要性,增强创新精神和实践能力。

课程性质:本课程为信息技术学科的课程,结合实际编程操作,强调理论与实践相结合。

学生特点:学生应为具有一定计算机基础知识和逻辑思维能力的高年级学生。

教学要求:课程应注重学生的动手实践能力培养,通过案例教学法和任务驱动法,引导学生主动探索和解决问题。

同时,注重课程的可评估性,确保学生能够达到预定的学习成果。

二、教学内容1. Qt基本环境配置与操作:介绍Qt的安装、创建新项目和基本界面设计。

- 熟悉Qt Creator界面。

- 学会创建Qt Widgets应用程序。

2. 时间处理类QTime和定时器QTimer的使用:- 学习QTime类的用法,掌握时间获取和格式化。

- 掌握QTimer的使用,实现时钟的动态更新。

3. 数字时钟设计原理与实现:- 分析数字时钟的组成和逻辑。

- 编写程序,实现数字时钟的核心功能。

4. 控件布局与界面美化:- 学习Qt中的布局管理,合理设计时钟界面。

- 了解基本的界面美化技巧,提升视觉效果。

5. 调试与优化:- 介绍常见的问题调试方法。

- 对程序进行性能优化。

6. 项目实践:- 分组合作,完成一个Qt数字时钟项目。

qt闹铃课程设计

qt闹铃课程设计

qt闹铃课程设计一、课程目标知识目标:1. 学生能理解qt闹铃的基本概念,掌握其编程原理;2. 学生能运用qt编程语言,独立设计并实现一个具备基本功能的闹铃程序;3. 学生了解闹铃程序在实际生活中的应用,并能够解释其工作原理。

技能目标:1. 学生掌握qt编程环境的基本操作,能够编写、调试和运行简单的闹铃程序;2. 学生通过实践操作,培养解决问题的能力,提高逻辑思维能力;3. 学生学会运用已学知识,进行创新设计,具备一定的编程实践能力。

情感态度价值观目标:1. 学生对计算机编程产生兴趣,培养积极的学习态度;2. 学生在团队合作中,学会互相帮助、共同进步,培养良好的团队协作精神;3. 学生通过编程实践,认识到科技改变生活的实际意义,激发对科技创新的热情。

课程性质:本课程为信息技术学科的一节实践性课程,结合学生特点和教学要求,注重培养学生的动手操作能力和创新思维。

学生特点:学生处于具备一定信息技术知识基础的年级,对新鲜事物充满好奇心,具备较强的学习能力和动手能力。

教学要求:课程要求教师以引导为主,注重启发式教学,关注学生的个体差异,提高学生的编程兴趣和实际操作能力。

通过课程学习,使学生能够将理论知识与实际应用相结合,培养具备创新精神和实践能力的优秀学生。

二、教学内容1. qt编程基础:- qt简介:介绍qt框架的发展历程、特点和应用领域;- qt环境搭建:讲解qt creator的安装和使用方法;- 基本语法:复习c++基础知识,介绍qt中的信号与槽机制、事件处理等。

2. 闹铃程序设计:- 界面设计:学习使用qt designer设计闹铃程序界面;- 功能实现:讲解闹铃程序的核心功能,如时间设置、闹铃响起、暂停和取消等;- 代码编写:指导学生编写闹铃程序的代码,并进行调试和优化。

3. 闹铃程序优化与拓展:- 界面美化:介绍如何为闹铃程序添加背景、图标等元素,提高视觉效果;- 功能拓展:引导学生思考如何为闹铃程序添加更多实用功能,如多闹铃设置、音量调节等;- 代码优化:讲解如何提高代码的可读性和可维护性,培养良好的编程习惯。

Qt设置系统时间

Qt设置系统时间

Qt设置系统时间大家都知道Qt中有QDateTime等有关时间与日期的类,类中包含很多成员函数,可以很方便的实现有关时间与日期的操作,比如:想要获得系统当前的时间与日期,可以调用currentDateTime();但是Qt并没有提供设置系统时间的方法,这样我们只能自己来区分平台,调用平台相关的API,这篇文章实现在Windows下的设置。

常用的与时间有关的Win32 API有两个:GetSystemTime(); 与SetSystemTime(); 下面是函数原型:Windows上实现:VOID GetSystemTime(LPSYSTEMTIME lpSystemTime);BOOL SetSystemTime( const SYSTEMTIME *lpSystemTime );我们查一下MSDN 看看LPSYSTEMTIME 与 SYSTEMTIME 是什么东东:typedef struct _SYSTEMTIME {WORD wYear;WORD wMonth;WORD wDayOfWeek;WORD wDay;WORD wHour;WORD wMinute;WORD wSecond;WORD wMilliseconds;} SYSTEMTIME, *PSYSTEMTIME;从中我们知道SYSTEMTIME 为结构体类型,LPSYSTEMTIME为结构体指针,传递给两个函数的参数都必须是指针或引用类型,下面看一个Qt的调用实例:1#include <QtCore/QCoreApplication>2#include <iostream>3#include <time.h>4#include <windows.h>5#include <QDateTime>6#include <QDebug>7using namespace std;89bool setDate(int,int,int);10int main(int argc, char*argv[])11{12 QCoreApplication a(argc, argv);13 qDebug()<<QDateTime::currentDateTime()<<endl; // Qt API 输出当前时间14setDate(2011,1,1); //设置时间15 qDebug()<<QDateTime::currentDateTime()<<endl; // Qt API 获取当前时间16return 0; //让程序完成任务直接退出吧...17}1819bool setDate(int year,int mon,int day)20{21 SYSTEMTIME st;22 GetSystemTime(&st); // Win32 API 获取系统当前时间,并存入结构体st中23 st.wYear=year;24 st.wMonth=mon;25 st.wDay=day;2627return SetSystemTime(&st); //Win32 API 设置系统时间28}29Linux上实现:qt-读取和修改系统时间QTime ct = QTime::currentTime();修改系统时间// change the system timeQDateTime dt = QDateTime::currentDateTime();dt.setTime(timeEditor->time());time_t tt = (time_t)dt.toTime_t();int r = stime(&tt);if (r){if (errno == EPERM)QMessageBox::warning(this, "Error", "You don't have permission to change system time.");}。

桌面时钟设计与实现

桌面时钟设计与实现

桌面时钟设计与实现成员:李睿江秀伟许艺韬专业班级:通信11401实践日期: 2014年月6日-2014年1月17日一、综合训练目的与要求本次面向对象编程实践是学习了C++面向对象程序设计之后进行的必要的实践性环节。

由于各种原因,信息管理与信息系统专业是在学习了一年之后才安排了这个实习,因为就在考试周,任务有点繁重。

本课综合训练的目的和任务:1. 重温和加深对 C++面向对象编程语言的基本知识的理解和掌握;2. 掌握 C++语言编程和程序调试的基本技能;3. 利用网络资源,自己学习MFC或者Qt等相关软件对页面进行布局;4. 学会与他人协作进行小软件的设计,遇到分歧时,能够处理得当,共同完成实践要求。

二、综合训练任务(1)需求分析随着计算机的普及,人们对计算机的要求远不是当初的进行大型数据运算和复杂数据分析了,人与人之间的日常交流,交易等等也都通过计算机来完成了。

因为是“日常用品”,当然想让它看起来舒服一些了,因此,人们对电脑桌面美观实用的需求也达到了一定的程度,现在的生活节奏也快了,都希望在最短的时间内完成最多的事,获取最大的信息量。

人们为了在用计算机工作的同时,随时了解时间,不错过自己的行程安排。

都会在电脑桌面安装一个显示时间日历的小工具,所以桌面时钟的需求还是挺大的。

(2)任务●桌面时钟是日常小工具,所以良好的人机界面应该做到●采用面向对象思想设计实现各种类●因为涉及界面,所以要运用界面设计,我们选择采用QT做界面设计●能够通过自学习运用自带函数来实现功能●在实习期间,完成自己负责部分代码的编写和调试,并完成规范的实习论文三、总体设计(1)主要模块划分图 1 主要模块(2)模块说明圆盘时钟:指的是圆盘时钟,也就是说,有时针分针秒针显示的时钟,这一模块中,需要绘制各个时针。

电子时钟:我们把它定义为向电子表那样显示的时钟部分,每秒更新一次,并显示出来。

日历:日历就是显示当前日历,并能查询前后的日历。

QT时钟实验报告

QT时钟实验报告

实验报告书实验名称: qmainwindow 程序设计专业班级: 111030706学号: 11103070602 姓名:陈俊宇潘耀斌联系电话:指导老师:谭智实验时间: 2014.4.28—2014.6.16 计算机科学与工程学院计算机实验室(中心)1 设计分析qmainwindow程序设计对qmainwindow类窗口进行分析,qmainwindow类是一个经常用到的类,为用户提供了一个主窗口程序,可包含一个菜单条,一个工具栏,一个状态条以及一个中央窗体,是许多应用程序的基础,,如文本编辑器,图片浏览器都是以qmainwindow为基础实现的。

本次程序设计,首先分析了一个基本的窗口程序的实现,只包含基本的菜单和工具栏,实现了基本的窗口功能,如打开、新建等。

2 功能需求实现一个基本的窗口程序,包括一个菜单条,一个工具栏,中央可编辑窗体及状态栏。

实现的效果如图所示。

在编写文本编辑器等应用中,需要对文本进行排序,以列表的方式显示各段文本。

即实现文本的排序功能,并实现文本的对齐及撤销和恢复功能,如图所示。

在编辑框中任意输入几段文字,单击工具栏上的下拉列表框,选择排序方式,则光标所在的文本段以所选排序方式自动缩进排序显示,并且紧接着的文本段以同样的方式排列;工具栏中部的4个快捷按钮实现文本的对齐功能,分别为左对齐,右对齐、居中和两端对齐;工具栏右部的快捷按钮实现文本操作的前进/回退功能。

3 程序源代码#include mainwindow11103070602.h #include ui_mainwindow11103070602.h #include &lt;qfile&gt;#include &lt;qfiledialog&gt; #include &lt;qtextstream&gt; #include &lt;qmessagebox&gt; #include &lt;qfont&gt;#include &lt;qfontdialog&gt; #include &lt;qcolordialog&gt; #include &lt;qtextcursor&gt; mainwindow11103070602::mainwindow11103070602(qwidget *parent) : qmainwindow(parent),ui(new ui::mainwindow11103070602) { ui-&gt;setupui(this);issaved=false;curfile=tr(noname);ui-&gt;action_left-&gt;setcheckable(true); ui-&gt;action_right-&gt;setcheckable(true); ui-&gt;action_justift-&gt;setcheckable(true); ui-&gt;action_center-&gt;setcheckable(true); /* workspace= new qworkspace; setcentralwidget(workspace); createmenu();qmainwindow*window1=new qmainwindow; qmainwindow*window2=new qmainwindow; qmainwindow*window3=new qmainwindow; window1-&gt;setwindowtitle(tr(window i)); window2-&gt;setwindowtitle(tr(window ii)); window3-&gt;setwindowtitle(tr(window iii)); qtextedit*edit1=new qtextedit; qtextedit*edit2=new qtextedit; qtextedit*edit3=new qtextedit; edit1-&gt;settext(tr(window 1)); edit2-&gt;settext(tr(window 2)); edit3-&gt;settext(tr(window 3)); window1-&gt;setcentralwidget(edit1); window2-&gt;setcentralwidget(edit2); window3-&gt;setcentralwidget(edit3); workspace-&gt;addwindow(window1); workspace-&gt;addwindow(window2); workspace-&gt;addwindow(window3); *///connect(ui-&gt;textedit-&gt;document(),signal(redoavallable(bool)),ui-&gt;action_redo,slot(setenabled(bool))); //connect(ui-&gt;textedit-&gt;document(),signal(undoavallable(bool)),ui-&gt;action_undo,slot(setenabled(bool))); } mainwindow11103070602::~mainwindow11103070602() {delete ui;}void mainwindow11103070602::slotnewfile() {saveornot();mainwindow11103070602 *newwin=new mainwindow11103070602; newwin-&gt;show();}void mainwindow11103070602::saveornot() {if(ui-&gt;textedit-&gt;document()-&gt;ismodified()) {qmessagebox box;box.setwindowtitle(tr(waring)); box.settext(curfile+tr(no save,save now?)); box.setstandardbuttons(qmessagebox::yes|qmessagebox::no); if(box.exec()==qmessagebox::yes) slotsaveas();}}篇二:linux实验报告(qt) linux实验报告学号:092201046 姓名:张攀班级:信工0901实验时间:2012-4-26 实验地点:a4049 指导老师:雷鸿实验名称实验目的 qt/e图形界面开发环境的搭建成功搭建qt/e图形界面一、实验题目qt/e图形界面开发环境的搭建二、实验内容及结果(请将相应题目代码和结果截图写在相应题目下方)1、[admin@localhost ~]$ su - root密码:[root@localhost ~]# cd /mnt/hgfs/vmwarefedora14/ [root@localhost my6410]# cd qt-x11-opensource-src-4.5.3 [***************************************.3]#./configure which edition of qt do you want to use ? type o if you want to use the open source edition. othis is the qt/x11 open source edition. you are licensed to use this software under the terms of the lesser gnu general public license (lgpl) versions 2.1. you are also licensed to use this software under the terms of the gnu general public license (gpl) versions 3. type 3 to view the gnu general public license version 3. type l to view the lesser gnu general public license version 2.1. type yes to accept this license offer. type no to decline this license offer. do you accept the terms of either license? yes for/home/admin/my6410/qt-x11-opensource-src-4.5.3/demos/arthurplugin/arthurplugin.pro qt is now configured for building. just run gmake. once everything is built, you must run gmake install. qt will be installed into /usr/local/trolltech/qt-4.5.3 to reconfigure, run gmake confclean and configure.[***************************************.3]#gmake [***************************************.3]#gmakeinstall [root@localhost qt-x11-opensource-src-4.5.3]# cd/usr/local/trolltech/qt-4.5.3/ [***************************************.3]#cd.. [root@localhost my6410]#tar –xzvf qt-everywhere-opensource-src-4.7.3.tar [root@localhost my6410]#mv qt-everywhere-opensource-src-4.7.3qt-everywhere-opensource-src-4.7.3-x86 [root@localhost my6410]#tar –xzvf qt-everywhere-opensource-src-4.7.3.tar [root@localhost my6410]#mv qt-everywhere-opensource-src-4.7.3-arm [**********************************************.3[root@localhostmy6410]cdqt-everywhere-opensource-src-4.7.3-x86qt-everywhere-opensource-src-4.7.3-x86]./configure –prefix/usr/local/trolltech/qtembedded-4.5.3-x86 [**********************************************.3-x86]gmake [**********************************************.3-x86]gmakeinstall 编译正在进行中[root@localhost opt]#./qt-creator-linux-x86- opensource-2.2.0.bin [root@localhost opt]# lsarm qt-creator-linux-x86-opensource-2.2.0.bin三、实验总结篇三:用qt实现桌面时钟设计.doc附件二【学生用】西北农林科技大学信息工程学院面向对象实习实习报告题学号姓名专业班级指导教师实践日期目:桌面时钟设计与实现目录一、综合训练目的与要求 ............................................................................. ........................................ 1 二、综合训练任务 ............................................................................. .................................................... 1 三、总体设计 ............................................................................. ............................................................ 2 四、详细设计说明 ............................................................................. .................................................... 4 五、调试与测试 ............................................................................. ........................................................ 5 六、实习日志 ............................................................................. ............................................................ 9 七、实习总结 ............................................................................. ............................................................ 9 八、附录:核心代码清单 ............................................................................. . (9)一、综合训练目的与要求正文这次面向对象实习实践活动是在c++面向对象程序设计之后进行的实践环节旨在加深对面向对象编程的设计思想(类的设计,抽象,封装,包含与继承,多态性,uml图)的理解,掌握使用c++编程的基本的技能,从而能通过小型的团队开发一些小软件的实现与设计,并能够解决一定的实际问题。

基于Qt5的闹钟实现代码

基于Qt5的闹钟实现代码

本程序基于QWidget实现,可以动态显示钟表,并且可以添加铃声头文件widget.h代码如下:#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include<QTime>#include<QTimer>#include<QLabel>#include<QPushButton>#include<QVBoxLayout>#include<QHBoxLayout>#include<QTimeEdit>#include<QLCDNumber>#include<QVariant>#include<QAction>#include<QMenu>#include<QCloseEvent>#include<QApplication>#include<QMessageBox>#include<QSystemTrayIcon>namespace Ui {class Widget;}class Widget : public QWidget{Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();signals:void isCurrentTime();private:Ui::Widget *ui;void init();void layout();void con();QLCDNumber *lcdNumber;QLabel *label;QTimeEdit *timeEdit;QPushButton *button;QTimer *timer;QTimer *uptimer;QTimer *dtimer;QTimer *itimer;QRect rect;int x;int y;double du;QSystemTrayIcon *trayIcon;QAction *restoreAct;QAction *quitAct;QMenu *trayIconMenu;private slots:void clockChange();void dialogUp();void mini();void down();void comToCur();protected:void closeEvent(QCloseEvent *e); };#endif // WIDGET_H源文件widget.cpp代码如下:#include "widget.h"#include "ui_widget.h"#include<QtDebug>#include<QApplication>#include<QDesktopWidget>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget){ui->setupUi(this);init();layout();con();void Widget::init(){setWindowTitle("我的闹钟");setFixedSize(300,80);timer=new QTimer;uptimer=new QTimer;dtimer=new QTimer;itimer=new QTimer;lcdNumber=new QLCDNumber;lcdNumber->setFixedSize(150,50);lcdNumber->setDigitCount(12);lcdNumber->setSegmentStyle(QLCDNumber::Flat);lcdNumber->display(QTime::currentTime().toString("hh:mm:ss:zzz"));label=new QLabel("闹钟");timeEdit=new QTimeEdit;timeEdit->setDisplayFormat("hh:mm:ss");button=new QPushButton("确定");trayIcon=new QSystemTrayIcon;trayIcon->setIcon(QIcon(":/new/prefix1/clock.ico"));trayIcon->show();setWindowIcon(QIcon(":/new/prefix1/clock.ico"));restoreAct=new QAction("显示",this);quitAct=new QAction("退出",this);trayIconMenu=new QMenu;trayIconMenu->addAction(restoreAct);trayIconMenu->addAction(quitAct);trayIcon->setContextMenu(trayIconMenu);setWindowFlags(Qt::WindowStaysOnTopHint);}void Widget::layout(){QHBoxLayout *rightTopLay=new QHBoxLayout;rightTopLay->addWidget(label);rightTopLay->addWidget(timeEdit);QHBoxLayout *rightButtonLay=new QHBoxLayout;rightButtonLay->addStretch();rightButtonLay->addWidget(button);QVBoxLayout *rightLay=new QVBoxLayout;rightLay->addLayout(rightTopLay);rightLay->addLayout(rightButtonLay);rightLay->addStretch();QHBoxLayout *m=new QHBoxLayout;m->addWidget(lcdNumber);m->addLayout(rightLay);setLayout(m);QDesktopWidget *p=new QDesktopWidget;p=QApplication::desktop();//rect=p->rect();x=rect.right()-width()-2;//y=rect.bottom()+30;//setGeometry(x,y,width(),height());}void Widget::con(){connect(timer,SIGNAL(timeout()),this,SLOT(clockChange()));timer->start(1);connect(uptimer,SIGNAL(timeout()),this,SLOT(dialogUp()));uptimer->start(20);connect(dtimer,SIGNAL(timeout()),this,SLOT(down()));dtimer->start(20);connect(button,SIGNAL(clicked(bool)),dtimer,SIGNAL(timeout())); connect(button,SIGNAL(clicked(bool)),this,SLOT(mini()));connect(itimer,SIGNAL(timeout()),this,SLOT(comToCur()));itimer->start(20);connect(restoreAct,SIGNAL(triggered(bool)),uptimer,SIGNAL(timeout())) ;connect(this,SIGNAL(isCurrentTime()),uptimer,SIGNAL(timeout())); connect(itimer,SIGNAL(timeout()),this,SLOT(comToCur()));itimer->start(20);connect(quitAct,SIGNAL(triggered(bool)),this,SLOT(close()));void Widget::clockChange(){lcdNumber->display(QTime::currentTime().toString("hh:mm:ss:zzz")); }void Widget::dialogUp(){show();uptimer->start(20);if(y>=rect.bottom()-height()-50){y-=2;}else {uptimer->stop();}setGeometry(x,y,width(),height());}void Widget::down(){dtimer->start(20);if(y<rect.bottom()){y+=height()+20;}else{dtimer->stop();}setGeometry(x,y,width(),height());}void Widget::mini(){hide();}void Widget::closeEvent(QCloseEvent *e){QMessageBox::information(0,"退出","您确定要退出吗?");void Widget::comToCur(){if(QVariant(QTime::currentTime().toString())==QVariant(timeEdit->time ()).toString()){QApplication::beep();emit isCurrentTime();}}Widget::~Widget(){delete ui;}程序运行截图:添加铃声:。

Qt程序

Qt程序

Qt作业效果图如下:功能就是:显示每台电脑当前的具体时间情况,分别显示出小时、分钟、秒钟;让你一目了然。

头文件:mydialog.h#ifndef MYDIALOG_H#define MYDIALOG_H#include<QtGui>class QLCDNumber; //定义一个QLCDNumber类class QTimer;class MyDialog:public Qdialog //MYDIALOG继承了Qdialog{Q_OBJECTpublic:explicit MyDialog(QWidget*parent=0); //创建一个空内容的构造函数signals:public slots:void onTimerOut(); //自定义一个计时函数private:QLCDNumber*lcd;QTimer*timer;};#endif源文件:mydialog.cpp#include"mydialog.h"MyDialog::MyDialog(QWidget*parent):QDialog(parent){lcd=new QLCDNumber(); //新建一个QLCDNumber对象lcd->setDigitCount(10); //设置显示的模式为十进制lcd->setMode(QLCDNumber::Dec);lcd->setSegmentStyle(QLCDNumber::Flat); //设置显示方式timer=new QTimer(); //新建一个QTimer对象timer->setInterval(1000);//设置定时器每个多少毫秒发送一个timeout()信号timer->start();//启动定时器QVBoxLayout*layout=new QVBoxLayout();layout->addWidget(lcd);//信号和槽connect(timer,SIGNAL(timeout()),this,SLOT(onTimerOut()));//重新设置窗口的布局管理器this->setLayout(layout);this->resize(200,100);//重新设置窗口的大小this->setWindowTitle("QTimerDemo");//重新设置窗口的标题}void MyDialog::onTimerOut(){QTime time=QTime::currentTime();//获取系统当前时间lcd->display(time.toString("hh:mm:ss"));//设置晶体管控件QLCDNumber上显示的内容}main.cpp#include<QApplication>#include"mydialog.h"int main(int argc,char*argv[]) //文件运行时以空格为间隔的所有参数个数,argv[ ]数组中依次存放所有字串{QApplication a(argc,argv); //定义一个应用对象MyDialog dialog;dialog.show();return a.exec();}。

Qt绘制简单时钟

Qt绘制简单时钟

Qt绘制简单时钟本⽂实例为⼤家分享了Qt绘制简单时钟的具体代码,供⼤家参考,具体内容如下概述(1)基本原理利⽤简单的⼏何图形绘出表盘和指针,然后基于系统时间调整指针偏转⾓度达到模拟时钟的效果。

其中可以加上⾃⼰喜欢的背景资源,设计不同风格的时钟。

(2)部分函数说明①调⾊和填充painter.setBrush(Qt::red); //设置画刷,填充颜⾊(红⾊)painter.setPen(Qt::red); //设置画笔,外围轮廓颜⾊(红⾊)②抗锯齿(减少图形外围锯齿状,使线条平滑)painter.setRenderHint(QPainter::Antialiasing,true);③⾃动保存与恢复painter.save(); //保存属性painter.restore(); //属性恢复核⼼代码dialog.h⽂件class Dialog : public QDialog{Q_OBJECTpublic:Dialog(QWidget *parent = nullptr);~Dialog();void Dialog::paintEvent(QPaintEvent *); //函数声明private:Ui::Dialog *ui;};头⽂件#include <QPainter>#include <QPoint>#include <QTime>#include <QTimer>#include <QPixmap>#include <math.h>指针参数(第⼀个QPoint前可以加static)QPoint sec[4]={QPoint(0,-80),QPoint(2,0),QPoint(0,8),QPoint(-2,0)};QPoint min[4]={QPoint(0,-62),QPoint(4,0),QPoint(0,10),QPoint(-4,0)};QPoint hour[4]={QPoint(0,-48),QPoint(5,0),QPoint(0,11),QPoint(-5,0)};关联函数Dialog::Dialog(QWidget *parent): QDialog(parent), ui(new Ui::Dialog){ui->setupUi(this);resize(300,300);QTimer * timer = new QTimer(this);timer->start(1000); //确定刷新时间(此处为1000毫秒=1秒)connect(timer,SIGNAL(timeout()),this,SLOT(update())); //update()为⾃动刷新界⾯,使指针可以不停转动}绘图函数(注意:只要时、分、秒三个指针任意完成⼀个,则其余两个只需修改指针旋转⾓度规则,其余属性相同即可)void Dialog::paintEvent(QPaintEvent *){QPainter painter(this);//时钟背景QRect q1(20,20,0.5*512,0.5*512);QRect q2(0,0,512,512);QPixmap map(":/b/ClockBack/clockback.png");painter.drawPixmap(q1,map,q2);QTime time=QTime::currentTime(); //调⽤系统时间//表盘painter.setRenderHint(QPainter::Antialiasing,true); //抗锯齿,平滑化painter.setPen(QPen(Qt::white,6,Qt::SolidLine)); //调⾊painter.translate(50,50);painter.drawEllipse(0,0,198,198);//指针属性painter.translate(99,99);//秒针painter.setRenderHint(QPainter::Antialiasing,true);painter.setBrush(Qt::red); //设置画刷,填充颜⾊painter.setPen(Qt::red); //设置画笔,外围轮廓颜⾊painter.save(); //保存属性painter.rotate(6.0*time.second());painter.drawPolygon(sec,4); //绘制指针(绘制⼏何图形函数)painter.restore(); //属性恢复//分针(同秒针)painter.setRenderHint(QPainter::Antialiasing,true);painter.setBrush(Qt::white);painter.setPen(Qt::white);painter.save();painter.rotate(6.0*time.minute()+time.second()/60.0);painter.drawPolygon(min,4);painter.restore();//时针(同秒针)painter.setRenderHint(QPainter::Antialiasing,true);painter.setBrush(Qt::black);painter.setPen(Qt::black);painter.save();painter.rotate(30.0*time.hour()+time.minute()/60.0);painter.drawPolygon(hour,4);painter.restore();//表盘刻度//长刻度(整点刻度)for(int i=1;i<=12;i++){painter.rotate(30);painter.drawLine(0,-100,0,-85);}//短刻度for(int i=1;i<=60;i++){painter.rotate(6);painter.drawLine(0,-94,0,-90);}}背景与效果图1、这⾥分享我⾃⼰采⽤的背景2、效果图以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

附件二【学生用】西北农林科技大学信息工程学院面向对象实习实习报告题目:桌面时钟设计与实现学号姓名专业班级指导教师实践日期目录一、综合训练目的与要求 (1)二、综合训练任务 (1)三、总体设计 (2)四、详细设计说明 (4)五、调试与测试 (5)六、实习日志 (9)七、实习总结 (9)八、附录:核心代码清单 (9)一、综合训练目的与要求正文这次面向对象实习实践活动是在C++面向对象程序设计之后进行的实践环节旨在加深对面向对象编程的设计思想(类的设计,抽象,封装,包含与继承,多态性,UML图)的理解,掌握使用C++编程的基本的技能,从而能通过小型的团队开发一些小软件的实现与设计,并能够解决一定的实际问题。

以及掌握撰写规范程序设计文档的能力,还有训练语言组织与表达能力,提高综合能力。

二、综合训练任务1、需求分析随着科学技术的不断发展,计算机已经成为了我们日常工作生活和学习中不可缺少的工具,很多人每天都使用计算机与外界进行交互,为了让用户对计算机有更舒心的体验,桌面的个性化是必不可少,因此很有必要设计美观使用的桌面的小软件,来让计算机更加人性化,二当今社会是信息时代,生活节奏也随即加快,而一个美观的能提示显示时间和日历的小软件能让人们合理安排自己的行程,避免不必要的损失,因此桌面时钟有一定的需求性。

2、任务实现(1)良好的人机交互界面是用户所亲睐的。

(2)使用QT Creater 来开发该界面程序。

(3)采用面向对象的思想实现时钟类、设置类的封装和与实现(4)实现软件界面的布局(5)为了实现不同类之间的联系提供类的接口。

(6)实现对代码的编写和调试工作,每天记录实习日志(7)独立完成论文并进行答辩。

三、总体设计1、整体功能设计图3-1-1图3-1-1 是整体功能的设计,为了界面的整洁性和强化界面的显示采取以上的功能模型组合。

增强可视化和人性化,用户可以自己更改时间和更改日历的显示的样式以及一些其他的操作。

2、各部件介绍:(1)主窗口实现窗口布局包含了数字时钟部件、日历部件、时间部件、和菜单。

(2)日历设置窗口实现窗口布局包含了日历样式设置和常规设置部件。

(3)时间设置窗口实现窗口的布局包含了时间Edit和刻度时钟动态实现更改时间设置。

3、整体UML图:图3-3-1图3-3-1是程序整体的UML图由于属性太多因此省略掉了属性和操作。

以程序中每个类之间的联系进行组合,反映出程序中结构和整体的框架。

便于后期编程。

四、详细设计说明1、主窗口类(UML图):图4-1-1图4-1-1是主窗口的UML图主要给用户可视界面进行布局和各个部件的创建和组合。

最后通过设置菜单对不同的部件及实例化的类-对象进行设置和优化。

2.日期时间属性类图4-2-1图4-2-1是日期时间属性窗口类的UML图,主要功能是获取时间和设置时间以及创建布局和label放置动态的时间信息。

最主要的是提供一个槽函数控制时间的动态性。

3、刻度时钟的类(UML)图4-3-1图4-3-1实现的是刻盘时钟窗口类实现。

主要的实现的功能是表盘的绘制和表针的绘制。

而表盘的绘制功能使用的QWidget中的虚函数paintEvent 的实现五、调试与测试1、主窗口框架调试:图5-1-1图5-1-1是对住窗口框架的布局,刚开始设计布局的时候不太了解三种布局和组合框的创建,但是进过后面对Qt的GUI类的了解慢慢的了解了布局和组合框架之间的联系。

经过多次调错,最后把4个组合框按照自己的想法进行了布局。

图5-1-2图5-1-2实现了时间日期的显示,最初的做法是在直接弹出一个label显示日期时间。

最后把label 封装一个窗口类然后吧,该窗口加载到主窗口中最后显示成功。

图5-1-3图5-1-3是通过我的小伙伴编写好的日历窗口类放入我所编写的主窗口中最后达到了想要的结果。

没有实现动态设置日历的样式。

图5-1-4图5-1-4是通过我的小伙伴编写好的日历窗口类和数字时钟窗口类放入我所编写的主窗口的运行效果图,而数字时钟没有实现更改功能。

图5-1-5图5-1-5是从网络上查到的刻度时钟的执行样式,通过对其代码的编写和理解。

明白了其中的原理,是利用了计算机图形学的知识执行了不旋转变换并且利用了timer中的timeout 和timestat进行控制,实现时钟的动态显示。

图5-1-6图5-1-6 是实现了刻度表盘的主窗口,时钟的数字也是通过确定一块固定大小的像素块进行编写文本,然后把文本中的字符(数字)在该固定的像素块上显示,通过旋转变换实现数字在表盘上的绘制。

通过点画绘制非凹形的图形。

实现了表针的设置。

图5-1-7图5-1-7实现了对所有部件的组装和布局,个性化界面实现已经固定了。

六、实习日志七、实习总结八、附录:核心代码清单displayTime.h#ifndef DISPLAYTIME_H#define DISPLAYTIME_H#include<QWidget>#include<QLabel>class DisplayTime:public QLabel{Q_OBJECTpublic:DisplayTime();void DateSet(const QDateTime date);QDateTime DateGet();~DisplayTime();private slots:void updateTime();private:QDateTime*DateTime;};#endif//DISPLAYTIME_HdisplayTime.cpp#include"displayTime.h"#include<QTimer>#include<QDateTime>DisplayTime::DisplayTime():QLabel(){QTimer*timer=new QTimer;connect(timer,SIGNAL(timeout()),this,SLOT(updateTime()));timer->start(1000);DateSet(QDateTime::currentDateTime());updateTime();//timeLabel->show();}DisplayTime::~DisplayTime(){delete DateTime;}void DisplayTime::DateSet(const QDateTime date){DateTime=new QDateTime(date);}QDateTime DisplayTime::DateGet(){return*DateTime;}void DisplayTime::updateTime(){this->setText(QDateTime::currentDateTime().toString("今天是yyyy年MM月dd日--hh:mm:ss"));}shapedclock.h#ifndef SHAPEDCLOCK_H#define SHAPEDCLOCK_H#include<QWidget>#include<QTimer>#include<QTime>class ShapedClock:public QWidget{Q_OBJECTpublic:ShapedClock(QWidget*parent=0);QSize sizeHint()const;~ShapedClock();void setTime(const QTime);QTime getTime();public slots://void settTime(QTime t);private:QTime*time;protected:void paintEvent(QPaintEvent*event);};#endifshapedclock.cpp#include<QtWidgets>#include"shapedclock.h"ShapedClock::ShapedClock(QWidget*parent):QWidget(parent)//出现了一点问提{QTimer*timer=new QTimer(this);connect(timer,SIGNAL(timeout()),this,SLOT(update()));timer->start(1000);}void ShapedClock::paintEvent(QPaintEvent*){static const QPoint hourHand[4]={QPoint(7,8),QPoint(0,20),QPoint(-7,8),QPoint(0,-50)};static const QPoint minuteHand[4]={QPoint(7,8),QPoint(0,20),QPoint(-7,8),QPoint(0,-68),};static const QPoint secondHand[4]={QPoint(7,8),QPoint(0,20),QPoint(-7,8),QPoint(0,-83)};static const QPoint test[4]={QPoint(88,-2),QPoint(96,-2),QPoint(96,2),QPoint(88,2)};QTime*time=new QTime(QTime::currentTime());QColor hourColor(127,0,127);QColor minuteColor(0,127,127,191);QColor secondColor(0,127,127,80);int side=qMin(width(),height());QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);painter.translate(width()/2,height()/2);painter.scale(side/200.0,side/200.0);painter.setPen(Qt::NoPen);painter.setBrush(hourColor);painter.save();painter.rotate(30.0*((time->hour()+time->minute()/60.0)));painter.drawConvexPolygon(hourHand,4);painter.restore();painter.setPen(hourColor);for(int i=0;i<12;++i){if(i==0)painter.drawText(-20,-86,40,40,Qt::AlignHCenter|Qt::AlignTop,QString::number(12));else painter.drawText(-20,-86,40,40,Qt::AlignHCenter|Qt::AlignTop,QString::number(i));if(i%3==0){painter.drawConvexPolygon(test,4);}else painter.drawLine(88,0,96,0);painter.rotate(30.0);}painter.setPen(Qt::NoPen);painter.setBrush(minuteColor);painter.save();painter.rotate(6.0*(time->minute()+time->second()/60.0)); painter.drawConvexPolygon(minuteHand,4);painter.restore();//testpainter.setPen(Qt::NoPen);painter.setBrush(secondColor);painter.save();painter.rotate(6.0*(time->minute()*60+time->second())); painter.drawConvexPolygon(secondHand,4);painter.restore();//testpainter.setPen(minuteColor);for(int j=0;j<60;++j){if((j%5)!=0)painter.drawLine(92,0,96,0);painter.rotate(6.0);}}//void ShapedClock::setTime(QTime t=QTime::currentTime()){//*time=t;//}ShapedClock::~ShapedClock(){delete time;}void ShapedClock::setTime(const QTime T=QTime::currentTime()){ time=new QTime(T);}QTime ShapedClock::getTime(){return*time;}QSize ShapedClock::sizeHint()const{return QSize(200,200);}timeset.h#ifndef TIMESET_H#define TIMESET_H#include<QWidget>#include<QGroupBox>#include<QGridLayout>#include<QVBoxLayout>#include<QLabel>#include<QDateEdit>#include<QPushButton>#include<QVBoxLayout>#include<QHBoxLayout>#include<QTime>#include<windows.h>#include<QTimer>#include"shapedclock.h"class timeSet:public QWidget{Q_OBJECTpublic:timeSet(QWidget*parent,ShapedClock*clock);~timeSet();QSize sizeHint()const;void createClockGBox();//这里实现盒子和布局实现页面的框架函数public slots:bool ChangeTime();private:ShapedClock*clock;QGroupBox*ClockGBox;QTimeEdit*dateset;QPushButton*save;QPushButton*cancel;QGridLayout*ClockLayout;QGridLayout*PushButton;//在这里添加部};#endif//TIMESET_Htimeset.cpp#include<QtWidgets>#include"timeset.h"timeSet::timeSet(QWidget*parent,ShapedClock*oclock) :QWidget(parent){clock=oclock;createClockGBox();dateset->setDisplayFormat("HH:mm:ss");dateset->setTime(QTime::currentTime());QGridLayout*DateSetLayout=new QGridLayout;DateSetLayout->addWidget(ClockGBox,0,0);setWindowTitle("TimeSet Widget");setLayout(DateSetLayout);connect(save,SIGNAL(clicked()),this,SLOT(ChangeTime()));connect(cancel,SIGNAL(clicked()),this,SLOT(close()));}void timeSet::createClockGBox(){ClockGBox=new QGroupBox("时间设置");ClockLayout=new QGridLayout;clock=new ShapedClock;dateset=new QTimeEdit;save=new QPushButton;cancel=new QPushButton;save->setText("保存");cancel->setText("取消");PushButton=new QGridLayout;PushButton->addWidget(save,0,0);PushButton->addWidget(cancel,0,1);ClockLayout->addWidget(clock,0,0);ClockLayout->addWidget(dateset,1,0);ClockLayout->addLayout(PushButton,2,0);ClockGBox->setLayout(ClockLayout);}timeSet::~timeSet(){}//实现见面的框架函数bool timeSet::ChangeTime(){QTime datetimes;datetimes=dateset->time();SYSTEMTIME st;GetSystemTime(&st);st.wHour=datetimes.hour()-8;//st.wHour=10;st.wMinute=datetimes.minute();st.wSecond=datetimes.second();this->close();return SetSystemTime(&st);}QSize timeSet::sizeHint()const{return QSize(200,300);}mainWindow.h#ifndef MAINWINDOW_H#define MAINWINDOW_H#include<QWidget>#include<QGroupBox>#include<QGridLayout>#include<QVBoxLayout>#include<QLabel>#include<QAction>#include<QMenu>#include<QToolButton>#include<QPushButton>#include<setcalendar.h>#include<QCalendarWidget>#include<timeset.h>#include"shapedclock.h"#include"displayTime.h"class mainWindow:public QWidget {Q_OBJECTpublic:mainWindow(QWidget*parent=0);~mainWindow();public slots:void openCSet();void openTSet();private:void createDateGBox();void createCalendarGBox();void createClockGBox();void createDClockGBox();QGroupBox*DateGBox;QGroupBox*ClockGBox;QGroupBox*DClockGBox;QGroupBox*CalendarGBox;QVBoxLayout*DateLayout;QVBoxLayout*CalendarLayout;QVBoxLayout*ClockLayout;QVBoxLayout*DClockLayout;DisplayTime*date;ShapedClock*clock;QTimer*timer;//创建菜单QMenu*popMenu;QAction*addCalendarAction;QAction*addDateAction;QAction*addTimeAction;QAction*quitAction;Widget*win;timeSet*tSWin;QCalendarWidget*calendar; };#endif//mainWindow mainWindow.cpp#include<QWidget>#include<QMainWindow>#include<QCalendarWidget>#include"mainWindow.h"#include"digitalclock.h"#include"shapedclock.h"#include"displayTime.h"#include<QLabel>#include<QPalette>//调色#include<QFont>#include<QAction>#include<QMenu>#include<QPalette>mainWindow::mainWindow(QWidget*parent):QWidget(parent){//QPalette pal=palette();//pal.setColor(QPalette::Background,QColor(0x00,0xff,0x00,0x00)); //setPalette(pal);//setAttribute(Qt::WA_TranslucentBackground,true);//setWindowOpacity(0);calendar=new QCalendarWidget;timer=new QTimer;win=new Widget(calendar);setContextMenuPolicy(Qt::ActionsContextMenu);quitAction=new QAction(tr("退出"),this);addAction(quitAction);connect(quitAction,SIGNAL(triggered()),this,SLOT(close()));addDateAction=new QAction(tr("日历设置"),this);addAction(addDateAction);connect(addDateAction,SIGNAL(triggered()),this,SLOT(openCSet()));addTimeAction=new QAction(tr("时间设置"),this);addAction(addTimeAction);connect(addTimeAction,SIGNAL(triggered()),this,SLOT(openTSet()));createDateGBox();createCalendarGBox();createDClockGBox();createClockGBox();QPalette*palette=new QPalette(this->palette());palette->setColor(QPalette::Background,QColor(246,246,246,255));this->setPalette(*palette);QGridLayout*mainLayout=new QGridLayout;mainLayout->addWidget(DateGBox,0,0,1,2);mainLayout->addWidget(CalendarGBox,1,0,2,1);mainLayout->addWidget(ClockGBox,1,1);mainLayout->addWidget(DClockGBox,2,1);setWindowTitle("Calendar Widget");setLayout(mainLayout);}void mainWindow::createDateGBox(){DateGBox=new QGroupBox("Date");DateLayout=new QVBoxLayout;//QLabel*date=new QLabel;//date->setText("fdsfsdfsdfsf");date=new DisplayTime;QFont textFont=date->font();textFont.setBold(true);textFont.setPointSize(20);textFont.setFamily("楷体");date->setFont(textFont);DateLayout->addWidget(date);DateGBox->setLayout(DateLayout);}void mainWindow::createCalendarGBox(){CalendarGBox=new QGroupBox("Calendar");CalendarLayout=new QVBoxLayout;calendar->setGridVisible(true);calendar->setStyleSheet("color:blue;""background-color:;""selection-color:red;""selection-background-color:pink;");CalendarLayout->addWidget(calendar);CalendarGBox->setLayout(CalendarLayout);}void mainWindow::createClockGBox(){ClockGBox=new QGroupBox("ClockGBox");ClockLayout=new QVBoxLayout;clock=new ShapedClock;ClockLayout->addWidget(clock);ClockGBox->setLayout(ClockLayout);}void mainWindow::createDClockGBox(){DClockGBox=new QGroupBox("DClock");DClockLayout=new QVBoxLayout;DigitalClock*clock=new DigitalClock;DClockLayout->addWidget(clock);DClockGBox->setMaximumHeight(100);DClockGBox->setMaximumWidth(400);DClockGBox->setLayout(DClockLayout);}void mainWindow::openCSet(){//QLabel*test=new QLabel;win->show();}void mainWindow::openTSet(){//QLabel*test=new QLabel;tSWin=new timeSet(0,clock);tSWin->show();}QSize QCalendarWidget::sizeHint()const{return QSize(400,400);}mainWindow::~mainWindow(){}main.cpp#include"mainWindow.h"#include<QApplication>int main(int argc,char*argv[]){QApplication app(argc,argv);mainWindow w;w.show();return app.exec();}排版格式要求1.页面设置纸张用A4,上下均为2.5厘米,左为2.8厘米,右为2.2厘米;页眉2厘米,页脚1厘米。

相关文档
最新文档