customcombobox.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #ifndef _CUSTOMCOMBOBOX_H_
  2. #define _CUSTOMCOMBOBOX_H_
  3. #include <QComboBox>
  4. /**
  5. * @brief 1、使用此类绘制下拉框阴影需要在样式表中设置QAbstractItemView {margin: LISTVIEW_MARGIN;}
  6. * 否则阴影会被遮挡
  7. * 2、调用函数setViewShadowEffect()设置下拉框阴影
  8. * 3、调用setViewShadowEffect()前,需要先设置样式表
  9. */
  10. class CustomComboBox : public QComboBox
  11. {
  12. public:
  13. explicit CustomComboBox(QWidget *parent = nullptr);
  14. ~CustomComboBox() override;
  15. /* 设置下拉框阴影,需要先设置样式表,再调用此函数 */
  16. void setViewShadowEffect();
  17. //重写下拉框弹出位置
  18. void showPopup() override;
  19. /* 禁用滚轮修改内容 */
  20. void setWheelDisabled(bool disabled = true) { m_wheelDisabled = disabled; }
  21. protected:
  22. void wheelEvent(QWheelEvent *event) override;
  23. private:
  24. const int LISTVIEW_MARGIN = 12; // QAbstractItemView边距(阴影宽度)
  25. bool m_wheelDisabled = false; // 是否禁用滚轮修改内容
  26. };
  27. #endif // _CUSTOMCOMBOBOX_H_