今天的風兒真喧囂呀!
1234
我們開始吧
------------------------------------------------------------------華麗的開始線--------------------------------------------------------------
用過QT的都知道QT的控件真的不好看,那么你可能需要自己重寫了,不用擔心重寫還是很麻煩的。
首先請看圖:
還是很好看的:::::
首先博主繼承的是QLabel
下面是代碼的頭文件h:
1 #pragma once
2 #include <QLabel>
3 #include <QMouseEvent>
4 #include <QPropertyAnimation>
5 #include "Tool.h"
6 #include "qdebug.h"
7 #include <QTimer>
8 #include <QTimerEvent>
9 class LogoLabel :public QLabel 10 { 11 Q_OBJECT 12 public: 13 explicit LogoLabel(QWidget *parent = 0); 14 LogoLabel(const QString &text, QWidget *parent = 0); 15 void setQPixmapPath(QString path); 16 void setImageSize(int imageWidth, int imageHeight); 17 signals: 18 // 鼠標單擊信號
19 void clicked(); 20 protected: 21 // 鼠標單擊事件
22 void mouseReleaseEvent(QMouseEvent *); 23 void enterEvent(QEvent *); 24 void leaveEvent(QEvent *); 25 //定時器
26 void timerEvent(QTimerEvent *event); 27 private: 28 //圖片緩存(使用緩存技術免得重復打開圖片造成浪費)
29 QPixmap logoPix; 30 //是否在旋轉
31 bool isRotate = true; 32 bool isMagnify = true; 33 //存儲圖片的大小
34 int imageWidth = 0; 35 int imageHeight = 0; 36 //圖片的地址
37 QString pixmapPath; 38 //旋轉定時器
39 int rotateTimerId; 40 //旋轉度數
41 int rotateNumber = 0; 42 public slots: 43 public : 44 //按鈕是否放大
45 void setMagnify(bool isMagnify){ 46 this->isMagnify = isMagnify; 47 } 48 //按鈕是否旋轉
49 void setRotate(bool isRotate){ 50 this->isRotate = isRotate; 51 } 52 //按鈕旋轉
53 void setImageTransparency(int transNumber); 54 };
注釋已經很詳細了:還有不明白的可以在下面留言。
那么我們再來看看cpp主體文件:
1 #include "LogoLabel.h"
2 LogoLabel::LogoLabel(QWidget *parent) : 3 QLabel(parent) 4 { 5 } 6
7 LogoLabel::LogoLabel(const QString &text, QWidget *parent) : 8 QLabel(parent) 9 { 10 setText(text); 11 } 12
13 //鼠標釋放事件
14 void LogoLabel::mouseReleaseEvent(QMouseEvent *ev) 15 { 16 if (ev->button() == Qt::LeftButton) 17 emit clicked();//(this);
18 } 19 //鼠標進入事件
20 void LogoLabel::enterEvent(QEvent *) 21 { 22 this->setWindowOpacity(1); 23 QPixmap tempLogoPix = logoPix.scaled(imageWidth, imageHeight); 24 //如果可以旋轉就啟動定時器
25 if (isRotate){ 26 rotateTimerId = startTimer(50); 27 } 28 //如果放大就設置圖片發達1.5倍
29 if (isMagnify){ 30 setImageSize(this->imageWidth*1.5, this->imageHeight*1.5); 31 } 32 } 33 //設置圖片大小事件
34 void LogoLabel::setImageSize(int imageWidth, int imageHeight){ 35 this->imageWidth = imageWidth; 36 this->imageHeight = imageHeight; 37 } 38 //設置圖片路勁
39 void LogoLabel::setQPixmapPath(QString path){ 40 pixmapPath = path; 41 logoPix = QPixmap(path); 42 QPixmap tempLogoPix = logoPix.scaled(imageWidth, imageHeight); 43 Tool::setPixMapTransparency(tempLogoPix); 44 this->setPixmap(tempLogoPix); 45 } 46 //設置圖片透明度
47 void LogoLabel::setImageTransparency(int transNumber){ 48 QPixmap tempLogoPix = logoPix.scaled(imageWidth, imageHeight); 49 Tool::setPixMapTransparency(tempLogoPix, transNumber); 50 this->setPixmap(tempLogoPix); 51 } 52 //定時器
53 void LogoLabel::timerEvent(QTimerEvent *event){ 54 if (event->timerId() == rotateTimerId) 55 { 56 if (isRotate){ 57 //默認一次旋轉3個角度
58 rotateNumber += 3; 59 QPixmap tempLogoPix = logoPix.scaled(imageWidth, imageHeight); 60 Tool::setPixMapRotate(tempLogoPix, rotateNumber); 61 this->setPixmap(tempLogoPix); 62 } 63 else{ 64 killTimer(rotateTimerId); 65 rotateTimerId = 0; 66 } 67 } 68 else{ 69 QWidget::timerEvent(event); 70 } 71 } 72 //鼠標離開事件
73 void LogoLabel::leaveEvent(QEvent *) 74 { 75 if (isRotate && (rotateTimerId != 0)){ 76 killTimer(rotateTimerId); 77 } 78 if (isMagnify){ 79 setImageSize(this->imageWidth / 1.5, this->imageHeight / 1.5); 80 } 81 QPixmap tempLogoPix = logoPix.scaled(imageWidth, imageHeight); 82 Tool::setPixMapTransparency(tempLogoPix); 83 this->setPixmap(tempLogoPix); 84 rotateNumber = 0; 85 }
還有一個Tool工具類配合使用那么上代碼吧:
1 #pragma once
2 #include <QObject>
3 #include <QPainter>
4 #include <QPixmap>
5 #include <qsize.h>
6 class Tool :public QObject 7 { 8 Q_OBJECT 9 public: 10 Tool(); 11 ~Tool(); 12 static void setPixMapTransparency( QPixmap &pix, int transparencyNumber=100); 13 static void setPixMapRotate(QPixmap &pix, int rotateNumber = 0); 14 };
定義為靜態的主要是因為方便調用..................
工具類的Cpp文件:
1 nclude "Tool.h"
2 Tool::Tool() 3 { 4 } 5 //設置圖片旋轉度數
6 void Tool::setPixMapRotate(QPixmap &pix, int rotateNumber ){ 7 int imageWidth = pix.width(); 8 int imageHeight = pix .height(); 9
10 QPixmap temp(pix.size()); 11 temp.fill(Qt::transparent); 12 QPainter painter(&temp); 13 painter.setRenderHint(QPainter::SmoothPixmapTransform, true); 14 painter.translate(imageWidth / 2, imageHeight / 2); //讓圖片的中心作為旋轉的中心
15 painter.rotate(rotateNumber); //順時針旋轉90度
16 painter.translate(-(imageWidth / 2), -(imageHeight / 2)); //使原點復原
17 painter.drawPixmap(0, 0, pix); 18 painter.end(); 19 pix = temp; 20 } 21 //設置圖片透明度
22 void Tool::setPixMapTransparency( QPixmap &pix,int transparencyNumber){ 23 QPixmap temp(pix.size()); 24 temp.fill(Qt::transparent); 25 QPainter imagePain(&temp); 26 imagePain.setCompositionMode(QPainter::CompositionMode_Source); 27 imagePain.drawPixmap(0, 0, pix); 28 imagePain.setCompositionMode(QPainter::CompositionMode_DestinationIn); 29 imagePain.fillRect(temp.rect(), QColor(0, 0, 0, transparencyNumber)); 30 imagePain.end(); 31 pix = temp; 32 } 33
34 Tool::~Tool() 35 { 36 }
一切都是這么麻煩,當然既然是C++那么麻煩也是一件好事。
學習就像爬山,沒有爬不過去的山,只有不想爬的人。
歡迎大家一起來分享你的好的代碼。
-----------------------------------------------------------------------------------------華麗的結束線---------------------------------------------------------------------------------