customcombobox.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. setView(new QListView());
  10. if (nullptr != view() && nullptr != view()->window()) {
  11. view()->window()->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
  12. view()->window()->setAttribute(Qt::WA_TranslucentBackground);
  13. QGraphicsDropShadowEffect *pShadowEffect = new QGraphicsDropShadowEffect(this);
  14. pShadowEffect->setBlurRadius(LISTVIEW_MARGIN); // 模糊度
  15. pShadowEffect->setColor(QColor(0, 0, 0, 90)); // 阴影的颜色
  16. pShadowEffect->setOffset(0, 0); // 水平和垂直偏移量
  17. view()->setGraphicsEffect(pShadowEffect);
  18. QApplication::setEffectEnabled(Qt::UI_AnimateCombo, false);
  19. }
  20. }
  21. CustomComboBox::~CustomComboBox()
  22. {
  23. }
  24. void CustomComboBox::showPopup()
  25. {
  26. QComboBox::showPopup();
  27. if (nullptr != view()) {
  28. view()->setMinimumWidth(width() + LISTVIEW_MARGIN * 2);
  29. }
  30. QWidget *popup = findChild<QFrame*>();
  31. if (nullptr != popup) {
  32. popup->move(mapToGlobal(QPoint(-LISTVIEW_MARGIN, height() - LISTVIEW_MARGIN + 4)));
  33. }
  34. }