12345678910111213141516171819202122232425262728293031323334353637383940 |
- #include "customcombobox.h"
- #include <QGraphicsDropShadowEffect>
- #include <QAbstractItemView>
- #include <QListView>
- #include <QTimer>
- #include <QDebug>
- #include <QStyleFactory>
- CustomComboBox::CustomComboBox(QWidget *parent)
- : QComboBox(parent)
- , m_nMargin(0)
- {
- setStyle(QStyleFactory::create("Windows"));
- setView(new QListView());
- view()->window()->setWindowFlags(Qt::Popup|Qt::FramelessWindowHint|Qt::NoDropShadowWindowHint);
- view()->window()->setAttribute(Qt::WA_TranslucentBackground);
- QGraphicsDropShadowEffect *pShadowEffect = new QGraphicsDropShadowEffect(this);
- pShadowEffect->setBlurRadius(10); // 模糊度
- pShadowEffect->setColor(QColor(0, 0, 0, 76)); // 阴影的颜色
- pShadowEffect->setOffset(0, 0); // 水平和垂直偏移量
- view()->setGraphicsEffect(pShadowEffect);
- QTimer::singleShot(0, this, [=]
- {
- view()->setMinimumWidth(width()+m_nMargin*2);
- });
- }
- CustomComboBox::~CustomComboBox()
- {
- qDebug()<<__func__;
- }
- void CustomComboBox::showPopup()
- {
- QComboBox::showPopup();
- QWidget *popup = findChild<QFrame*>();
- popup->move(mapToGlobal(QPoint(-m_nMargin, height())));
- }
|