Qt日历控件示例-QCalendarWidget

news2024/9/26 3:31:12

基本说明

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_OBJECT

public:
    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);
    // 样式 1
    setStyleSheet(GetQss());
    
    m_pCalender = new TCalendarWidget(this);
    QHBoxLayout* pMainLayout = new QHBoxLayout(this);
    pMainLayout->setMargin(0);
    pMainLayout->addWidget(m_pCalender);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/957717.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

java_error_in_idea.hprof 文件

在用户目录下的java_error_in_idea.hprof文件(/Users/用户) 大约1.5个G,IDEA的错误日志,可以删除

shiny根据数据的长度设置多个色板

shiny根据数据的长度设置多个色板 library(shiny) library(colourpicker) ui <- fluidPage(# 添加一个选择颜色的下拉菜单uiOutput("color_dropdown") )server <- function(input, output) {# 数据长度data_length <- reactive({length(c("数据1"…

Linux(centos) 下 Mysql 环境安装

linux 下进行环境安装相对比较简单&#xff0c;可还是会遇到各种奇奇怪怪的问题&#xff0c;我们来梳理一波 安装 mysql 我们会用到下地址&#xff1a; Mysql 官方文档的地址&#xff0c;可以参考&#xff0c;不要全部使用 https://dev.mysql.com/doc/refman/8.0/en/linux-i…

文献阅读:Deep Learning based Semantic Communications: An Initial Investigation

目录 论文简介动机&#xff1a;为什么作者想要解决这个问题&#xff1f;贡献&#xff1a;作者在这篇论文中完成了什么工作(创新点)&#xff1f;规划&#xff1a;他们如何完成工作&#xff1f;理由&#xff1a;通过什么实验验证它们的工作结果自己的看法 论文简介 作者 Huiqiang…

day-06 多进程服务器端 -- 进程间通信

一.多进程服务器端 &#xff08;一&#xff09;进程概念及应用 利用之前学习到的内容&#xff0c;我们的服务器可以按照顺序处理多个客户端的服务请求。在客户端和服务时间增长的情况下&#xff0c;服务器就不足以满足需求了。 1.两种类型的服务器端 &#xff08;1&#xff…

安全基础 --- https详解(02)、cookie和session、同源和跨域

https详解&#xff08;02&#xff09;--- 数据包扩展 Request --- 请求数据包Response --- 返回数据包 若出现代理则如下图&#xff1a; Proxy --- 代理服务器 &#xff08;1&#xff09;http和https的区别 http明文传输&#xff0c;数据未加密&#xff1b;http页面响应速度…

【Java 动态数据统计图】动态X轴二级数据统计图思路Demo(动态,排序,动态数组(重点推荐:难)九(131)

需求&#xff1a; 1.有一组数据集合&#xff0c;数据集合中的数据为动态&#xff1b; 举例如下&#xff1a; [{province陕西省, city西安市}, {province陕西省, city咸阳市}, {province陕西省, city宝鸡市}, {province陕西省, city延安市}, {province陕西省, city汉中市}, {pr…

用了这么久SpringBoot却还不知道的一个小技巧

前言 你可能调第三方接口喜欢启动application&#xff0c;修改&#xff0c;再启动&#xff0c;再修改&#xff0c;顺便还有个不喜欢写JUnitTest的习惯。 你可能有一天想要在SpringBoot启动后&#xff0c;立马想要干一些事情&#xff0c;现在没有可能是你还没遇到。 那么SpringB…

windows10默认浏览器总是自动更改为Edge浏览器

在设置的默认应用设置中把默认浏览器改为chrome或其他之后他自动又会改回Edge。不得不说*软真的狗。 解决办法&#xff1a; 后来发现在Edge浏览器的设置中有这么一个选项&#xff0c;会很无耻的默认是Edge。把它关掉后重新设置就行了。

02. 计算机的组成

1. 从手机和电脑开始 要是20年前&#xff0c;大家对于计算机还很陌生&#xff0c;但是现在手机和电脑已经非常普及了&#xff0c;即使对于偏远地区可能有人没有接触过电脑&#xff0c;但是手机肯定都用过。其实手机和电脑都是计算机&#xff01; 1.1 手机的8G256G是什么意思?…

2D-2D对极几何中的基本矩阵、本质矩阵和单应矩阵

本文主要参考高翔博士的视觉SLAM十四讲第二版中的7.3章节内容。文章目录 1 对极约束2 本质矩阵E3 单应矩阵 1 对极约束 现在&#xff0c;假设我们从两张图像中得到了一对配对好的特征点&#xff0c;如图7.9所示&#xff08;假如后面我们有若干对这样的匹配点&#xff0c;根据这…

IDEA打开一个项目时,idea左侧project模式下,不显示项目工程目录的解决方法

在IDEA打开一个一个已有的项目chapter3时&#xff0c;idea左侧project模式下&#xff0c;左侧也没有project按钮&#xff0c;如下问题截图&#xff1a;&#xff08;ps:项目结构可以显示&#xff0c;但是src等目录不见&#xff09; 在网上查了一些方法&#xff1a; 1、解决办法…

移动端的概念

【移动端】 1. 什么是移动端 大前端时代&#xff1a; ​ 前端开发涉及的领域越来越多&#xff0c;如&#xff1a;PC端&#xff0c;移动端&#xff0c;小程序&#xff0c;App&#xff0c;甚至是物联网​ 大前端’的大字体现在地位重要&#xff0c;涉及领域众多​ 前后端完全分…

从零开始的Hadoop学习(五)| HDFS概述、shell操作、API操作

1. HDFS 概述 1.1 HDFS 产出背景及定义 1&#xff09;HDFS 产生背景 随着数据量越来越大&#xff0c;在一个操作系统存不下所有的数据&#xff0c;那么就分配到更多的操作系统管理的磁盘中&#xff0c;但是不方便管理和维护&#xff0c;迫切 需要一种系统来管理多台机器上的…

OpenCV(八):图像二值化

目录 1.固定值二值化 2.自适应阈值二值化 3.Android JNI完整代码 1.固定值二值化 固定阈值二值化是OpenCV中一种简单而常用的图像处理技术&#xff0c;用于将图像转换为二值图像。在固定阈值二值化中&#xff0c;像素值根据一个预定义的阈值进行分类&#xff0c;大于阈值的…

OFDM 系统在 AWGN 信道下对不同载波频率偏移 (CFO) 的 BER 灵敏度研究(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

软件测评国家标准概要总结

软件测试 软件测评是指针对软件功能、性能、用途使用价值等进行的评价和测试&#xff1b; 软件测评主要依靠标准是GB/T 25000.51-2016 系统与软件工程 系统与软件质量要求和评价&#xff08;SQuaRE&#xff09; 第51部分&#xff1a;就绪可用软件产品&#xff08;RUSP&#x…

基于JavaWeb和mysql实现校园订餐前后台管理系统(源码+数据库)

一、项目简介 本项目是一套基于JavaWeb和mysql实现网上书城前后端管理系统&#xff0c;主要针对计算机相关专业的正在做毕设的学生与需要项目实战练习的Java学习者。 包含&#xff1a;项目源码、项目文档、数据库脚本等&#xff0c;该项目附带全部源码可作为毕设使用。 项目都…

小兔鲜儿---商品分类

目录 准备工作​ 渲染轮播图​ 一级分类​ 获取数据​ Tab 交互​ 二级分类​ 骨架屏​ 准备工作​ 参考效果 商品分类页中的广告位&#xff0c;可复用之前定义的轮播图组件 XtxSwiper。 静态结构 商品分类页静态结构&#xff1a; src/pages/category/category.vue …

Docsify + Gitalk详细配置过程讲解

&#x1f496; 作者简介&#xff1a;大家好&#xff0c;我是Zeeland&#xff0c;开源建设者与全栈领域优质创作者。&#x1f4dd; CSDN主页&#xff1a;Zeeland&#x1f525;&#x1f4e3; 我的博客&#xff1a;Zeeland&#x1f4da; Github主页: Undertone0809 (Zeeland)&…