16,212
社区成员
发帖
与我相关
我的任务
分享
///tmainwnd.h
#ifndef TMAINWND_H
#define TMAINWND_H
#include <QWidget>
#include <QPushButton>
class TPushBtn2 : public QPushButton
{
Q_OBJECT
public:
explicit TPushBtn2(QWidget *parent = 0);
TPushBtn2(const QString &,QWidget *parent = 0);
void UpdateAlpha();
signals:
public slots:
protected:
void timerEvent(QTimerEvent *e);
void enterEvent(QEvent *);
void leaveEvent(QEvent *);
void changeMe();
private:
int _alpha;
int _alphaTimer;
qint8 _nDirection;
};
class TMainWnd : public QWidget
{
Q_OBJECT
public:
explicit TMainWnd(QWidget *parent = 0);
signals:
public slots:
};
#endif // TMAINWND_H
///tmainwnd.cpp
#include <QVBoxLayout>
#include <QTimerEvent>
#include "tmainwnd.h"
TMainWnd::TMainWnd(QWidget *parent) :
QWidget(parent)
{
QVBoxLayout *mainLO = new QVBoxLayout;
for(int i = 0 ; i < 10 ; i ++){
TPushBtn2 *btn = new TPushBtn2(tr("Test %1").arg(i));
mainLO->addWidget(btn );
}
// style()->drawControl(.drawPushButton(this, paint);
setLayout(mainLO);
}
TPushBtn2::TPushBtn2(QWidget *parent) :
QPushButton(parent)
{}
TPushBtn2::TPushBtn2(const QString &text, QWidget *parent)
:QPushButton(text,parent),_nDirection(1)
{}
void TPushBtn2::timerEvent(QTimerEvent *e)
{
changeMe();
}
void TPushBtn2::enterEvent(QEvent *)
{
_alpha = 255;
_nDirection = 1;
changeMe();
}
void TPushBtn2::leaveEvent(QEvent *)
{
if(_alphaTimer) killTimer(_alphaTimer);
_nDirection = -1;
_alphaTimer = startTimer(100);
}
void TPushBtn2::changeMe()
{
_alpha += 45 * _nDirection;
QPalette pal =this-> palette();
QColor cr = pal.window().color();
if(_alpha >= 255){
if(_alphaTimer) killTimer(_alphaTimer);
_alphaTimer = 0;
_alpha = 255 ;
} else if( _alpha <= 0) {
killTimer(_alphaTimer);
_alphaTimer = 0;
_alpha = 0 ;
}
cr.setRed(_alpha);
cr.setBlue(_alpha);
pal.setColor(QPalette::All,QPalette::Button , cr);
this-> setPalette(pal);
}
////main.cpp
#include <QApplication>
#include "tmainwnd.h"
int main(int argc, char **argv)
{
QApplication app(argc,argv);
TMainWnd window;
window.show();
window.resize(800,500);
return app.exec();
}