QT按钮循环改变颜色
最近开始学习QT相关的知识,在编写程序的时候遇到一个问题是需要将每个按钮控件逐个显示。
如图:
在各个博客论坛上也没有寻找的有直接解决这类型问题的方法,最后自己琢磨出来了,在这里分享一下。(如果存在错误或者有更好的方法,欢迎批评指正)
首先在mainwindow.cpp文件下创建一个计时器(记得包含头文件)
QTimer *timer = new QTimer(this);
connect(timer,&QTimer::timeout,this,&::MainWindow::getNextButton);
timer->start(1000);
connect函数接收计时器timer的消息再调用getNextButton
这个槽函数。(每1000毫秒执行一次)
然后在mainwindow.h
的头文件里面编写getNextButton()
槽函数
void getNextButton() {
if (currentIndex==0){
ui->aButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->aButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==1){
ui->bButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->bButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==2){
ui->cButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->cButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==3){
ui->dButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->dButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==4){
ui->eButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->eButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==5){
ui->fButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->fButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==6){
ui->gButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->gButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==7){
ui->hButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->hButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==8){
ui->iButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->iButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==9){
ui->jButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->jButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==10){
ui->kButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->kButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==11){
ui->lButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->lButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==12){
ui->mButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->mButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==13){
ui->nButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->nButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==14){
ui->oButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->oButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==15){
ui->pButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->pButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==16){
ui->qButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->qButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==17){
ui->rButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->rButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==18){
ui->sButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->sButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==19){
ui->tButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->tButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==20){
ui->uButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->uButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==21){
ui->vButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->vButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==22){
ui->wButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->wButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==23){
ui->xButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->xButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==24){
ui->yButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->yButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
else if (currentIndex==25){
ui->zButton->setStyleSheet("background:red;");
QTimer::singleShot(500, [this]() mutable {
ui->zButton->setStyleSheet("");
});
currentIndex = (currentIndex + 1) % 26;
}
}
以aButton
这个控件为例:
首先通过if(currentIndex==0)
表示当前对aButton
按钮执行以下操作:
ui->aButton->setStyleSheet("background:red;")
通过setStyleSheet
方法将aButton
按钮背景设置为红色;QTimer::singleShot(500, [this]() mutable { ui->pButton->setStyleSheet(""); });
使用Qtimer
的singleShot
方法,再500毫秒后执行一个匿名函数,在这个函数中将aButton
按钮的样式设置为空,及恢复aButton
的默认样式。currentIndex = (currentIndex + 1) % 26;
最后对currentIndex
加1后对26进行取模,实现循环索引。
以上就是本文的主要类容了,在选择这个方法之前也尝试了许多其他方法,但是都失败了。目前这就是我解决这类问题的方法了。
如果存在错误或者有更好的方法,欢迎一起讨论。