当前位置: 首页 > news >正文

Qt日历控件示例-QCalendarWidget

基本说明

QCalendarWidget介绍:
QCalendarWidget 是 Qt 框架中提供的一个日期选择控件,用户可以通过该控件快速选择需要的日期,并且支持显示当前月份的日历。
这里,我们继承了QCalendarWidget,做了一些简单封装和样式调整

1.使用的IDE: QtCreator;
2.qt 版本:Desktop Qt 5.15.2 MSVC2015 64bit
3.效果图:
在这里插入图片描述

代码

TCalendarWidget.h

#ifndef TCALENDARWIDGET_H
#define TCALENDARWIDGET_H#include <QCalendarWidget>class QPushButton;
class QLabel;class TCalendarWidget : public QCalendarWidget
{Q_OBJECTpublic:TCalendarWidget(QWidget *parent = 0);~TCalendarWidget();void SetHighlightDate(QList<QDate> lstDate);private:void InitControl();void InitTopWidget();void SetDataLabelTimeText(int year, int month);signals:void SignalSetCalendarTime(const QDate& data);private slots:void SlotBtnClicked();protected:void paintCell(QPainter *painter, const QRect &rect, const QDate &date) const;private:QPushButton     *m_pBtnLeftYear;QPushButton     *m_pBtnLeftMonth;QPushButton     *m_pBtnRightYear;QPushButton     *m_pBtnRightMonth;QLabel          *m_pLblDate;QList<QDate>     m_lstHighlightDate;
};#endif //_T_PROPERTY_H_

TCalendarWidget.cpp

