代码来自“QT小神童”视频
#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui/QWidget>
#include <QSystemTrayIcon>
#include <QTimer>
#include <QLabel>
#include <QPixmap>
#include <QApplication>
#include <QDesktopWidget>
#include <QList>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
protected slots:
void flash();
void handle(QSystemTrayIcon::ActivationReason);
void showPic();
private:
QSystemTrayIcon *sysIcon;
bool flag;
QTimer *timer2;
QLabel *label;
QList<QLabel *> list;
int i;
int j;
};
#endif // WIDGET_H
实现文件
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
flag=0;
i=0;
j=0;
sysIcon=new QSystemTrayIcon;
// sysIcon->setIcon(QIcon(":/heart.png"));
sysIcon->show();
connect(sysIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this,SLOT(handle(QSystemTrayIcon::ActivationReason)));
QTimer *timer1=new QTimer(this);
timer1->start(500);
connect(timer1,SIGNAL(timeout()),this,SLOT(flash()));
timer2=new QTimer(this);
connect(timer2,SIGNAL(timeout()),this,SLOT(showPic()));
}
Widget::~Widget()
{
}
void Widget::flash()
{
switch(flag)
{
case 0:{sysIcon->setIcon(QIcon(":/heart.png"));
flag=1;
break;
}
case 1:{sysIcon->setIcon(QIcon(":/123.png"));
flag=0;
break;
}
}
}
void Widget::handle(QSystemTrayIcon::ActivationReason reason)
{
switch(reason)
{
case QSystemTrayIcon::DoubleClick:
timer2->start(1000);
}
}
void Widget::showPic()
{
label=new QLabel;
label->setWindowFlags(Qt::FramelessWindowHint|Qt::SubWindow);
label->setAttribute(Qt::WA_DeleteOnClose);
QPixmap pix(":/heart.png");
label->setPixmap(pix);
label->resize(pix.rect().width(),pix.rect().height());
if(i*label->width()>QApplication::desktop()->screenGeometry().width())
{
i=0;
j++;
}
if((j+1)*label->height()>QApplication::desktop()->screenGeometry().height())
{
j=0;
for(int count=0;count< list.size();count++)
list[count]->close();
list.clear();
}
list.append(label);
label->setGeometry(0+i*label->width(),0+j*label->height(),label->width(),label->height());
label->show();
i++;
}
主程序文件
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
资源文件