Qt 高仿QQ窗口抖动效果【原】

//头文件YShakeWidget.h
#ifndef YSHAKEWIDGET_H
#define YSHAKEWIDGET_H
#include <QWidget>
class QTimer;
class YShakeWidget : public QWidget
{
    Q_OBJECT
    
public:
    explicit YShakeWidget(QWidget *parent = 0);
private slots:
    void slot_btnTestclicked();
    void slot_timerOut();
    
private:
    QTimer* m_timer;
    int m_nPosition;
    QPoint m_curPos;
};
#endif // YSHAKEWIDGET_H

//实现文件YShakeWidget.cpp
#include "YShakeWidget.h"
#include <QTimer>
#include <QHBoxLayout>
#include <QPushButton>
enum {MaxLimitTimes = 12};
enum {MaxLimitSpace = 5};
YShakeWidget::YShakeWidget(QWidget *parent) :
    QWidget(parent),
    m_timer(new QTimer(this)),
    m_nPosition(0),
    m_curPos(QPoint())
{
    QPushButton* btnTest = new QPushButton(tr("Test"),this);
    bool b = connect(btnTest,SIGNAL(clicked()),this,SLOT(slot_btnTestclicked()));
    Q_ASSERT(b);
    QHBoxLayout* hBox = new QHBoxLayout(this);
    hBox->addWidget(btnTest);
    this->setLayout(hBox);
    // 计时器
    b = connect(m_timer,SIGNAL(timeout()),this,SLOT(slot_timerOut()));
    Q_ASSERT(b);
    m_timer->setInterval(40);
}
void YShakeWidget::slot_btnTestclicked()
{
    m_nPosition = 0;
    m_curPos = this->pos();
    m_timer->start();
}
void YShakeWidget::slot_timerOut()
{
    m_timer->stop();
    if(m_nPosition < MaxLimitTimes)
    {
        ++m_nPosition;
        switch(m_nPosition%4)
        {
        case 1:
        {
            QPoint tmpPos(m_curPos.x(),m_curPos.y()-MaxLimitSpace);
            this->move(tmpPos);
        }
            break;
        case 2:
        {
            QPoint tmpPos(m_curPos.x()-MaxLimitSpace,m_curPos.y()-MaxLimitSpace);
            this->move(tmpPos);
        }
            break;
        case 3:
        {
            QPoint tmpPos(m_curPos.x()-MaxLimitSpace,m_curPos.y());
            this->move(tmpPos);
        }
            break;
        default:
        case 0:
            this->move(m_curPos);
            break;
        }
        m_timer->start();
    }
}

//main.cpp文件
#include <QtGui/QApplication>
#include "YShakeWidget.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    YShakeWidget w;
    w.show();
    
    return a.exec();
}

你可能感兴趣的:(源代码,qt,窗口抖动,闪屏,震屏)