基于Qt实现自定义时间选择控件

 更新时间:2023年12月13日 17:02:18   作者:友善啊,朋友  
这篇文章主要为大家详细介绍了如何基于Qt实现自定义时间选择控件,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下

Qt编写自定义控件:时间选择控件

完整代码

#include "rotateedittimewidget.h"
#include <QPainter>
#include <QDateTime>
#include <QDebug>
#include <QMouseEvent>
 
struct RotateEditTimeWidgetPrivate
{
    bool isEditHour{true};
    QRect handleRect;
    bool isPress{false};
    int angle{0};
    int hour{0};
    int minute{0};
    QRect textRect;
};
 
RotateEditTimeWidget::RotateEditTimeWidget(QWidget *parent)
    : QWidget(parent)
{
    d_ptr = new RotateEditTimeWidgetPrivate;
    auto time = QDateTime::currentDateTime().time();
    d_ptr->hour = time.hour();
    d_ptr->minute = time.minute();
    d_ptr->angle = d_ptr->hour * 15;
}
 
RotateEditTimeWidget::~RotateEditTimeWidget()
{
    delete d_ptr;
}
 
void RotateEditTimeWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing,true);
    painter.setRenderHints(QPainter::TextAntialiasing);
 
    auto thisRect = rect();
    auto side = qMin(thisRect.width(), thisRect.height()) / 2 - 5;
 
    painter.save();
    painter.translate(thisRect.center());
    painter.setBrush(Qt::transparent);
    painter.setPen(QPen(Qt::red,3));
    painter.drawEllipse(QPoint(0,0),side ,side);
    painter.restore();
 
    {
        painter.save();
 
        QString time24 = QString("%1:%2").arg(d_ptr->hour, 2, 10, QLatin1Char('0')).arg(d_ptr->minute, 2, 10, QLatin1Char('0'));
 
        QFont font("Arial", 12);
        int fontSize = side / 5;
        font.setPointSize(fontSize);
        font.setBold(true);
        painter.setFont(font);
 
        d_ptr->textRect = painter.boundingRect(thisRect,Qt::AlignCenter,time24);
        QLinearGradient linearGradient(d_ptr->textRect.topLeft(),d_ptr->textRect.topRight());
        if(d_ptr->isEditHour)
        {
            linearGradient.setColorAt(0.0,Qt::blue);
            linearGradient.setColorAt(0.45,Qt::blue);
            linearGradient.setColorAt(0.46,Qt::black);
            linearGradient.setColorAt(0.6,Qt::black);
            linearGradient.setColorAt(1.0,Qt::black);
        }
        else
        {
            linearGradient.setColorAt(0.0,Qt::black);
            linearGradient.setColorAt(0.4,Qt::black);
            linearGradient.setColorAt(0.54,Qt::black);
            linearGradient.setColorAt(0.55,Qt::blue);
            linearGradient.setColorAt(1.0,Qt::blue);
        }
 
        painter.setPen(QPen(QBrush(linearGradient),3));
 
        painter.drawText(thisRect,Qt::AlignCenter,time24);
        painter.restore();
    }
 
    painter.save();
    painter.setBrush(QColor("#aae8e8e8"));
    painter.setPen(QPen(Qt::blue,3));
 
    painter.translate(thisRect.center());
    auto handleRectTemp_zeroRect_center = QPoint(0,-side * 0.8);
    QTransform transform;
    transform.rotate(d_ptr->angle);
    QPoint transform_p = transform.map(handleRectTemp_zeroRect_center);
    d_ptr->handleRect = QRect(transform_p - QPoint(side / 9,side /9),transform_p + QPoint(side / 9,side /9));
    painter.drawEllipse(d_ptr->handleRect);
    painter.restore();
}
 
void RotateEditTimeWidget::mousePressEvent(QMouseEvent *event)
{
    auto pos = event->pos();
    const auto & rect = d_ptr->textRect;
    if(rect.contains(pos))
    {
        if(QRect(rect.topLeft(),QSize(rect.width() * 0.4,rect.height())).contains(pos))
        {
            d_ptr->isEditHour = true;
            d_ptr->angle = d_ptr->hour * 15;
        }
        else if(QRect(rect.topLeft() + QPoint(rect.width() * 0.6,0),QSize(rect.width() * 0.4,rect.height())).contains(pos))
        {
            d_ptr->isEditHour = false;
            d_ptr->angle = d_ptr->minute * 6;
        }
        update();
    }
    else
    {
        auto temp = d_ptr->handleRect;
        auto thisRect = this->rect();
        temp.translate(thisRect.width() / 2,thisRect.height() / 2);
 
        if(temp.contains(pos))
        {
            d_ptr->isPress = true;
            update();
        }
    }
}
 
void RotateEditTimeWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if(d_ptr->isPress)
    {
        d_ptr->isPress = false;
        update();
    }
}
 
void RotateEditTimeWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(d_ptr->isPress)
    {
        auto pos = event->pos();
        auto rectCenterPos = rect().center();
 
        QLineF line1(rectCenterPos,QPoint(rectCenterPos.x(),0));
        QLineF line2(rectCenterPos,pos);
        d_ptr->angle = line2.angleTo(line1);
        if(d_ptr->isEditHour)
        {
            d_ptr->hour = d_ptr->angle / 15;
        }
        else
        {
            d_ptr->minute = d_ptr->angle / 6;
        }
        update();
    }
}

效果图

到此这篇关于基于Qt实现自定义时间选择控件的文章就介绍到这了,更多相关Qt时间选择控件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Visual Studio 2019配置qt开发环境的搭建过程

    Visual Studio 2019配置qt开发环境的搭建过程

    这篇文章主要介绍了Visual Studio 2019配置qt开发环境的搭建过程,本文图文并茂给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-03-03
  • c/c++拷贝构造函数和关键字explicit详解

    c/c++拷贝构造函数和关键字explicit详解

    这篇文章主要介绍了c/c++拷贝构造函数和关键字explicit的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-08-08
  • Linux下C语言实现贪吃蛇小游戏

    Linux下C语言实现贪吃蛇小游戏

    这篇文章主要为大家详细介绍了Linux下C语言实现贪吃蛇小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-03-03
  • C语言广播的使用详解

    C语言广播的使用详解

    顾名思义可以把自己的数据发送给在特定范围内的所有人;我们网络编程中的广播一般是通过特定的广播地址把自己的数据发送给局域网内当前在线的客户端
    2022-05-05
  • c++ KMP字符串匹配算法

    c++ KMP字符串匹配算法

    大家好,本篇文章主要讲的是c++ KMP字符串匹配算法,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-01-01
  • C++超详细讲解稀疏矩阵

    C++超详细讲解稀疏矩阵

    今天小编就为大家分享一篇关于C++稀疏矩阵的转置思路并实现乘法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2022-05-05
  • wince禁止程序标题栏上的退出按钮示例

    wince禁止程序标题栏上的退出按钮示例

    这篇文章主要介绍了wince禁止程序标题栏上的退出按钮示例,需要的朋友可以参考下
    2014-02-02
  • C语言猜凶手的代码实现

    C语言猜凶手的代码实现

    本文主要介绍了C语言猜凶手的代码实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • C语言实现猜数字大小的游戏

    C语言实现猜数字大小的游戏

    这篇文章主要为大家详细介绍了C语言实现猜数字大小的游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-01-01
  • C语言数组元素循环右移问题及解决方法

    C语言数组元素循环右移问题及解决方法

    这篇文章主要介绍了C语言数组元素循环右移问题,本文通过多种方法给大家分享解决方案,通过实例代码讲解,对大家的工作或学习具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-03-03

最新评论