Qt悬浮动态

粉丝悬浮动态,及抽奖程序

#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值