searchcombobox.cpp 4.0 KB

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