Qt實(shí)現(xiàn)網(wǎng)易云音樂進(jìn)度條效果
Qt網(wǎng)易云音樂中的音樂進(jìn)度條是一個(gè)小小的難關(guān),今天在Android寫記錄步數(shù)進(jìn)度條中突然想到了它。移動(dòng)端和電腦端自定義寫界面模塊的方式大同小異,現(xiàn)在將其中的代碼放出,供大家點(diǎn)評(píng)參閱。代碼還是比較混亂,請(qǐng)各位大俠還是要多多包涵。
其中我把這個(gè)類打包出來,類的實(shí)現(xiàn)就是靠painevent來完成的。其中的兩個(gè)自定義信號(hào)是為了快進(jìn)快退而準(zhǔn)備的,它們的實(shí)現(xiàn)不在本類中,大小位置也是在外部類中實(shí)現(xiàn)。其中的一些位置參數(shù),大小參數(shù)屬于本身創(chuàng)作需要,如有迷惑,還望海涵。
代碼:
#ifndef DOWN_PROGRESSBARWIDGET_H #define DOWN_PROGRESSBARWIDGET_H #include <QWidget> #include <QLabel> #include <QPushButton> #include <QPaintEvent> #include <QPainter> #include <QMouseEvent> #include <QPalette> #include <QTimer> class down_progressbarWidget : public QWidget { Q_OBJECT public: QString s="0:0"; QString t="0:0"; explicit down_progressbarWidget(QWidget *parent = 0); void changPosition(qint64 xx); private: int X=56; qint64 druntime;//總進(jìn)度 qint64 positontime;//當(dāng)前進(jìn)度 QLabel *liftLabel;//左時(shí)間顯示 QLabel *rightLabel;//右邊時(shí)間顯示 QTimer *timer3;//刷新 void paintEvent(QPaintEvent* event); void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); signals: void changeX();//自定義一個(gè)信號(hào) public slots: void updatepos(); }; #endif // DOWN_PROGRESSBARWIDGET_H #include "down_progressbarwidget.h" down_progressbarWidget::down_progressbarWidget(QWidget *parent) : QWidget(parent) { QPalette pal; pal.setColor(QPalette::WindowText,QColor(255,255,255)); QFont font("Courier",10); liftLabel=new QLabel(this); rightLabel=new QLabel(this); liftLabel->setAlignment(Qt::AlignCenter); rightLabel->setAlignment(Qt::AlignCenter); liftLabel->setGeometry(0,10,50,50); rightLabel->setGeometry(640,10,50,50); rightLabel->setFont(font); liftLabel->setFont(font); liftLabel->setPalette(pal); rightLabel->setPalette(pal); timer3=new QTimer(this); timer3->setInterval(1000); connect(timer3,SIGNAL(timeout()),this,SLOT(updatepos())); } void down_progressbarWidget::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); painter.setRenderHint(QPainter::SmoothPixmapTransform); painter.setRenderHint(QPainter::Antialiasing); QColor baseColor(0,20,20); QColor inColor(255,0,0); QColor outColor(255,255,255); painter.save(); painter.setPen(Qt::NoPen); painter.setBrush(baseColor); QRectF rect=QRectF(55,32,580,6); painter.drawRoundedRect(rect,3,3); painter.restore(); painter.save(); painter.setPen(Qt::NoPen); painter.setBrush(inColor); QRectF playrect=QRectF(55,32,X-63,6); painter.drawRoundedRect(playrect,3,3); painter.restore(); painter.setPen(Qt::NoPen); painter.setBrush(outColor); painter.drawEllipse(X-7,25,20,20); painter.restore(); painter.save(); painter.setPen(Qt::NoPen); painter.setBrush(inColor); painter.drawEllipse(X,32,6,6); painter.restore(); } void down_progressbarWidget::mousePressEvent(QMouseEvent *event) { if(event->pos().y()>30&&event->pos().y()<50) { int value; value=event->pos().x(); if(value<55) { X=56; } else if(value>635) { X=620; } else { X=value; } if(X<=620) { emit changeX(); } update(); setCursor(Qt::PointingHandCursor); } else { event->ignore(); } } void down_progressbarWidget::mouseMoveEvent(QMouseEvent *event)//可以進(jìn)行拖動(dòng) { int value; value=event->pos().x(); if(value<55) { X=56; } else if(value>635) { X=620; } else { X=event->pos().x(); } emit changeX();//自定義了一個(gè)信號(hào) update(); setCursor(Qt::PointingHandCursor); } /* * 外部使用一個(gè)定時(shí)器每隔1秒觸發(fā)這個(gè)程序進(jìn)行更新 */ void down_progressbarWidget::updatepos()//顯示歌曲時(shí)間進(jìn)度 { float a; a=(float)positontime/(float)druntime; X=a*580+56; qint64 time=druntime; double secondDouble=time/1000; int minint=secondDouble/60; int secondint=secondDouble-minint*60; double secondtime1=positontime/1000; int minint1=secondtime1/60; int secondint1=secondtime1-minint1*60; t=QString("%0:%1").arg(QString::number(minint1),QString::number(secondint1)); s=QString("%0:%1").arg(QString::number(minint),QString::number(secondint)); rightLabel->setText(s); liftLabel->setText(t); update(); } void down_progressbarWidget::changPosition(qint64 xx)//外部寫入當(dāng)前時(shí)間 { this->positontime=xx; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++設(shè)計(jì)模式編程中proxy代理模式的使用實(shí)例
這篇文章主要介紹了C++設(shè)計(jì)模式編程中proxy代理模式的使用實(shí)例解析,代理模式可以被歸類為結(jié)構(gòu)型的設(shè)計(jì)模式,代理模式主張為對(duì)象提供一種代理以控制對(duì)這個(gè)對(duì)象的訪問,需要的朋友可以參考下2016-03-03C/C++中的atan和atan2函數(shù)實(shí)例用法
在本篇文章里小編給大家分享的是一篇關(guān)于C/C++中的atan和atan2函數(shù)實(shí)例用法相關(guān)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-02-02C++基于控制臺(tái)實(shí)現(xiàn)的貪吃蛇小游戲
這篇文章主要介紹了C++基于控制臺(tái)實(shí)現(xiàn)的貪吃蛇小游戲,實(shí)例分析了貪吃蛇游戲的原理與C++實(shí)現(xiàn)技巧,是非常經(jīng)典的游戲算法,需要的朋友可以參考下2015-04-04C語言中實(shí)現(xiàn)itoa函數(shù)的實(shí)例
這篇文章主要介紹了C語言中實(shí)現(xiàn)itoa函數(shù)的實(shí)例的相關(guān)資料,希望通過本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10C++浮點(diǎn)數(shù)在內(nèi)存中的存儲(chǔ)詳解
大家好,本篇文章主要講的是C++浮點(diǎn)數(shù)在內(nèi)存中的存儲(chǔ)詳解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01基于C++浮點(diǎn)數(shù)(float、double)類型數(shù)據(jù)比較與轉(zhuǎn)換的詳解
本篇文章是對(duì)C++中浮點(diǎn)數(shù)(float、double)類型數(shù)據(jù)比較與轉(zhuǎn)換進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05深入剖析C語言中qsort函數(shù)的實(shí)現(xiàn)原理
這篇文章主要介紹了C語言中qsort函數(shù)的實(shí)現(xiàn)原理,本文將從回調(diào)函數(shù),qsort函數(shù)的應(yīng)用,qsort函數(shù)的實(shí)現(xiàn)原理三個(gè)方面進(jìn)行講解,并通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-03-03OpenCV實(shí)現(xiàn)彩色照片轉(zhuǎn)換成素描卡通片
這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)彩色照片轉(zhuǎn)換成素描卡通片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01