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

时间:2025-01-24 11:35:40
#include "" #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%").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; // (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%").arg( i ); label->setPixmap(str); label->setScaledContents(true); label->setGeometry(0,0,82,86); }