customcombobox.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "customcombobox.h"
  2. #include <QApplication>
  3. #include <QGraphicsDropShadowEffect>
  4. #include <QListView>
  5. #include <QDebug>
  6. CustomComboBox::CustomComboBox(QWidget *parent)
  7. : QComboBox(parent)
  8. {
  9. }
  10. CustomComboBox::~CustomComboBox()
  11. {
  12. }
  13. /* 设置下拉框阴影 */
  14. void CustomComboBox::setViewShadowEffect()
  15. {
  16. setView(new QListView());
  17. if (nullptr != view() && nullptr != view()->window())
  18. {
  19. view()->window()->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
  20. view()->window()->setAttribute(Qt::WA_TranslucentBackground);
  21. QGraphicsDropShadowEffect *pShadowEffect = new QGraphicsDropShadowEffect(this);
  22. pShadowEffect->setBlurRadius(LISTVIEW_MARGIN); // 模糊度
  23. pShadowEffect->setColor(QColor(0, 0, 0, 90)); // 阴影的颜色
  24. pShadowEffect->setOffset(0, 0); // 水平和垂直偏移量
  25. view()->setGraphicsEffect(pShadowEffect);
  26. QApplication::setEffectEnabled(Qt::UI_AnimateCombo, false);
  27. }
  28. }
  29. void CustomComboBox::showPopup()
  30. {
  31. QComboBox::showPopup();
  32. if (nullptr != view()) {
  33. view()->setMinimumWidth(width() + LISTVIEW_MARGIN * 2);
  34. }
  35. QWidget *popup = findChild<QFrame*>();
  36. if (nullptr != popup) {
  37. popup->move(mapToGlobal(QPoint(-LISTVIEW_MARGIN, height() - LISTVIEW_MARGIN + 4)));
  38. }
  39. }