Qt递归遍历子控件的按钮并设置辉光(阴影)效果

#include "mainwindow.h"

#include 
#include 
#include 
#include 

void applyEffectToButtons(QWidget *widget, const QColor& color) {
    // 使用 findChildren 查找所有 QPushButton
    QList buttons = widget->findChildren();


    // 为所有按钮应用效果
    for (QPushButton *button : buttons) {
        // 创建 QGraphicsDropShadowEffect 实例
        QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
        effect->setColor(color);
        effect->setBlurRadius(10);
        effect->setOffset(0, 0);

        button->setGraphicsEffect(effect);
    }

    // 递归处理子控件
    for (QObject *child : widget->children()) {
        if (child->isWidgetType()) {
            applyEffectToButtons(qobject_cast(child), color);
        }
    }
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;

    QVBoxLayout laout;
    QWidget wzt;
    wzt.setLayout(&laout);
    QPushButton button("HelloWorld");
    QPushButton button1("HelloWorld");
    QPushButton button2("HelloWorld");

    laout.addWidget(&button);
    laout.addWidget(&button1);
    laout.addWidget(&button2);

    w.setCentralWidget(&wzt);

    applyEffectToButtons(&w, Qt::black);
    w.show();
    return a.exec();
}

你可能感兴趣的:(qt,数据库,服务器)