Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Qt Lunar Calendar Control

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to realize Qt Lunar Calendar Control". In daily operation, I believe many people have doubts about how to realize Qt Lunar Calendar Control. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to realize Qt Lunar Calendar Control". Next, please follow the editor to study!

I. Preface

Lunar calendar control is one of the necessary controls in domestic linux. After all, you have to adapt to the habits of Chinese people. If you look at the calendar of win10 system, there is lunar calendar directly on it now, which is very convenient and user-friendly. So in many projects done with Qt, there are also application scenarios of lunar calendar controls, while the calendar controls that Qt comes with are relatively simple. People who have carefully read the source code only know that they are actually composed of a bunch of fine-tuning boxes, drop-down boxes and tables. So I intend to use this method to create a lunar calendar control. The algorithm of this control is provided by Ni Xiaxia, and there is no problem in personal testing. The biggest difficulty in building this lunar calendar control is how to calculate the lunar calendar time according to the date. Coupled with some solar terms of the lunar calendar, and so on, this online estimate also has a lot of algorithm reference.

Main functions:

Border color / weekend color / corner color / lunar festival color can be set

You can set current month text color / other month text color / selected date text color / hover date text color

You can set the current month lunar text color / other lunar text color / selected date lunar text color / hover date lunar text color

You can set current month background color / other month background color / selected date background color / hover date background color

Three selected background modes can be set: rectangular background + circular background + picture background.

You can switch directly to the previous year / next year / last month / next month / to today

Can be set whether to display lunar calendar information, if not, it can be used as a normal calendar

Support the range from 1900 to 2099

It is convenient to change it to multiple dates.

2. Code idea void LunarCalendarItem::paintEvent (QPaintEvent *) {/ / preparation for drawing, enable anti-aliasing QPainter painter (this); painter.setRenderHints (QPainter::Antialiasing | QPainter::TextAntialiasing); / / draw background and frame drawBg (& painter); / / draw selected state first, and then draw hover state if (select) {drawBgCurrent (& painter, selectBgColor) } else if (hover) {drawBgCurrent (& painter, hoverBgColor);} / draw date drawDay (& painter); / / draw lunar calendar information drawLunar (& painter);} void LunarCalendarItem::drawBg (QPainter * painter) {painter- > save (); / / choose the corresponding color QColor bgColor = currentBgColor; if (dayType = DayType_MonthPre | | dayType = = DayType_MonthNext) {bgColor = otherBgColor according to the current type } painter- > setPen (borderColor); painter- > setBrush (bgColor); painter- > drawRect (rect ()); painter- > restore ();} void LunarCalendarItem::drawBgCurrent (QPainter * painter, const QColor & color) {int width = this- > width (); int height = this- > height (); int side = qMin (width, height); painter- > save (); painter- > setPen (Qt::NoPen); painter- > setBrush (color) / draw background style if (selectType = = SelectType_Rect) {painter- > drawRect (rect ());} else if (selectType = = SelectType_Circle) {int radius = side / 2-3; painter- > drawEllipse (QPointF (width / 2, height / 2), radius, radius);} else if (selectType = SelectType_Triangle) {int radius = side / 3; QPolygon pts Pts.setPoints (3, 1, 1, radius, 1, 1, radius); painter- > drawRect (rect ()); painter- > setBrush (superColor); painter- > drawConvexPolygon (pts);} else if (selectType = = SelectType_Image) {/ / Center QImage img (bgImage) If (! img.isNull ()) {img = img.scaled (this- > size (), Qt::KeepAspectRatio, Qt::SmoothTransformation); int x = (width-img.width ()) / 2; int y = (height-img.height ()) / 2; painter- > drawImage (x, y, img);} painter- > restore () } void LunarCalendarItem::drawDay (QPainter * painter) {int width = this- > width (); int height = this- > height (); int side = qMin (width, height); painter- > save (); / / choose the corresponding color QColor color = currentTextColor; if (dayType = = DayType_MonthPre | | dayType = = DayType_MonthNext) {color = otherTextColor;} else if (dayType = DayType_WeekEnd) {color = weekColor } if (select) {color = selectTextColor;} else if (hover) {color = hoverTextColor;} painter- > setPen (color); if (showLunar) {QFont font; font.setPixelSize (side / 2.7); painter- > setFont (font); QRect dayRect = QRect (0,0, width, height / 1.7) Painter- > drawText (dayRect, Qt::AlignHCenter | Qt::AlignBottom, QString::number (date.day ();} else {QFont font; font.setPixelSize (side / 2); painter- > setFont (font); QRect dayRect = QRect (0,0, width, height); painter- > drawText (dayRect, Qt::AlignCenter, QString::number (date.day ());} painter- > restore () } void LunarCalendarItem::drawLunar (QPainter * painter) {if (! showLunar) {return;} int width = this- > width (); int height = this- > height (); int side = qMin (width, height); painter- > save () / / to determine whether the current text of the lunar calendar is a festival, if it is a festival and the current month, then bool exist = (! listDayName.contains (lunar) & & dayType! = DayType_MonthPre & & dayType! = DayType_MonthNext); / / choose the corresponding color QColor color = currentLunarColor; if (dayType = = DayType_MonthPre | | dayType = = DayType_MonthNext) {color = otherLunarColor according to the current type } if (select) {color = selectTextColor;} else if (hover) {color = hoverTextColor;} else if (exist) {color = lunarColor;} painter- > setPen (color); QFont font; font.setPixelSize (side / 5); painter- > setFont (font); QRect lunarRect (0, height / 2, width, height / 2); painter- > drawText (lunarRect, Qt::AlignCenter, lunar) Painter- > restore ();} III. Effect diagram

At this point, the study on "how to implement the Qt lunar calendar control" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report