#pragma execution_character_set("utf-8")
#include "TCalendarWidget.h"#include <QLocale> 
#include <QPainter>
#include <QTextCharFormat>
#include <QProxyStyle>
#include <QTableView>
#include <QLayout>
#include <QPushButton>
#include <QLabel>class QCustomStyle : public QProxyStyle
{
public:QCustomStyle(QWidget *parent) {setParent(parent);};private:void drawPrimitive(PrimitiveElement element, const QStyleOption *option,QPainter *painter, const QWidget *widget) const{if (element == PE_FrameFocusRect){return;}QProxyStyle::drawPrimitive(element, option, painter, widget);}
};TCalendarWidget::TCalendarWidget(QWidget *parent): QCalendarWidget(parent)
{InitControl();
}TCalendarWidget::~TCalendarWidget()
{}void TCalendarWidget::SetHighlightDate(QList<QDate> lstDate)
{m_lstHighlightDate = lstDate;updateCells();
}void TCalendarWidget::InitControl()
{layout()->setSizeConstraint(QLayout::SetFixedSize);setLocale(QLocale(QLocale::Chinese));setNavigationBarVisible(false);setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);setHorizontalHeaderFormat(QCalendarWidget::SingleLetterDayNames);setStyle(new QCustomStyle(this));QTextCharFormat format;format.setForeground(QColor("#FFFFFF"));format.setBackground(QColor(27, 33, 43));setHeaderTextFormat(format);setWeekdayTextFormat(Qt::Saturday, format);setWeekdayTextFormat(Qt::Sunday, format);setWeekdayTextFormat(Qt::Monday, format);setWeekdayTextFormat(Qt::Tuesday, format);setWeekdayTextFormat(Qt::Wednesday, format);setWeekdayTextFormat(Qt::Thursday, format);setWeekdayTextFormat(Qt::Friday, format);InitTopWidget();connect(this, &QCalendarWidget::currentPageChanged, [this](int year, int month) {SetDataLabelTimeText(year, month);});
}void TCalendarWidget::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const
{bool bHightlight = false;foreach (QDate date1,m_lstHighlightDate){if (date1 == date){bHightlight = true;}}if (date == selectedDate()){painter->save();painter->setRenderHint(QPainter::Antialiasing);painter->setPen(QColor("#1B212B"));painter->setBrush(QColor("#1B212B"));painter->drawRect(rect);painter->setPen(QColor("#2678D5"));painter->setBrush(QColor("#264974"));painter->drawRoundedRect(rect.x() + 6, rect.y() + 2, 24, 24, 2, 2);painter->setPen(bHightlight?QColor("#2678D5"): QColor("#FFFFFF"));painter->drawText(rect, Qt::AlignCenter, QString::number(date.day()));painter->restore();}else if (date == QDate::currentDate()){painter->save();painter->setRenderHint(QPainter::Antialiasing);painter->setPen(QColor("#1B212B"));painter->setBrush(QColor("#1B212B"));painter->drawRect(rect);painter->setPen(QColor("#2678D5"));painter->setBrush(Qt::NoBrush);painter->drawRoundedRect(rect.x()+6, rect.y()+2, rect.width()-12, rect.height()-4, 2, 2);painter->setPen(bHightlight ? QColor("#2678D5") : QColor("#FFFFFF"));painter->drawText(rect, Qt::AlignCenter, QString::number(date.day()));painter->restore();}else if (date < minimumDate() || date > maximumDate()){painter->save();painter->setRenderHint(QPainter::Antialiasing);painter->setPen(Qt::NoPen);painter->setBrush(QColor(249, 249, 249));painter->drawRect(rect.x(), rect.y() + 3, rect.width(), rect.height() - 6);painter->setPen(QColor("#3D4E5E"));painter->drawText(rect, Qt::AlignCenter, QString::number(date.day()));painter->restore();}else{painter->save();painter->setRenderHint(QPainter::Antialiasing);painter->setPen(QColor("#1B212B"));painter->setBrush(QColor("#1B212B"));painter->drawRect(rect);painter->setPen(bHightlight ? QColor("#2678D5") : QColor("#FFFFFF"));painter->drawText(rect, Qt::AlignCenter, QString::number(date.day()));painter->restore();}
}void TCalendarWidget::InitTopWidget()
{QWidget* pTopWidget = new QWidget(this);pTopWidget->setObjectName("CalendarTopWidget");pTopWidget->setFixedHeight(36);pTopWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);QHBoxLayout* pHBoxLayout = new QHBoxLayout;pHBoxLayout->setContentsMargins(20, 0, 20, 0);pHBoxLayout->setSpacing(10);m_pBtnLeftYear = new QPushButton(this);m_pBtnRightYear = new QPushButton(this);m_pBtnLeftMonth = new QPushButton(this);m_pBtnRightMonth = new QPushButton(this);m_pLblDate = new QLabel(this);m_pBtnLeftYear->setObjectName("CalendarLeftYearBtn");m_pBtnRightYear->setObjectName("CalendarRightYearBtn");m_pBtnLeftMonth->setObjectName("CalendarLeftMonthBtn");m_pBtnRightMonth->setObjectName("CalendarRightMonthBtn");m_pLblDate->setObjectName("CommonTextWhite14");pHBoxLayout->addWidget(m_pBtnLeftYear);pHBoxLayout->addWidget(m_pBtnLeftMonth);pHBoxLayout->addStretch();pHBoxLayout->addWidget(m_pLblDate);pHBoxLayout->addStretch();pHBoxLayout->addWidget(m_pBtnRightMonth);pHBoxLayout->addWidget(m_pBtnRightYear);pTopWidget->setLayout(pHBoxLayout);QVBoxLayout *vBodyLayout = qobject_cast<QVBoxLayout *>(layout());vBodyLayout->insertWidget(0, pTopWidget);connect(m_pBtnLeftYear, SIGNAL(clicked()), this, SLOT(SlotBtnClicked()));connect(m_pBtnLeftMonth, SIGNAL(clicked()), this, SLOT(SlotBtnClicked()));connect(m_pBtnRightYear, SIGNAL(clicked()), this, SLOT(SlotBtnClicked()));connect(m_pBtnRightMonth, SIGNAL(clicked()), this, SLOT(SlotBtnClicked()));SetDataLabelTimeText(selectedDate().year(), selectedDate().month());
}void TCalendarWidget::SetDataLabelTimeText(int year, int month)
{m_pLblDate->setText(QString("%1年%2月").arg(year).arg(month));
}void TCalendarWidget::SlotBtnClicked()
{QPushButton *senderBtn = qobject_cast<QPushButton *>(sender());if (senderBtn == m_pBtnLeftYear){showPreviousYear();}else if (senderBtn == m_pBtnLeftMonth){showPreviousMonth();}else if (senderBtn == m_pBtnRightYear){showNextYear();}else if (senderBtn == m_pBtnRightMonth){showNextMonth();}
}

