12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- #include "customcombobox.h"
- #include <QApplication>
- #include <QGraphicsDropShadowEffect>
- #include <QListView>
- #include <QDebug>
- #include <QStyleFactory>
- CustomComboBox::CustomComboBox(QWidget *parent)
- : QComboBox(parent)
- {
-
- }
- CustomComboBox::~CustomComboBox()
- {
- }
- /* 设置下拉框阴影 */
- void CustomComboBox::setViewShadowEffect()
- {
- /* 设置这个在Linux下,下拉框才会背景透明 */
- setStyle(QStyleFactory::create("Windows"));
- 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);
- }
- }
- 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)));
- }
- }
|