关于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
#include
#include
#include
#include

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);

}

发布了12 篇原创文章 · 获赞 3 · 访问量 1401
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览