searchcombobox.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #ifndef SEARCHCOMBOBOX_H
  2. #define SEARCHCOMBOBOX_H
  3. #include <QComboBox>
  4. #include <QMouseEvent>
  5. /**
  6. * @brief 1、使用此类绘制下拉框阴影需要在样式表中设置QAbstractItemView {margin: LISTVIEW_MARGIN;}
  7. * 否则阴影会被遮挡
  8. * 2、调用函数setViewShadowEffect()设置下拉框阴影,将下拉框阴影从构造函数中独立出去,单独设置,防止手动加载QSS时阴影不生效
  9. */
  10. class SearchComboBox : public QComboBox
  11. {
  12. Q_OBJECT
  13. public:
  14. typedef struct ItemData
  15. {
  16. QString text;
  17. QVariant userdata;
  18. QString alphbat;
  19. }ItemData;
  20. public:
  21. explicit SearchComboBox(QWidget *parent = nullptr);
  22. ~SearchComboBox();
  23. //重写下拉框弹出位置
  24. void showPopup() override;
  25. void hidePopup() override;
  26. /* 设置下拉框阴影 */
  27. void setViewShadowEffect();
  28. void clear();
  29. void addItem(const QString &text, const QVariant &userData = QVariant());
  30. void autoquery();
  31. protected:
  32. bool event(QEvent *event) override;
  33. void wheelEvent(QWheelEvent *e) override;
  34. private slots:
  35. void OnCurrentIndexChanged(const QString &text);
  36. private:
  37. const int LISTVIEW_MARGIN = 12; // QAbstractItemView边距(阴影宽度)
  38. QList<ItemData> m_items;
  39. bool m_autoquery;
  40. bool m_showPopup;
  41. QString m_currentText;
  42. };
  43. #endif // SEARCHCOMBOBOX_H