样式:

在这里插入图片描述

QString Dialog::GetQss()
{QString str = " QPushButton{font-family: \"Microsoft YaHei\";border:none;background:transparent;}\\QWidget#CalendarTopWidget \{ \background: #1B212B; \border:none; \border-bottom: 1px solid #45596B; \} \\QPushButton#CalendarLeftYearBtn \{ \max-height:16px; \min-height:16px; \max-width:16px; \min-width:16px; \image: url(STYLESHEET_PIC_PATH/common/year_last_nor.png); \} \\QPushButton#CalendarLeftYearBtn:hover \{ \image: url(STYLESHEET_PIC_PATH/common/year_last_down.png); \} \\\QPushButton#CalendarRightYearBtn \{ \max-height:16px; \min-height:16px; \max-width:16px; \min-width:16px; \image: url(STYLESHEET_PIC_PATH/common/year_next_nor.png); \} \\QPushButton#CalendarRightYearBtn:hover \{ \image: url(STYLESHEET_PIC_PATH/common/year_next_down.png); \} \\QPushButton#CalendarLeftMonthBtn \{ \max-height:16px; \min-height:16px; \max-width:16px; \min-width:16px; \image: url(STYLESHEET_PIC_PATH/common/month_last_nor.png); \} \\QPushButton#CalendarLeftMonthBtn:hover \{ \image: url(STYLESHEET_PIC_PATH/common/month_last_down.png); \} \\QPushButton#CalendarRightMonthBtn \{ \max-height:16px; \min-height:16px; \max-width:16px; \min-width:16px; \image: url(STYLESHEET_PIC_PATH/common/month_next_nor.png); \} \\QPushButton#CalendarRightMonthBtn:hover \{ \image: url(STYLESHEET_PIC_PATH/common/month_next_down.png); \} \QLabel#CommonTextWhite14 \{ \color: #ffffff; \font-size: 14px; \} \";str.replace("STYLESHEET_PIC_PATH/common/", "://res/");return str;
}

图片资源

图片下载

调用代码

Dialog::Dialog(QWidget *parent): QDialog(parent), ui(new Ui::Dialog)
{ui->setupUi(this);setWindowTitle(tr("Calendar Widget"));setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);// 样式 1setStyleSheet(GetQss());m_pCalender = new TCalendarWidget(this);QHBoxLayout* pMainLayout = new QHBoxLayout(this);pMainLayout->setMargin(0);pMainLayout->addWidget(m_pCalender);
}

相关文章:

Qt日历控件示例-QCalendarWidget

基本说明 QCalendarWidget介绍&#xff1a; QCalendarWidget 是 Qt 框架中提供的一个日期选择控件,用户可以通过该控件快速选择需要的日期,并且支持显示当前月份的日历。 这里&#xff0c;我们继承了QCalendarWidget&#xff0c;做了一些简单封装和样式调整 1.使用的IDE&…...

函数式编程(四)Stream流使用

