Qt按钮自定义特效buttonEffects

相关资料:不记得是在哪里看到的了,不好意思了原作者。

.pro

 1 QT       += core gui
 2 
 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 4 
 5 CONFIG += c++11
 6 
 7 # The following define makes your compiler emit warnings if you use
 8 # any Qt feature that has been marked deprecated (the exact warnings
 9 # depend on your compiler). Please consult the documentation of the
10 # deprecated API in order to know how to port your code away from it.
11 DEFINES += QT_DEPRECATED_WARNINGS
12 
13 # You can also make your code fail to compile if it uses deprecated APIs.
14 # In order to do so, uncomment the following line.
15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
17 
18 SOURCES += 
19     main.cpp 
20     mainwindow.cpp
21 
22 HEADERS += 
23     mainwindow.h
24 
25 FORMS +=
26 
27 TARGET = buttonEffects
28 
29 # Default rules for deployment.
30 qnx: target.path = /tmp/$${TARGET}/bin
31 else: unix:!android: target.path = /opt/$${TARGET}/bin
32 !isEmpty(target.path): INSTALLS += target
View Code
main.cpp
 1 #include <QApplication>
 2 #include <mainwindow.h>
 3 
 4 int main(int argc, char *argv[])
 5 {
 6     QApplication a(argc, argv);
 7     TMainWnd window;
 8     window.show();
 9     window.resize(800,500);
10     return a.exec();
11 }
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QWidget>
 5 #include <QPushButton>
 6 #include <QVBoxLayout>
 7 #include <QTimerEvent>
 8 
 9 class TPushBtn2 : public QPushButton
10 {
11     Q_OBJECT
12 public:
13     explicit TPushBtn2(QWidget *parent = 0);
14     TPushBtn2(const QString  &,QWidget *parent = 0);
15     void UpdateAlpha();
16 signals:
17 public slots:
18 protected:
19     void timerEvent(QTimerEvent *e);
20     void enterEvent(QEvent *);
21     void leaveEvent(QEvent *);
22     void changeMe();
23 private:
24     int _alpha;
25     int _alphaTimer;
26     qint8 _nDirection;
27 };
28 
29 class TMainWnd : public QWidget
30 {
31     Q_OBJECT
32 public:
33     explicit TMainWnd(QWidget *parent = 0);
34 signals:
35 public slots:
36 };
37 #endif // MAINWINDOW_H
View Code

mainwindow.cpp

 1 #include "mainwindow.h"
 2 
 3 TMainWnd::TMainWnd(QWidget *parent) :
 4     QWidget(parent)
 5 {
 6     QVBoxLayout *mainLO = new QVBoxLayout;
 7     for(int i = 0 ; i < 10 ; i ++){
 8         TPushBtn2 *btn  = new TPushBtn2(tr("Test %1").arg(i));
 9         mainLO->addWidget(btn );
10     }
11     // style()->drawControl(.drawPushButton(this, paint);
12     setLayout(mainLO);
13 }
14 
15 TPushBtn2::TPushBtn2(QWidget *parent) :
16     QPushButton(parent)
17 {}
18 
19 TPushBtn2::TPushBtn2(const QString &text, QWidget *parent)
20     :QPushButton(text,parent),_nDirection(1)
21 {}
22 
23 void TPushBtn2::timerEvent(QTimerEvent *e)
24 {
25     changeMe();
26 }
27 
28 void TPushBtn2::enterEvent(QEvent *)
29 {
30     _alpha = 255;
31     _nDirection = 1;
32     changeMe();
33 }
34 
35 void TPushBtn2::leaveEvent(QEvent *)
36 {
37     if(_alphaTimer) killTimer(_alphaTimer);
38     _nDirection = -1;
39     _alphaTimer = startTimer(100);
40 }
41 
42 void TPushBtn2::changeMe()
43 {
44     _alpha += 45 * _nDirection;
45 
46     QPalette pal =this-> palette();
47 
48     QColor cr  = pal.window().color();
49 
50     if(_alpha >= 255){
51       if(_alphaTimer)  killTimer(_alphaTimer);
52         _alphaTimer = 0;
53         _alpha =  255 ;
54     }    else if( _alpha <= 0) {
55         killTimer(_alphaTimer);
56         _alphaTimer = 0;
57         _alpha =  0 ;
58     }
59     cr.setRed(_alpha);
60     cr.setBlue(_alpha);
61     pal.setColor(QPalette::All,QPalette::Button , cr);
62 
63    this-> setPalette(pal);
64 }
View Code
原文地址:https://www.cnblogs.com/FKdelphi/p/13405490.html