QT按钮循环改变颜色

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(""); });使用QtimersingleShot方法,再500毫秒后执行一个匿名函数,在这个函数中将aButton按钮的样式设置为空,及恢复aButton的默认样式。
  • currentIndex = (currentIndex + 1) % 26;最后对currentIndex加1后对26进行取模,实现循环索引。

以上就是本文的主要类容了,在选择这个方法之前也尝试了许多其他方法,但是都失败了。目前这就是我解决这类问题的方法了。

如果存在错误或者有更好的方法,欢迎一起讨论。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt中,你可以轻松地为按钮添加单击事件处理程序,使得按钮被点击后颜色发生变化。以下是步骤和基本示例: 1. **包含所需库**: 在`.pro`文件或`.cpp`文件中,确保包含了所需的`QApplication`、`QPushButton`和可能的`QtWidgets`库。 2. **创建按钮并设置样式**: 创建一个QPushButton实例,并为其定义初始的颜色。例如: ```cpp QPushButton* button = new QPushButton("点击我", this); button->setStyleSheet("background-color: blue;"); ``` 这里将按钮背景设为蓝色。 3. **添加点击事件处理函数**: 使用`QObject::connect()`方法连接按钮的`clicked()`信号到一个槽函数(slot function),该槽函数会更改按钮颜色: ```cpp connect(button, &QPushButton::clicked, [button](){ // 改变颜色的部分 QColor initialColor = button->palette().color(QPalette::Button); // 获取当前颜色 QColor newColor = initialColor.darker(); // 或者用其他方式改变颜色,比如更亮或不同颜色 button->setStyleSheet("background-color: " + newColor.name()); // 更新样式 }); ``` 当按钮被点击时,这个槽函数会被调用,使按钮颜色变化。 4. **完整示例**: ```cpp #include <QApplication> #include <QPushButton> #include <QPalette> int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget window; window.resize(200, 100); QPushButton button("点击我", &window); button.move(50, 50); button.setStyleSheet("background-color: blue;"); QObject::connect(&button, &QPushButton::clicked, [button](){ QColor initialColor = button.palette().color(QPalette::Button); QColor newColor = initialColor.darker(); button.setStyleSheet("background-color: " + newColor.name()); }); window.show(); return app.exec(); } ``` 以上代码中,每次按钮被点击,按钮颜色都会暗淡一些。你可以根据需要修改颜色变化的方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值