一、概述 在使用stream之前&#xff0c;先理解Optional 。 Optional是Java 8引入的一个容器类&#xff0c;用于处理可能为空的值。它提供了一种优雅的方式来处理可能存在或不存在的值&#xff0c;避免了空指针异常。 Optional的主要特点如下&#xff1a; 可能为空&#xff…...

区块链面临六大安全问题 安全测试方案研究迫在眉睫

区块链面临六大安全问题 安全测试方案研究迫在眉睫 近年来&#xff0c;区块链技术逐渐成为热门话题&#xff0c;其应用前景受到各国政府、科研机构和企业公司的高度重视与广泛关注。随着技术的发展&#xff0c;区块链应用与项目层出不穷&#xff0c;但其安全问题不容忽视。近年…...

K8S---kubelet TLS 启动引导

一、引导启动初始化过程(Bootstrap Initialization ) 1、kubeadm 生成一个Token,类似07401b.f395accd246ae52d这种格式,或者自己手动生成2、使用kubectl命令行,生成一个Secret,具体详见认证、授权3、kubelet 进程启动 (begin)4、kubelet 看到自己没有对应的 kubeconfig…...

Android系统修改驱动固定USB摄像头节点绑定前后置摄像头

前言 Android系统中usb摄像头节点会因为摄像头所接的usb口不同或者usb设备识别顺序不一样而出现每次开机生成的video节点不一样的问题。由于客户app调用摄像头时,需要固定摄像头的节点。因此需要针对前面的情况做处理。 方式1:通过摄像头名称固定摄像头节点 --- a/kernel…...

RT-Thread 内核移植

内核移植 内核移植就是将RTT内核在不同的芯片架构、不同的板卡上运行起来&#xff0c;能够具备线程管理和调度&#xff0c;内存管理&#xff0c;线程间同步等功能。 移植可分为CPU架构移植和BSP&#xff08;Board support package&#xff0c;板级支持包&#xff09;移植两部…...

springboot中entity层、dto层、vo层通俗理解三者的区别

entity&#xff1a;这个类的属性是跟数据库字段一模一样的&#xff08;驼峰命名&#xff09;&#xff0c;当我们使用MyBatis-Plus的时候经常用得到。 dto&#xff1a;用于后端接收前端返回的数据&#xff0c;一般是post请求&#xff0c;前端会给我们返回一个json对象&#xff…...

TypeScript_队列结构-链表

队列 队列&#xff08;Queue&#xff09;&#xff0c;它是一种受限的线性表&#xff0c;先进先出&#xff08;FIFO First In First Out&#xff09; 受限之处在于它只允许在队列的前端&#xff08;front&#xff09;进行删除操作而在队列的后端&#xff08;rear&#xff09;进…...

STM32G0 定时器PWM DMA输出驱动WS2812配置 LL库

通过DMA方式输出PWM模拟LED数据信号 优点&#xff1a;不消耗CPU资源 缺点&#xff1a;占用内存较大 STM32CUBEMX配置 定时器配置 定时器通道&#xff1a;TIM3 CH2 分频&#xff1a;0 重装值&#xff1a;79&#xff0c;芯片主频64Mhz&#xff0c;因此PWM输出频率&#xff1a…...

记录错误:Access denied for user ‘root‘@‘localhost‘ (using password:No) 解决方案

他说我没输入密码&#xff0c;但是我输入了啊&#xff1f;&#xff1f;于是&#xff0c;我试了试这儿&#xff0c;password 一改就好了。。。 他原来是是我打的很快&#xff0c;快速生成的。。。。...

python爬虫实战(5)--获取小破站热榜

1. 分析地址 打开小破站热榜首页&#xff0c;查看响应找到如下接口地址 2. 编码 定义请求头 拿到标头 复制粘贴&#xff0c;处理成json 处理请求头代码如下: def format_headers_to_json():f open("data.txt", "r", encoding"utf-8") # 读…...

单目标应用:基于麻雀搜索算法SSA的微电网优化调度MATLAB

