Qt4.5实现类似于QQ弹出消息

widget.h:

#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
#include
#include

class Widget : public QWidget
{
     Q_OBJECT
public:
     Widget();
     virtual ~Widget();
public slots:
     void PopUp();
private:
     QLabel *m_pLabel;
     QTimer *m_pTimer;
     QRect m_Rect;
};
#endif // WIDGET_H

widget.cpp:

#include
#include
#include
#include "widget.h"

int x,y;

Widget::Widget()
{

//    QPalette pal = palette();
//    pal.setColor(QPalette::Background, QColor(255,0,0,200));
//    setPalette(pal);
    extern int x,y;
    setWindowFlags(Qt::Dialog);
    setFixedSize(300, 170);
    setWindowTitle(trUtf8("Information!"));
    m_Rect = QApplication::desktop()->rect();
    x = m_Rect.right() - width() - 2;
    y = m_Rect.bottom() - 33;
    m_pLabel = new QLabel(this);
    m_pLabel->setWindowFlags(Qt::FramelessWindowHint);
    m_pLabel->setAttribute(Qt::WA_TranslucentBackground,false);
    m_pLabel->setAutoFillBackground(true);

    m_pLabel->setText(trUtf8("My Informatin:........pop up!..................!"));
    QHBoxLayout * layout = new QHBoxLayout(this);
    layout->addWidget(m_pLabel);

    setLayout(layout);
    //QTimer control PopUp
    m_pTimer = new QTimer();
    connect(m_pTimer, SIGNAL(timeout()), this, SLOT(PopUp()));
    //set popup time
    m_pTimer->start(20);
    PopUp();
}

Widget::~Widget()
{

}

void Widget::PopUp()
{
     extern int x,y;
     if (y >= m_Rect.bottom() - height() - 33)
         y -= 1;
     else
         m_pTimer->stop();
     setGeometry(x, y, height(), width());
}

main.cpp:

#include
#include "widget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

代码很详细了,就不多说了。

你可能感兴趣的:(QT)