#include "mainwindow.h"#include <QApplication>
#include <QPushButton>
#include <QGraphicsDropShadowEffect>
#include <QVBoxLayout>void applyEffectToButtons(QWidget *widget, const QColor& color) {// 使用 findChildren 查找所有 QPushButtonQList<QPushButton *> buttons = widget->findChildren<QPushButton *>();// 为所有按钮应用效果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<QWidget *>(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();
}