customcombobox.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "customcombobox.h"
  2. #include <QGraphicsDropShadowEffect>
  3. #include <QAbstractItemView>
  4. #include <QListView>
  5. #include <QTimer>
  6. #include <QDebug>
  7. #include <QStyleFactory>
  8. CustomComboBox::CustomComboBox(QWidget *parent)
  9. : QComboBox(parent)
  10. , m_nMargin(0)
  11. {
  12. setStyle(QStyleFactory::create("Windows"));
  13. setView(new QListView());
  14. view()->window()->setWindowFlags(Qt::Popup|Qt::FramelessWindowHint|Qt::NoDropShadowWindowHint);
  15. view()->window()->setAttribute(Qt::WA_TranslucentBackground);
  16. QGraphicsDropShadowEffect *pShadowEffect = new QGraphicsDropShadowEffect(this);
  17. pShadowEffect->setBlurRadius(10); // 模糊度
  18. pShadowEffect->setColor(QColor(0, 0, 0, 76)); // 阴影的颜色
  19. pShadowEffect->setOffset(0, 0); // 水平和垂直偏移量
  20. view()->setGraphicsEffect(pShadowEffect);
  21. QTimer::singleShot(0, this, [=]
  22. {
  23. view()->setMinimumWidth(width()+m_nMargin*2);
  24. });
  25. }
  26. CustomComboBox::~CustomComboBox()
  27. {
  28. qDebug()<<__func__;
  29. }
  30. void CustomComboBox::showPopup()
  31. {
  32. QComboBox::showPopup();
  33. QWidget *popup = findChild<QFrame*>();
  34. popup->move(mapToGlobal(QPoint(-m_nMargin, height())));
  35. }