关于qt实现指示灯图片闪烁问题(换了一种方法已经解决)

定时器前面稍短时间能正常显示,但是后面定时器就卡住了,甚至系统崩溃。

void Widget::onPushButtonclicked()
{
    timer = new QTimer(this);
    timer->stop();
    connect(timer,SIGNAL(timeout()),this,SLOT(timerOut()));
    timer->start(500);
}

void Widget::timerOut()
{
    qDebug() << "11";

    label->setPixmap(QPixmap("C:\\*****"));
    label->setScaledContents(true);
    label->setGeometry(30,30,82,86);
    label->setVisible(true);
    timer->stop();
    //timer->start(2000);
    //label->setVisible(false); //标签不可见

    timer->start(500);
    connect(timer,SIGNAL(timeout()),this,SLOT(timerOut1()));
    qDebug() << "22";
}

void Widget::timerOut1()
{
    //label->setVisible(false); //标签不可见
    label->close();
    timer->stop();

    timer->start(500);
    connect(timer,SIGNAL(timeout()),this,SLOT(timerOut()));
}

新的方法::

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

#include <QPalette>
#include <QPainter>
#include <QDebug>
#include <QString>
#include <QKeyEvent>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    QPalette pal;
    pal.setBrush(QPalette::Window,Qt::black);
    setPalette(pal);

    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(setTimer()));

    connect(ui->startButton,SIGNAL(clicked(bool)),this,SLOT(onStartButtonclicked()));
    connect(ui->stopButton,SIGNAL(clicked(bool)),this,SLOT(onStopButtonclicked()));

    label = new QLabel(this);

}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *event)
{

    QPainter painter(this);
    painter.drawPixmap(0,0,82,86,QPixmap("C:\\Qt\\text\\text8\\image\\icon_02.png"));


    qDebug() << "i:12121212:" ;
}

void Widget::keyPressEvent(QKeyEvent *event) //键盘事件
{
    QString str = QString("C:\\Qt\\text\\text8\\image\\icon_color_0%1.png").arg( 2 );
    label->setPixmap(str);
    label->setScaledContents(true);


    switch ( event->key() )
    {
        case Qt::Key_Left : timer->start(500); break;
        case Qt::Key_2 : label->setGeometry(0,0,82,86); break;
    }
}

void Widget::onStopButtonclicked()
{
    timer->stop();
    label->clear();
    delete label;
    return;
//    QPainter painter;
//    painter.drawPixmap(0,0,82,86,QPixmap("C:\\Qt\\text\\text8\\image\\icon_color_02.png"));

}

void Widget::onStartButtonclicked()
{
    //timer->start(500);

    static int flag = 1; //设置标志位,flag为1时按钮显示"开始",为0时显示"暂停"
    if( flag == 1 ){
        ui->startButton->setText(tr("暂停"));
        timer->start(300);//启动计时器
        flag = 0;
    }
    else{
        ui->startButton->setText(tr("开始"));
        timer->stop();
        flag = 1;
        label->clear();
    }

}

void Widget::setTimer()
{

    timer->start(1000);
    static int i = 1;
    qDebug() << "i::" << i++;
    if( i == 3 ){
        i = 1;
    }
    QString str = QString("C:\\Qt\\text\\text8\\image\\icon_color_0%1.png").arg( i );
    label->setPixmap(str);
    label->setScaledContents(true);
    label->setGeometry(0,0,82,86);

}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值