shadowwidget.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "shadowwidget.h"
  2. #include <QLayout>
  3. #include <QGraphicsDropShadowEffect>
  4. #include <QDebug>
  5. ShadowWidget::ShadowWidget(QWidget *parent)
  6. : QWidget(parent)
  7. , m_pCentralWidget(new QWidget(this))
  8. {
  9. setWindowFlags(Qt::FramelessWindowHint);
  10. setAttribute(Qt::WA_TranslucentBackground);
  11. QHBoxLayout* pLayout = new QHBoxLayout();
  12. if (nullptr != pLayout) {
  13. pLayout->addWidget(m_pCentralWidget);
  14. this->setLayout(pLayout);
  15. }
  16. QGraphicsDropShadowEffect *pShadowEffect = new QGraphicsDropShadowEffect(this);
  17. pShadowEffect->setBlurRadius(16); // 模糊度
  18. pShadowEffect->setColor(QColor(0, 0, 0, 90)); // 阴影的颜色
  19. pShadowEffect->setOffset(0, 0); // 水平和垂直偏移量
  20. m_pCentralWidget->setGraphicsEffect(pShadowEffect);
  21. }
  22. void ShadowWidget::setCentralLayout(QLayout *layout)
  23. {
  24. if (nullptr != m_pCentralWidget) {
  25. m_pCentralWidget->setLayout(layout);
  26. }
  27. }
  28. QLayout* ShadowWidget::getLayout() const
  29. {
  30. if (nullptr != m_pCentralWidget) {
  31. return m_pCentralWidget->layout();
  32. }
  33. return nullptr;
  34. }