1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- #ifndef SEARCHCOMBOBOX_H
- #define SEARCHCOMBOBOX_H
- #include <QComboBox>
- #include <QMouseEvent>
- /**
- * @brief 1、使用此类绘制下拉框阴影需要在样式表中设置QAbstractItemView {margin: LISTVIEW_MARGIN;}
- * 否则阴影会被遮挡
- * 2、调用函数setViewShadowEffect()设置下拉框阴影,将下拉框阴影从构造函数中独立出去,单独设置,防止手动加载QSS时阴影不生效
- */
- class SearchComboBox : public QComboBox
- {
- Q_OBJECT
- public:
- typedef struct ItemData
- {
- QString text;
- QVariant userdata;
- QString alphbat;
- }ItemData;
- public:
- explicit SearchComboBox(QWidget *parent = nullptr);
- ~SearchComboBox();
- //重写下拉框弹出位置
- void showPopup() override;
- void hidePopup() override;
- /* 设置下拉框阴影 */
- void setViewShadowEffect();
- void clear();
- void addItem(const QString &text, const QVariant &userData = QVariant());
- void autoquery();
- protected:
- bool event(QEvent *event) override;
- void wheelEvent(QWheelEvent *e) override;
- private slots:
- void OnCurrentIndexChanged(const QString &text);
- private:
- const int LISTVIEW_MARGIN = 12; // QAbstractItemView边距(阴影宽度)
- QList<ItemData> m_items;
- bool m_autoquery;
- bool m_showPopup;
- QString m_currentText;
- };
- #endif // SEARCHCOMBOBOX_H
|