Qt Creater开发万年历(C++).docx

上传人:太** 文档编号:97713476 上传时间:2024-06-24 格式:DOCX 页数:24 大小:34.43KB
返回 下载 相关 举报
Qt Creater开发万年历(C++).docx_第1页
第1页 / 共24页
Qt Creater开发万年历(C++).docx_第2页
第2页 / 共24页
点击查看更多>>
资源描述

《Qt Creater开发万年历(C++).docx》由会员分享,可在线阅读,更多相关《Qt Creater开发万年历(C++).docx(24页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、1 .Calendar.cpp#include #include calendar.h#include #include #include #include #include Hitembutton.hn#include nlunarday.hn using namespace lunar;Calendar: :Calendar(QWidget parent) :QWidget(parent)(当前年份QDateTime dt;QTime time;QDate date;dt.setTime(time.currentTime();dt.setDate(date.currentDate();QS

2、tring currentDate = dt.toString(nyyyy:MM:dd:hh:mm:ssH);QStringList currentDateList = currentDate. split。:);newyear=currentDateList.at(0).toInt();newmonth=currentDateList.at( 1 ).tolnt();newday=currentDateList.at(2).toInt();m_bgBlackPixmap = QPixmap:fromImage(QImage(tr(n./images/cumputerback.pngn);/m

3、_barPixmap= QPixmap:fromImage(QTmage(tr(,./images/bar.png,);/m_bgPixmap = QPixmap:fromImage(QImage(tr(n./images/calendarback.pngn);/退出按钮m_btnQuit = new QLabel(this);QPixmap quitPixmap = QPixmap:fromImage(QImage(n./images/quit.pngn);/setPixmap设置图片,m_btnQuit-setPixmap(quitPixmap);设置大小m_btnQuit-resize(

4、78, 65);移动m_btnQuit-move(800 - 78, 0);int tempyear=newyear-1901;int m=tempyear/4;int n=tempyear%4;double lunartempday=(l4*m)+( 10.6*( 1 +n);for(int i=0;inewmonth-1 ;i+)(lunartempday4-=ai;)for(int var = 0; var deleteLater();lundarweek = NULL;m_l i stWeekltem .removeAt(var);var;)for (int var = 0; var

5、deleteLater();week = NULL;m_listWeekItem.removeAt(var);var;)int newday= 1;for(int m=0;m6;m+)(for(int n=0;n7;n+)(if(m=0&nsetTextColor(Qt: white); week-resize(83, 40);wcck-sctRcct(QRcct(0,0,83, 40);week-setAlignment(Qt:AlignCenter);week-setTextFont(QFont(宋体,15); week-setText(tr(n H);week-update();week

6、-move(210+83*n/80+40*m);week-show();m_listWeekItem.append(week);)elselunartempday=lunartempday+1;int lunardayl=(lunartempdaylO);lunarday=lunardayl%295;if(lunarday200)(lunarday=lunarday/l 0;)else if(lunarday100)(lunarday=lunarday/10;)else if(lunarday10)lunarday=lunarday/10;elselunarday=30;QString new

7、date=QString:number(newday, 10);itemButton *week= new itemButton(this);week-setTextColor(Qt: white);week-resize(83, 40);week-setRect(QRect(0,0,83, 40);week-setAlignment(Qt:AlignCenter);week-setTextFont(QFont(宋体,15);week-setText(newdate);week-update();week-move(210+83*n/80+40*m);week-show();m_listWee

8、kItem.append(week);itemButton *lundarweek= new itemButton(this);lundarweek-setTextColor(Qt: white);lundarweek-resize(83, 40);lundarweek-setRect(QRect(25,0,83, 40);lundarwcck-sctAlignmcnt(Qt:AlignCcntcr);lundarweek-setTextFont(QFont(宋体,13);lundarweek-setText(blunarday-l);lundarweek-update();lundarwee

9、k-move(210+83*n, 180+40*m);lundarweek-show();m_listWeekItem.append(lundarweek);newday+;)if(newday=currentDateList.at(2).toInt()(/templunar=blunarday-l ;)if(newdayanewmonth-l)(return;)bool Calendar:isleap(int newyear)(if(newyear%4=0 & newyear% 100 !=0) | (newyear%4=0 & newyear%400=0) return true;else

10、 return false;)int Calendar: :getDay s()(int a12=31,28,31,30,31,30,31,31,30,31,30,31);inti;int sumDay=l;for(i=0;inewyear;i+)(if(isleap(i) sumDay+=366;else sumDay+=365;)if(islcap(ncwycar) a 1=29;else al=28;for(i=0;i(newmonth-1 );i+)sumDay=sumDay-i-ai;return sumDay;)int Calendar:getWeek()(int newweek=

11、getDays()%7;new week二new week+5;newweek=newweek%7;return newweek;)void Calendar:paintEvent(QPaintEvent *event)(Q_UNUSED(event);QPainter painter(this);/QWidget绘制图片,先绘制的在底层,后绘制的在上层 painter.drawPixmap(O, 0, 800, 480, m_bgBlackPixmap);painter.drawPixmap(O, 0, 800, 480, m_bgPixmap);painter.drawPixmap(0,0

12、,800,68,m_barPixmap);)#include itembutton.hftinclude ftinclude ftinclude itemButton:itemButton(QWidget *parent):QWidget(parent),m_textColor(Qt:white),m_alignment(Qt:AlignLeft),m isLight(false)(void itemButton:刀方方(QPaintEvent *event)Q_UNUSED(event);quint64 nTime = QDateTime:currentMSecsSinceEpoch();Q

13、Painter painter(this);if(!m_isLight) if(!m_bgPixmap. isNull () painter. drawPixmap(m_imageRect, m bgPixmap);1 else if(Im bgPixmapS. isNull() painter. drawPixmap(m_imageRect, m bgPixmapS);! )if(Im strText. isEmpty() QPen pen;pen. setColor(m_textColor);painter. setPen (pen);painter. setFont(m_textFont

14、);painter. drawText (m workRect, m alignment, m strText);1 qDebugO _FUNCTI0N_ _LINE_ QDateTime:currentMSecsSinceEpoch () - nTime; )ftinclude ftinclude z/ma inwindow. hftinclude calendar. hftinclude ftinclude ftinclude int main(int argc, char *argv) QApplication a(argc, argv);QTextCodec: : setCodecFo

15、rCStrings (QTextCodec: :codecForName (/ZGB2312/Z) ;/ / 1QTextCodec: :setCodecForLocale(QTextCodec: :codecForName(/GB2312/) ;/ 2QTextCodec:setCodecForTr(QTextCodec:codecForName(,ZGB2312/Z) ;/ 3如上三句是关键语句Calendar *cld二new Calendar;cld-setGeometry (QRect (250, 250, 350, 250);设置窗口显示方位与大 小cld-setWindowFla

16、gs (Qt: :FramelessWindowHint);隐藏标题框 cld-rili ();/cld-getbackday();cld-resize(800, 480);cld-show();return a. exec ();)ftifndef ITEMBUTT0N_Hdefine ITEMBUTTON_Hftinclude /* brief实现贴图,写字的功能*/class itemButton : public QWidget (Q_0BJECTpublic:explicit itemButton(QWidget parent = 0);/* brief设置字体颜色* /inline

17、 void setTextColor(const QColor &textColor) mtextColor = textColor;)/* brief设置字体对齐方向* /inline void setAlignment(const Qt:Alignment &alignment) m_alignment = alignment;)/* brief设置字体的位置* param rect. x -针对控件的x的值* /inline void setTextRect (const QRect &rect) m_workRect = rect;)一/* brief设置显示的字符串*/inline

18、void setText (const QString &text) mstrText = text;1一/* brief设置字体大小inline void setTextFont (const QFont &font) m_textFont = font;1 /vt* xL*sL*kL* xL* *JL* kJ kJ xL*Jt* *X* *JL*kL* kL* *JL* *JL* *Jx kJ/ XIX Xrx xjx ZTX XTX XT XTX xjx Zjx xjx X7X XTX XTX XjX Tx XjX XgX XTX XTX ZjX XjX XjX XjX XTX XTX

19、zlx xjx X7X jX XTX XTX XjX Tx XjX XgX XTX xTx XTX X7X* 一下操作是图片操作KLZ KLZ vtz kIxsJ vlz vtzkLzsXz sXkLz/jx XT% XlX X|X XjX XjX XTX XlX XjXXlX ZlX XTx *TX XjX *TX xjs ZlX XjX X|X TX #TX XjX XtXT Tx r T* *TS /inline void setBGImage(const QPixmap &pixmap)mbgPixmap = pixmap;)inline void setBGImage(const Q

20、String &strPixmap) this-setBGImage(QP i xmap:fromlmage(Qlmage(strPixmap); )/* brief设置选中之后的图片*/inline void setBGImageS(const QPixmap &pixmap) m_bgPixmapS = pixmap;1 一inline void setBGImageS(const QString &strPixmap) thi s-setBGImageS(QPixmap:fromlmage(Qlmage(strPixmap);)inline void setRect (const QRe

21、ct &rect) mimageRect = rect; this-setTextRect(rect);inline void setLightScene(const bool &isLight) misLight = isLight;this-update();)protected:void painre/?(QPaintEvent *event);private:QString m_strText;QColor m_textColor;QFont m_textFont;Qt:Alignment malignment;QRect mworkRect;bool misLight;QRect m

22、_iinageRect;QPixmap m bgPixmap;QPixmap m bgPixmapS;QStringList m_templist;signals:public slots:);ttendif / ITEMBUTTON JI5.calendar, hftifndef CALENDARdefine CALENDARSftinclude ftinclude /zitembutton. h/zftinclude class QWidget;class QPushButton;class Calendar :public QWidget (public:Calendar (QWidge

23、t parent=0);void rili ();bool isleap(int newyear);获取闰年int getWeek。;返回星期int getDays();返回从0开始总天数;/void getbackday(); 返I回农历日期 protected:/* brief QWidget 绘制背景*/void paintEvent(QPaintEvent *event);void 勿ous67Yesspe/(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);void 勿方(QMouseEvent *event);p

24、rivate:void onKeyYearreduce();void lightScene ();bool isFocus (const QPoint &point, QWidget *button); private slots:取年份和月份private:QLabel *m_btnQuit; /退出QLabel *yearadd; 向上调年份QLabel *yearreduce;向下调年份QLabel *monthadd; /向上调月份QLabel *monthreduce;向下调月份QLabel *Box;itemButton *m_gr egori an; 公历itemButton *

25、m_btncalendar;万年历itemButton itemButton itemButton itemButton itemButton itemButton itemButton*m_lunar; 农历*m_ge tback; 返 回今日*year; 年份*month; 月份*week; 星期*gregoriandisplay;*lunardisplay;QList mlistWeekltem;QPixmap mbarPixmap;QPixmap m bgPixmap;QPixmap m bgBlackPixmap;int int int int int int int int int

26、 intnewyear; 变动后的年份 newmonth; 变动后的月份 newday; 天数 lunaryear; / 农历年 lunarmonth; / 农历月 lunarday; / 农历日 neweek; /星期几 a;m_i;m_j;QString b;);ttendif / CALENDARJI6.工程文件 Calendar.pro# Project created by QtCreator 2013-10-18T10:24:53 #ttQT+= core guiTARGET = calendarTEMPLATE = appSOURCES += main. cpp mainwind

27、ow. cpp calendar, cpp itembutton, cppHEADERS += mainwindow. h calendar, h itembutton, h C:/Documents and Settings/Administrator/dada/lunarday. h FORMS += mainwindow. ui OTHER FILES += right_three. png right_S. png quithigh. png quit, png left_three. png calendarback. png bar. png stand_light. png ri

28、ght_clicked. png right, png quit_clicked. png left_clicked. png left, png calendar_fini shed, png1 .以上代码图片名和图片路径仅供参考,2 .农历算法稍有误差,下面给出正确农历算法自己组装代码,lunarday.h#ifndefLUNARDAY_H#define LUNARDAY.H/file: lunarday.h/author: /2007-02-13 11:22:04/url:namespace lunar (/保存一个农历日期typedef struct T_Date ( 年int yea

29、r;/Box = new QLabel(this);/QPixmap BoxPixmapl 二 QPixmap:fromImage(QImage(,./images/stand_lightM);/Box-setPixmap(BoxPixmap 1);/Box-resize(58,48);/qDebug()m_im_j;/Box-move(210+83*m_i,180+40*m);/Box-show();向下年份按钮yearreduce = new QLabel(this);QPixmap leftPixmapl = QPixmap:fromImage(QImage(,./images/left

30、.png,);yearreduce-setPixmap(leftPixmap 1);yearreduce-resize(54,54);yearreduce-move(230,80);向下月份按钮monthreduce = new QLabel(this);QPixmap leftPixmap2 = QPixmap:fromImage(QImage(n./images/left.pngn);monthreduce-setPixmap(leftPixmap2);monthreduce-resize(54,54);monthreduce-move(530,80);向上年份按钮yearadd = ne

31、w QLabel(this);QPixmap rightPixmapl = QPixmap:fromImage(QImage(./images/right.pngn);yearadd-setPixmap(rightPixmap 1);yearadd-resize(54,54);yearadd-move(435,80);向上月份按钮monthadd = new QLabel(this);QPixmap rightPixmap2 = QPixmap:fromImage(QImage(./images/right.pngn);monthadd-setPixmap(rightPixmap2);mont

32、hadd-resize(54,54);monthadd-move(730,80);设置公历m_gregorian = new itemButton(this);m_gregorian-setTextColor(Qt:white);m_gregorian-resize( 176, 49);m_gregorian-setRect(QRect(0,0,176, 49);m_gregorian-setAlignment(Qt:AlignLeft);月int month;日int day;是否闰月bool leap; Date;/公历转农历Date LuanrDate(int solar_year,in

33、t solar_month,int solar_day); )#endif / LUNARDAY_Hlunarday.cpp#include nlunarday.hH#include #include #include #include /file: lunarday.cpp/author: /2007-02-13 11:22:48公历转农历using namespace std;using lunar:Date;/使用比特位记录每年的情况04共5bit春节日份56共2bit春节月份719共13bit 13个月的大小月情况(如果无闰月,最后位无效),大月为1,小月为02。23共4bit记录闰月

34、的月份,如果没有闰月为0static const int BEGIN_YEAR = 1901;static const int NUMBER_YEAR = 199;static const unsigned int LUNAR_YEARS 199 = 0x04AE53,0x0A5748,0x5526BD,0x0D2650,0x0D9544,0x46AAB9,0x056A4D,0x09AD42,0x24AEB6,0x04AE4A, /1901 -19100x6A4DBE,0x0A4D52,0x0D2546,0x5D52BA,0x0B544E,0x0D6A43,0x296D37,0x095B4B,

35、0x749BC 1,0x049754, /1911-19200x0A4B48,0x5B25BC,0x06A550,0x06D445,0x4ADAB8,0x02B64D,0x095742,0x2497B7,0x04974A,0x664B3E,/1921-19300x0D4A51,0x0EA546,0x56D4BA,0x05AD4E,0x02B644,0x393738,0x092E4B,0x7C96BF,0x0C9553,0x0D4A48,/193M9400x6DA53B,0x0B554F,0x056A45,0x4AADB9,0x025D4D,0x092D42,0x2C95B6,0x0A954A,

36、0x7B4ABD,0x06CA51,/1941-19500x0B5546,0x555ABB,0x04DA4E,0x0A5B43,0x352BB8,0x052B4C,0x8A953F,0x0E9552,0x06AA48,0x7AD53C,/1951-l 9600x0AB54F,0x04B645,0x4A5739,0x0A574D,0x052642,0x3E9335,0x0D9549,0x75AABE,0x056A5l,0x096D46, /1961-19700x54AEBB,0x04AD4F,0x0A4D43,0x4D26B7,0x0D254B,0x8D52BF,0x0B5452,0x0B6A4

37、7,0x696D3C,0x095B50,/197M9800x049B45,0x4A4BB9,0x0A4B4D,0xAB25C2,0x06A554,0x06D449,0x6ADA3D,0x0AB651,0x093746,0x5497BB, /1981 -19900x04974F,0x064B44,0x36A537,0x0EA54A,0x86B2BF,0x05AC53,0x0AB647,0x5936BC,0x092E50,0x0C9645, /1991 -20000x4D4AB8,0x0D4A4C,0x0DA541,0x25AAB6,0x056A49,0x7AADBD,0x025D52,0x092

38、D47,0x5C95BA,0x0A954E,/2001-2010 0x0B4A43,0x4B5537,0x0AD54A,0x955ABF,0x04BA53,0x0A5B48,0x652BBC,0x052B50,0x0A9345,0x474AB9, /2011-20200x06AA4C,0x0AD541,0x24DAB6,0x04B64A,0x69573D,0x0A4E51,0x0D2646,0x5E933A,0x0D534D,0x05AA43, /2021 -20300x36B537,0x096D4B,0xB4AEBF,0x04AD53,0x0A4D48,0x6D25BC,0x0D254F,0

39、x0D5244,0x5DAA38,0x0B5A4C, /2031 -20400x056D41,0x24ADB6,0x049B4A,0x7A4BBE,0x0A4B51, 0x0AA546,0x5B52BA,0x06D24E,0x0ADA42,0x355B37,/2041 -20500x09374B,0x8497Cl,0x049753,0x064B48,0x66A53C,0x0EA54F,0x06B244,0x4AB638,0x0AAE4C,0x092E42,/2051 -20600x3C9735,0x0C9649,0x7D4ABD,0x0D4A5 l,0x0DA545,0x55AABA,0x05

40、6A4E,0x0A6D43,0x452EB7,0x052D4B,/2061-20700x8A95BF,0x0A9553,0x0B4A47,0x6B553B,0x0AD54F,0x055A45,0x4A5D38,0x0A5B4C,0x052B42,0x3A93B6, /2071 -20800x069349,0x7729BD,0x06AA5 1,0x0AD546,0x54DABA,0x04B64E,OxOA5743,0x452738,0x0D264A,0x8E933E,/2081-20900x0D5252,0x0DAA47,0x66B53B,0x056D4F,0x04AE45, 0x4A4EB9,

41、0x0A4D4C,0x0D 1541,0x2D92B5 /2091 -2099);计算这个公历日期是一年中的第几天static int DayOfSolarYear(int year, int month, int day )(为了提高效率,记录每月一日是一年中的第几天static const int NORMAL_YDAY 12 = 1,32,60,91/21,152, 182,213,244,274,305,335;闰年的情况static const int LEAP_YDAY12 = 1,32,61,92,122,153,183,214,245,275,306,336;const int

42、 *t_year_yday_ = NORMAL_YDAY;判断是否是公历闰年if(year%100 !=0)t_year_yday_ = LEAP_YDAY;if(year%400 = 0)t_year_yday_ = LE AP_YD AY;)return t_year_yday_month -1 + (day -1);)Date lunar:LuanrDate(int solar_year,int solar_month,int solar_day) (Date luanr_date ;luanr_date.year 二 solar_year;luanr date.month = 0;lu

43、anr_date.day = 0;luanr_date.leap = false;/越界检查,如果越界,返回无效日期if(solar_year BEGIN.YEAR + NUMBER_YEAR - 1 ) return luanr_date;int year_index 二 solar_year - BEGIN_YEAR;/计算春节的公历日期int spring_ny_month =(LUNAR_YEARSyear_index & 0x60 ) 5;int spring_ny_day = ( LUNAR_YEARS year_index & Ox If);/计算今天是公历年的第几天int today_solar_yd = DayOfSolarYear(solar_year,solar_month,solar_d

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 解决方案

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