customcombobox.cpp 1.5 KB

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