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
|
#include <uibase/widgetutility.h>
#include <uibase/eventfilter.h>
#include <QCheckBox>
#include <QHeaderView>
#include <QMenu>
#include <QWidgetAction>
namespace MOBase
{
void onHeaderContextMenu(QTreeView* view, const QPoint& pos)
{
auto* header = view->header();
QMenu menu;
// display a list of all headers as checkboxes
QAbstractItemModel* model = header->model();
for (int i = 1; i < model->columnCount(); ++i) {
const QString columnName = model->headerData(i, Qt::Horizontal).toString();
auto* checkBox = new QCheckBox(&menu);
checkBox->setText(columnName);
checkBox->setChecked(!header->isSectionHidden(i));
// Enable the checkbox if 1) the section is visible, or 2) the
// EnabledColumnRole is not found, or 3) the value for the role is true.
auto display = model->headerData(i, Qt::Horizontal, EnabledColumnRole);
checkBox->setEnabled(!header->isSectionHidden(i) || !display.isValid() ||
display.toBool());
auto* checkableAction = new QWidgetAction(&menu);
checkableAction->setDefaultWidget(checkBox);
QObject::connect(checkBox, &QCheckBox::clicked, [=] {
header->setSectionHidden(i, !checkBox->isChecked());
});
menu.addAction(checkableAction);
}
menu.exec(header->viewport()->mapToGlobal(pos));
}
void setCustomizableColumns(QTreeView* view)
{
auto* header = view->header();
header->setSectionsMovable(true);
header->setContextMenuPolicy(Qt::CustomContextMenu);
QObject::connect(header, &QWidget::customContextMenuRequested, view,
[view](auto&& pos) {
onHeaderContextMenu(view, pos);
});
}
} // namespace MOBase
|