123456789101112131415161718192021222324252627282930313233343536373839 |
- #include "customcombobox.h"
- #include <QApplication>
- #include <QGraphicsDropShadowEffect>
- #include <QListView>
- #include <QDebug>
- CustomComboBox::CustomComboBox(QWidget *parent)
- : QComboBox(parent)
- {
- setView(new QListView());
- if (nullptr != view() && nullptr != view()->window()) {
- view()->window()->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
- view()->window()->setAttribute(Qt::WA_TranslucentBackground);
- QGraphicsDropShadowEffect *pShadowEffect = new QGraphicsDropShadowEffect(this);
- pShadowEffect->setBlurRadius(LISTVIEW_MARGIN); // 模糊度
- pShadowEffect->setColor(QColor(0, 0, 0, 90)); // 阴影的颜色
- pShadowEffect->setOffset(0, 0); // 水平和垂直偏移量
- view()->setGraphicsEffect(pShadowEffect);
- QApplication::setEffectEnabled(Qt::UI_AnimateCombo, false);
- }
- }
- CustomComboBox::~CustomComboBox()
- {
- }
- void CustomComboBox::showPopup()
- {
- QComboBox::showPopup();
- if (nullptr != view()) {
- view()->setMinimumWidth(width() + LISTVIEW_MARGIN * 2);
- }
- QWidget *popup = findChild<QFrame*>();
- if (nullptr != popup) {
- popup->move(mapToGlobal(QPoint(-LISTVIEW_MARGIN, height() - LISTVIEW_MARGIN + 4)));
- }
- }
|