一、微网系统运行优化模型 参考文献&#xff1a; [1]李兴莘,张靖,何宇,等.基于改进粒子群算法的微电网多目标优化调度[J].电力科学与工程, 2021, 37(3):7 二、麻雀搜索算法简介 麻雀搜索算法 (Sparrow Search Algorithm, SSA) 是一种新型的群智能优化算法&#xff0c;于2020…...

C# easymodbus

库介绍 EasyModbus是用于 .NET 和 Java 平台上的Modbus TCP/UDP/RTU通讯协议库&#xff0c;支持多种编程语言&#xff0c;如C#、VB.NET、Java、C 与更多C#的变体&#xff0c;如Unity、Mono、.NET Core等等。 EasyModbus的Java版本至少需要Java 7&#xff0c;而C#版本兼容 .NE…...

HikariCP源码修改,使其连接池支持Kerberos认证

HikariCP-4.0.3 修改HikariCP源码,使其连接池支持Kerberos认证 修改后的Hikari源码地址:https://github.com/Raray-chuan/HikariCP-4.0.3 Springboot使用hikari连接池并进行Kerberos认证访问Impala的demo地址:https://github.com/Raray-chuan/springboot-kerberos-hikari-im…...

5分钟看明白rust mod use

rust把mod简单的事没说清&#xff0c;一片混乱&#xff0c;似懂非懂. mod语句查找只有一条规则&#xff1a;先找mod名1.rs,没有就我同名文件夹下的mod名1.rs&#xff0c;如果没有&#xff0c;就同名文件夹下的mod名1/mod.rs,再没有就error. 在mod.rs中&#xff0c;pub mod 文件…...

【Java核心知识】ThreadLocal相关知识

ThreadLocal 什么是ThreadLocal ThreadLoacal类可以为每个线程保存一份独有的变量&#xff0c;该变量对于每个线程都是独占的。实现原理为每个Thread类中包含一个ThreadHashMap&#xff0c;key为变量的对应的ThreadLocal对象&#xff0c;value为变量的值。 在日常使用中&…...

《Python基础教程(第三版)》阅读笔记 1

目录 1 快速上手&#xff1a;基础知识2 列表和元组3 字符串4 字典5 条件、循环及其他6 抽象7 再谈抽象8 异常9 魔法方法、特性和迭代器10 开箱即用 本文参考自《Beginning Python: from novice to professional》&#xff0c;中文版为《Python基础教程&#xff08;第三版&#…...

坦克400 Hi4-T预售价28.5万元起,越野新能源好理解

8月25日&#xff0c;在以“智享蓉城&#xff0c;驭见未来”为主题的成都国际车展上&#xff0c;坦克品牌越野新能源再启新程&#xff0c;首次以全Hi4-T新能源阵容亮相展台&#xff0c;释放坦克品牌加速布局越野新能源的强烈信号。 Hi4-T架构首款落地车型坦克500 Hi4-T上市至今斩…...

我的Vim学习笔记(不定期更新)

2023年9月3日&#xff0c;周日上午 学到了啥就写啥&#xff0c;不定期更新 目录 字体 文件 标签页 分屏 调用系统命令 字体 设置字体大小 :set guifont字体:h字体大小 例如&#xff0c;:set guifontMonospace:h20 查询当前使用的字体和字体大小 :set guifont? 查看…...

spring boot项目生成容器并运行

一个安静的周末&#xff0c;shigen又睡懒觉了&#xff0c;上次说的拖延症的惩罚来了&#xff1a;早晚各100个健腹轮练习&#xff0c;早上的已经完成了。今天的文章来的有点晚&#xff0c;但是依旧保持质量。 springboot项目生成容器并运行 背景 将springboot项目打包成jar包&…...

【Oracle APEX开发小技巧12】

