粉丝悬浮动态,及抽奖程序
#include "masklabel.h"
MaskLabel::MaskLabel(int pos_x,QString fans_name,QWidget*parent)
:QLabel(parent)
{
this->setAlignment(Qt::AlignHCenter);//设置字体居中
this->setStyleSheet("color:white;font-size:20px");//设置字体颜色大小
animation_fans = new QPropertyAnimation(this,"pos");
animation_fans->setStartValue(QPoint(pos_x,900));//起始位置
animation_fans->setEndValue(QPoint(pos_x,-50));//结束位置
animation_fans->setDirection(QAbstractAnimation::Direction::Forward);
animation_fans->setDuration(15000);//时长15妙
animation_fans->start(QAbstractAnimation::DeleteWhenStopped);//动画结束后自动关闭,释放内存
this->setText("粉丝名称的长度设置初始化");
this->adjustSize();//自适应文字的长度
this->setFixedHeight(50);//设置高度
this->setText(fans_name);//导入粉丝的名称
QGraphicsOpacityEffect* pGra = new QGraphicsOpacityEffect(this);
pGra->setOpacity(0);
this->setGraphicsEffect(pGra);
QPropertyAnimation* animation_opa = new QPropertyAnimation(pGra,"opacity");
animation_opa->setDuration(2000);
animation_opa->setStartValue(0);
animation_opa->setEndValue(1);
animation_opa->start(QAbstractAnimation::DeleteWhenStopped);
}
MaskLabel::~MaskLabel()
{
}
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setStyleSheet("#frame{background-image:url(:/new/prefix1/res/pic/background.jpg)}");
mytime = new QTimer(this);
connect(mytime,SIGNAL(timeout()),this,SLOT(slot_timeout()));
QFile nameFile(":/new/prefix1/res/txt/fans_name.txt");
if(false == nameFile.open(QIODevice::ReadOnly))
{
return;
}
QTextStream toText(&nameFile);
toText.setCodec("GBK");
while (!nameFile.atEnd()) {
QString name = nameFile.readLine();
name_items << name.replace("\n","");
}
nameFile.close();
btn_items << ui->btn_1 << ui->btn_2 << ui->btn_3 << ui->btn_4;
for(int i = 0;i < btn_items.count();i++)
{
connect(btn_items[i],SIGNAL(clicked(bool)),this,SLOT(slot_btn_click()));
btn_items[i]->setStyleSheet("QPushButton{color:rgb(75,152,204);background-color:rgb(255,255,255);border-radius:20px}"
"QPushButton::hover{color:rgb(53,135,202)}");
btn_items[i]->hide();
}
mytime->start(10000);
ui->label_head->setStyleSheet("color:rgba(255,255,255,100)");
ui->label_head->setAlignment(Qt::AlignHCenter);
}
void Widget::qMsleep(int msec)
{
QTime dieTime = QTime::currentTime().addMSecs(msec);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 200);
}
void Widget::slot_timeout()
{
mytime->stop();
int pos_x;
int k = 0;
int arr_pox_x[11] = {20,220,420,620,820,1020,120,320,520,720,920};
for(int i = 0; i < name_items.count();i++)
{
pos_x = arr_pox_x[k++];
label_items << new MaskLabel(pos_x,name_items[i],this);
label_items[i]->show();
if(k == 6)
{
qMsleep(1500);
}else if(k == 11)
{
k = 0;
qMsleep(1500);
}
}
qMsleep(15000);
for(int i = 0;i < btn_items.count();i++)
{
btn_items[i]->show();
}
ui->label_head->setText("幸运粉丝");
ui->label_head->setStyleSheet("color:rgba(255,255,255,255)");
}
void Widget::slot_btn_click()
{
QPushButton* btn = qobject_cast<QPushButton*>(sender());
QTime randtime;
randtime = QTime::currentTime();
qsrand(randtime.msec() + randtime.second() * 1000);
int num = qrand()%name_items.count();
btn->setText(name_items[num]);
btn->setDisabled(true);
}
Widget::~Widget()
{
delete ui;
}