-
Notifications
You must be signed in to change notification settings - Fork 1
/
QtComboBoxWithTreeView.h
70 lines (63 loc) · 2.06 KB
/
QtComboBoxWithTreeView.h
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
#ifndef QTCOMBOBOXWITHTREEVIEW_H
#define QTCOMBOBOXWITHTREEVIEW_H
#include <QComboBox>
#include <QMouseEvent>
#include <QModelIndex>
#include <QAbstractItemView>
#include <QTreeView>
#include <QApplication>
/*
* From the hack provided in below url:
* http://www.developer.nokia.com/Community/Wiki/Implementing_QTreeView_in_QComboBox_using_Qt-_Part_2
*
* There are some bugs being worked on. Hopefully patch will reach Qt5
* and we can remove this file.
* https://bugreports.qt-project.org/browse/QTBUG-11660
* https://codereview.qt-project.org/#change,16716
*/
class QtComboBoxWithTreeView : public QComboBox
{
Q_OBJECT
public:
explicit QtComboBoxWithTreeView(QWidget *parent = 0) :
QComboBox(parent), skipNextHide(false)
{
QTreeView *view = new QTreeView(this);
view->setHeaderHidden(true);
setView(view);
view->viewport()->installEventFilter(this);
}
bool eventFilter(QObject* object, QEvent* event)
{
if (event->type() == QEvent::MouseButtonPress && object == view()->viewport())
{
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
QModelIndex index = view()->indexAt(mouseEvent->pos());
if (!view()->visualRect(index).contains(mouseEvent->pos()))
skipNextHide = true;
}
return false;
}
virtual void hidePopup()
{
if (skipNextHide) {
skipNextHide = false;
/*
* We are doing a big hack here.
* This is done in order to expand/shrink the popup
* as trees expand/shrink
*/
bool old = QApplication::isEffectEnabled(Qt::UI_AnimateCombo);
if (old)
QApplication::setEffectEnabled(Qt::UI_AnimateCombo, false);
QComboBox::hidePopup();
QComboBox::showPopup();
if (old)
QApplication::setEffectEnabled(Qt::UI_AnimateCombo, true);
} else
QComboBox::hidePopup();
}
private:
bool skipNextHide;
};
#endif // QTCOMBOBOXWITHTREEVIEW_H