-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhintcombobox.cpp
94 lines (86 loc) · 2.11 KB
/
hintcombobox.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "hintcombobox.h"
HintList::HintList(QWidget* parent)
: QListWidget(parent)
{
}
HintComboBox::HintComboBox(QWidget* parent)
: QComboBox(parent)
{
installEventFilter(this);
setEditable(true);
items << QString("默认");
setStringList(items);
list = new HintList(parent);
list->hide();
isMustHint=true;
setView(new QListView());
connect(this->lineEdit(), &QLineEdit::textEdited, this, &HintComboBox::showHint);
connect(list, &QListWidget::itemClicked, this, &HintComboBox::selectOne);
}
void HintComboBox::setNotMustHint(bool isNotMust)
{
isMustHint=!isNotMust;
}
void HintComboBox::selectOne()
{
if (list->count() >= 1)
this->setCurrentText(list->currentItem()->text());
list->hide();
}
void HintComboBox::flushList()
{
list->clear();
QString key = this->currentText();
for (QString s : items) {
if (s.contains(key)) {
list->addItem(s);
}
}
if (isMustHint && list->count() <= 0)
list->addItem(QString("无结果"));
}
void HintComboBox::showHint()
{
flushList();
if(!isMustHint && ((list->count())<=0 || this->currentText().size()==0) ){
list->hide();
}
else
{
if (list->isHidden()) {
int x = this->x() + 4;
int y = this->y() + this->height();
int w = this->width();
int h = 100;
list->setGeometry(x, y, w, h);
list->setCurrentRow(0);
list->show();
}
list->raise();
if (this->currentText().compare(list->item(0)->text()) == 0) {
list->hide();
}
}
}
bool HintComboBox::eventFilter(QObject* o, QEvent* e)
{
if (QEvent::MouseButtonPress == e->type()) //窗口停用
{
if (o != list && !list->isHidden()) {
list->hide();
}
}
return QWidget::eventFilter(o, e);
}
void HintComboBox::setStringList(QStringList sl)
{
if (sl.size() == 0) {
sl << QString("默认");
this->clear();
this->addItems(sl);
} else {
items = sl;
this->clear();
this->addItems(sl);
}
}