有如下需求&#xff1a; 有一个问题反馈页面&#xff0c;要实现在apex页面展示能直观看到反馈时间超过7天未处理的数据&#xff0c;方便管理员及时处理反馈。 我的方法&#xff1a;直接将逻辑写在SQL中&#xff0c;这样可以直接在页面展示 完整代码&#xff1a; SELECTSF.FE…...

条件运算符

C中的三目运算符&#xff08;也称条件运算符&#xff0c;英文&#xff1a;ternary operator&#xff09;是一种简洁的条件选择语句&#xff0c;语法如下&#xff1a; 条件表达式 ? 表达式1 : 表达式2• 如果“条件表达式”为true&#xff0c;则整个表达式的结果为“表达式1”…...

【磁盘】每天掌握一个Linux命令 - iostat

目录 【磁盘】每天掌握一个Linux命令 - iostat工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景 注意事项 【磁盘】每天掌握一个Linux命令 - iostat 工具概述 iostat&#xff08;I/O Statistics&#xff09;是Linux系统下用于监视系统输入输出设备和CPU使…...

【2025年】解决Burpsuite抓不到https包的问题

环境&#xff1a;windows11 burpsuite:2025.5 在抓取https网站时&#xff0c;burpsuite抓取不到https数据包&#xff0c;只显示&#xff1a; 解决该问题只需如下三个步骤&#xff1a; 1、浏览器中访问 http://burp 2、下载 CA certificate 证书 3、在设置--隐私与安全--…...

第一篇:Agent2Agent (A2A) 协议——协作式人工智能的黎明

AI 领域的快速发展正在催生一个新时代&#xff0c;智能代理&#xff08;agents&#xff09;不再是孤立的个体&#xff0c;而是能够像一个数字团队一样协作。然而&#xff0c;当前 AI 生态系统的碎片化阻碍了这一愿景的实现&#xff0c;导致了“AI 巴别塔问题”——不同代理之间…...

全面解析各类VPN技术:GRE、IPsec、L2TP、SSL与MPLS VPN对比

目录 引言 VPN技术概述 GRE VPN 3.1 GRE封装结构 3.2 GRE的应用场景 GRE over IPsec 4.1 GRE over IPsec封装结构 4.2 为什么使用GRE over IPsec&#xff1f; IPsec VPN 5.1 IPsec传输模式&#xff08;Transport Mode&#xff09; 5.2 IPsec隧道模式&#xff08;Tunne…...

Python Ovito统计金刚石结构数量

大家好,我是小马老师。 本文介绍python ovito方法统计金刚石结构的方法。 Ovito Identify diamond structure命令可以识别和统计金刚石结构,但是无法直接输出结构的变化情况。 本文使用python调用ovito包的方法,可以持续统计各步的金刚石结构,具体代码如下: from ovito…...

C/C++ 中附加包含目录、附加库目录与附加依赖项详解

在 C/C 编程的编译和链接过程中&#xff0c;附加包含目录、附加库目录和附加依赖项是三个至关重要的设置&#xff0c;它们相互配合&#xff0c;确保程序能够正确引用外部资源并顺利构建。虽然在学习过程中&#xff0c;这些概念容易让人混淆&#xff0c;但深入理解它们的作用和联…...

接口自动化测试:HttpRunner基础

相关文档 HttpRunner V3.x中文文档 HttpRunner 用户指南 使用HttpRunner 3.x实现接口自动化测试 HttpRunner介绍 HttpRunner 是一个开源的 API 测试工具&#xff0c;支持 HTTP(S)/HTTP2/WebSocket/RPC 等网络协议&#xff0c;涵盖接口测试、性能测试、数字体验监测等测试类型…...

MySQL 主从同步异常处理

阅读原文&#xff1a;https://www.xiaozaoshu.top/articles/mysql-m-s-update-pk MySQL 做双主&#xff0c;遇到的这个错误&#xff1a; Could not execute Update_rows event on table ... Error_code: 1032是 MySQL 主从复制时的经典错误之一&#xff0c;通常表示&#xff…...