searchcombobox.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "searchcombobox.h"
  2. #include "wordtopinyin.h"
  3. #include <QStyleFactory>
  4. #include <QListView>
  5. #include <QApplication>
  6. #include <QGraphicsDropShadowEffect>
  7. #include <QTimer>
  8. SearchComboBox::SearchComboBox(QWidget *parent)
  9. : QComboBox(parent)
  10. , m_autoquery(false)
  11. , m_showPopup(false)
  12. {
  13. setStyle(QStyleFactory::create("Windows"));
  14. QTimer::singleShot(0, this, [=]
  15. {
  16. view()->setMinimumWidth(width() + LISTVIEW_MARGIN * 2);
  17. });
  18. setEditable(true);
  19. setCompleter(nullptr);
  20. //输入后,开始查询
  21. connect(this, (void (QComboBox::*)(const QString &))&QComboBox::currentIndexChanged, this, &SearchComboBox::OnCurrentIndexChanged);
  22. }
  23. SearchComboBox::~SearchComboBox()
  24. {
  25. // qDebug()<<__func__;
  26. }
  27. void SearchComboBox::showPopup()
  28. {
  29. // QComboBox::showPopup();
  30. // QWidget *popup = findChild<QFrame*>();
  31. // popup->move(mapToGlobal(QPoint( - LISTVIEW_MARGIN, height() - LISTVIEW_MARGIN)));
  32. // m_showPopup = true;
  33. QComboBox::showPopup();
  34. if (nullptr != view()) {
  35. view()->setMinimumWidth(width() + LISTVIEW_MARGIN * 2);
  36. }
  37. QWidget *popup = findChild<QFrame*>();
  38. if (nullptr != popup) {
  39. popup->move(mapToGlobal(QPoint(-LISTVIEW_MARGIN, height() - LISTVIEW_MARGIN + 4)));
  40. }
  41. m_showPopup = true;
  42. }
  43. void SearchComboBox::hidePopup()
  44. {
  45. QComboBox::hidePopup();
  46. m_showPopup = false;
  47. }
  48. /* 设置下拉框阴影 */
  49. void SearchComboBox::setViewShadowEffect()
  50. {
  51. setView(new QListView());
  52. view()->window()->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
  53. view()->window()->setAttribute(Qt::WA_TranslucentBackground);
  54. //取消拉下动画
  55. QApplication::setEffectEnabled(Qt::UI_AnimateCombo, false);
  56. //设置阴影
  57. QGraphicsDropShadowEffect *pShadowEffect = new QGraphicsDropShadowEffect(this);
  58. pShadowEffect->setBlurRadius(10); // 模糊度
  59. pShadowEffect->setColor(QColor(0, 0, 0, 60)); // 阴影的颜色
  60. pShadowEffect->setOffset(0, 0); // 水平和垂直偏移量
  61. view()->setGraphicsEffect(pShadowEffect);
  62. }
  63. void SearchComboBox::wheelEvent(QWheelEvent *e)
  64. {
  65. Q_UNUSED(e);
  66. //屏蔽鼠标滚动
  67. }
  68. bool SearchComboBox::event(QEvent *event)
  69. {
  70. //添加一个鼠标滚轮拦截来禁用combox滚轮改变CurrentIndex
  71. if(event->type()==QEvent::Wheel)
  72. {
  73. return true;
  74. }
  75. else if(event->type() == QEvent::KeyPress)
  76. {
  77. QKeyEvent* ev = (QKeyEvent*)event;
  78. if(ev->key() == Qt::Key_Enter || ev->key() == Qt::Key_Return){//回车开始查找匹配的数据
  79. if(isEditable()){
  80. autoquery();
  81. m_autoquery = false;
  82. }
  83. }
  84. }
  85. else if(event->type() == QEvent::MouseButtonPress)
  86. {
  87. //点击下拉框,恢复到源数据集
  88. }
  89. else if(event->type() == QEvent::FocusOut)
  90. {
  91. //离开焦点时,编辑框自动填充当前选择项的文本
  92. if(!m_showPopup)
  93. {
  94. if(!m_currentText.isEmpty())
  95. {
  96. setCurrentText(m_currentText);
  97. }
  98. else
  99. {
  100. int index = currentIndex();
  101. if(index >= 0 && index < m_items.count())
  102. {
  103. setCurrentText(m_items[index].text);
  104. }
  105. }
  106. update();
  107. }
  108. }
  109. return QComboBox::event(event);
  110. }
  111. void SearchComboBox::OnCurrentIndexChanged(const QString &text)
  112. {
  113. m_currentText = text;
  114. }
  115. void SearchComboBox::addItem(const QString &text, const QVariant &userData)
  116. {
  117. ItemData item;
  118. item.text = text;
  119. item.userdata = userData;
  120. item.alphbat = WordToPinyin::firstPinyin(item.text);
  121. m_items.append(item);
  122. QComboBox::addItem(item.text, item.userdata);
  123. }
  124. void SearchComboBox::clear()
  125. {
  126. m_items.clear();
  127. QComboBox::clear();
  128. }
  129. void SearchComboBox::autoquery()
  130. {
  131. QString text = currentText().toLower();
  132. QComboBox::clear();
  133. if(text.isEmpty())
  134. {
  135. //显示源数据集
  136. foreach(ItemData item,m_items)
  137. {
  138. QComboBox::addItem(item.text, item.userdata);
  139. }
  140. }
  141. else
  142. {
  143. //显示匹配的数据集
  144. foreach(ItemData item, m_items)
  145. {
  146. if(item.alphbat.toLower().contains(text) || item.text.toLower().contains(text))
  147. {
  148. QComboBox::addItem(item.text, item.userdata);
  149. }
  150. }
  151. //没匹配到任何数据
  152. //....
  153. }
  154. showPopup();
  155. }