-
Notifications
You must be signed in to change notification settings - Fork 364
/
MyComboBox.cpp
124 lines (103 loc) · 3.12 KB
/
MyComboBox.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "MyComboBox.h"
#include <QMouseEvent>
#include <QLineEdit>
#include <QStyleOptionViewItem>
#include <QStyle>
#include <QScrollBar>
#include <QDebug>
ComboView::ComboView(QWidget *parent)
: QListWidget(parent)
{
}
QRect ComboView::visualRect(const QModelIndex &index) const
{
QRect rect=QListWidget::visualRect(index);
int width=this->width();
if(verticalScrollBar()->isVisible()){
width-=verticalScrollBar()->width();
}
rect.setWidth(width);
return rect;
}
ComboItem::ComboItem(const QString &text, QWidget *parent)
: QWidget(parent),
textValue(text),
btn(new QPushButton(text,this))
{
QHBoxLayout *layout=new QHBoxLayout(this);
layout->addStretch();
layout->addWidget(btn);
layout->setMargin(0);
layout->setSpacing(0);
connect(btn,&QPushButton::clicked,[this]{
emit itemClicked(textValue);
});
}
ComboItem::~ComboItem()
{
qDebug()<<"~delete"<<textValue;
}
QString ComboItem::text() const
{
return textValue;
}
ComboDelegate::ComboDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
void ComboDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem opt(option);
//去掉焦点带来的虚线框
opt.state &= ~QStyle::State_HasFocus;
initStyleOption(&opt, index);
QStyledItemDelegate::paint(painter, opt, index);
}
MyComboBox::MyComboBox(QWidget *parent)
: QComboBox(parent),
itemList(new ComboView(this))
{
//itemList->setTextElideMode(Qt::ElideNone);
setModel(itemList->model());
setView(itemList);
setItemDelegate(new ComboDelegate(this));
}
void MyComboBox::setRemovableItems(const QStringList &items)
{
//combox的additem insertitem不是虚函数
//实现里时调用的model->insertRow,但是懒得再去重写listmodel-view,就新增一个接口
itemList->clear();
if(items.isEmpty())
return;
for(int i=0;i<items.count();i++)
{
QListWidgetItem* widget_item = new QListWidgetItem(itemList);
ComboItem *item=new ComboItem(items.at(i),itemList);
widget_item->setData(Qt::DisplayRole,items.at(i));
//widget_item->setData(Qt::TextAlignmentRole,int(Qt::AlignRight|Qt::AlignVCenter));
//itemList->addItem(widget_item);
itemList->setItemWidget(widget_item,item);
connect(item,&ComboItem::itemClicked,this,[this,item,widget_item](){
//take移除item后没有刷新弹框大小,干脆隐藏掉先
hidePopup();
itemList->takeItem(itemList->row(widget_item));
delete widget_item;
emit itemRemoved(item->text());
});
}
}
void MyComboBox::showPopup()
{
QComboBox::showPopup();
}
void MyComboBox::hidePopup()
{
QStyle * const style = this->style();
QStyleOptionComboBox opt;
initStyleOption(&opt);
view()->scrollTo(view()->currentIndex(),
style->styleHint(QStyle::SH_ComboBox_Popup, &opt, this)
? QAbstractItemView::PositionAtCenter
: QAbstractItemView::EnsureVisible);
QComboBox::hidePopup();
}