C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > Qt时间选择控件

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

作者:友善啊,朋友

这篇文章主要为大家详细介绍了如何基于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时间选择控件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文