基于Qt實(shí)現(xiàn)自定義時(shí)間選擇控件
Qt編寫自定義控件:時(shí)間選擇控件
完整代碼
#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(); } }
效果圖
到此這篇關(guān)于基于Qt實(shí)現(xiàn)自定義時(shí)間選擇控件的文章就介紹到這了,更多相關(guān)Qt時(shí)間選擇控件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言入門篇--初識(shí)結(jié)構(gòu)體
本篇文章是基礎(chǔ)篇,適合c語(yǔ)言剛?cè)腴T的朋友,本文對(duì)c語(yǔ)言的結(jié)構(gòu)體做了簡(jiǎn)單的分析,幫助大家快速入門c語(yǔ)言的世界,更好的理解c語(yǔ)言2021-08-08C語(yǔ)言結(jié)構(gòu)體數(shù)組的定義和使用詳解
結(jié)構(gòu)體中也有數(shù)組,稱為結(jié)構(gòu)體數(shù)組。它與數(shù)值型數(shù)組幾乎是一模一樣的,只不過(guò)需要注意的是,結(jié)構(gòu)體數(shù)組的每一個(gè)元素都是一個(gè)結(jié)構(gòu)體類型的變量,都包含結(jié)構(gòu)體中所有的成員項(xiàng)。本文將帶大家詳解了解結(jié)構(gòu)體數(shù)組的定義與使用2021-12-12VS中scanf函數(shù)報(bào)錯(cuò)問(wèn)題的幾種解決方法
本文主要介紹了VS中scanf函數(shù)報(bào)錯(cuò)問(wèn)題的幾種解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07淺析成員函數(shù)和常成員函數(shù)的調(diào)用
下面小編就為大家?guī)?lái)一篇淺析成員函數(shù)和常成員函數(shù)的調(diào)用。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧2016-05-05使用C語(yǔ)言實(shí)現(xiàn)從avi視頻中提取圖片
這篇文章主要為大家詳細(xì)介紹了如何使用C語(yǔ)言實(shí)現(xiàn)從avi視頻中提取圖片,文中的示例代碼簡(jiǎn)潔易懂,具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下2023-10-10C++高并發(fā)內(nèi)存池的實(shí)現(xiàn)
本文主要介紹了C++高并發(fā)內(nèi)存池的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07