-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
191 lines (158 loc) · 5.24 KB
/
main.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <QtWidgets>
#define MAX_COLUMN_COUNT 30
#include "customtableheader.h"
class HeaderObject : public QWidget{
QGridLayout *layout;
QComboBox *comboBox;
QCheckBox *checkBox;
public:
HeaderObject(QWidget *parent = 0) : QWidget(parent){
comboBox = new QComboBox(this);
comboBox->addItem("test");
comboBox->addItem("test2");
comboBox->addItem("test3");
comboBox->addItem("test4");
checkBox = new QCheckBox(this);
checkBox->setChecked(true);
layout = new QGridLayout;
layout->addWidget(comboBox);
layout->addWidget(checkBox);
setLayout(layout);
}
};
class CustomHeader : public QHeaderView{
public:
CustomHeader(QWidget *parent = 0):QHeaderView(Qt::Horizontal, parent){
for(int i = 0; i < MAX_COLUMN_COUNT; i++){
headerSections.insert(i,new HeaderObject(this));
headerSections[i]->hide();
}
//setFont(QFont("Helvetica [Cronyx]", 32));
//setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
setMinimumSectionSize(headerSections[0]->minimumSizeHint().width());
setDefaultSectionSize(headerSections[0]->minimumSizeHint().width());
}
protected:
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
if (!rect.isValid())
return;
//qDebug() << logicalIndex;
//qDebug() << "QRect: " << rect << " AND OFFSET:" << offset();
headerSections[logicalIndex]->setGeometry(rect);
headerSections[logicalIndex]->show();
}
QSize sizeHint() const
{
QSize s = size();
s.setHeight(headerSections[0]->minimumSizeHint().height());
return s;
}
private:
mutable QVector< QPointer <HeaderObject> > headerSections;
};
class CustomModel : public QAbstractTableModel
{
public:
CustomModel() : QAbstractTableModel(){
}
int rowCount(const QModelIndex &parent = QModelIndex()) const
{
return 10;
}
int columnCount(const QModelIndex &parent = QModelIndex()) const
{
return 20;
}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
{
if (role == Qt::DisplayRole) return QVariant();
return QVariant();
}
};
class MyHorizontalHeader : public QHeaderView{
public:
QComboBox *boxes[MAX_COLUMN_COUNT];
MyHorizontalHeader(QWidget *parent = 0) : QHeaderView(Qt::Horizontal, parent)
{
connect(this, SIGNAL(sectionResized(int, int, int)), this,
SLOT(handleSectionResized(int)));
connect(this, SIGNAL(sectionMoved(int, int, int)), this,
SLOT(handleSectionMoved(int, int, int)));
setSectionsMovable(true);
for (int i = 0; i < MAX_COLUMN_COUNT; i++)
{
boxes[i] = NULL;
}
}
void showEvent(QShowEvent *e)
{
for (int i=0;i<count();i++) {
if (!boxes[i]) {
QComboBox *box = new QComboBox(this);
box->addItem("test 1");
box->addItem("test 2");
boxes[i] = box;
}
boxes[i]->setGeometry(sectionViewportPosition(i), 0,
sectionSize(i) - 5, height());
boxes[i]->show();
}
QHeaderView::showEvent(e);
}
void handleSectionResized(int i)
{
for (int j=visualIndex(i);j<count();j++) {
int logical = logicalIndex(j);
boxes[logical]->setGeometry(sectionViewportPosition(logical), 0,
sectionSize(logical) - 5, height());
}
}
void handleSectionMoved(int logical, int oldVisualIndex, int newVisualIndex)
{
for (int i=qMin(oldVisualIndex, newVisualIndex);i<count();i++){
int logical = logicalIndex(i);
boxes[logical]->setGeometry(sectionViewportPosition(logical), 0,
sectionSize(logical) - 5, height());
}
}
void fixComboPositions()
{
for (int i = 0; i<count();i++)
boxes[i]->setGeometry(sectionViewportPosition(i), 0,
sectionSize(i) - 5, height());
}
}; // class MyHorizontalHeader
class MyQTableView : public QTableView {
public:
MyHorizontalHeader *horizHeader;
CustomHeader *cusHeader;
CheckedHeader *checkHeader;
MyQTableView(QWidget * parent = 0) : QTableView(parent)
{
horizHeader = new MyHorizontalHeader;
setHorizontalHeader(horizHeader);
//cusHeader = new CustomHeader(this);
//setHorizontalHeader(cusHeader);
//checkHeader = new CheckedHeader(Qt::Horizontal, this);
//setHorizontalHeader(checkHeader);
}
void scrollContentsBy(int dx, int dy)
{
QTableView::scrollContentsBy(dx, dy);
if (dx != 0)
horizHeader->fixComboPositions();
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CustomModel *model = new CustomModel;
MyQTableView *view = new MyQTableView();
view->setModel(model);
//view->setHorizontalHeader(new CustomHeader(view));
//view->setHorizontalHeader(new MyHorizontalHeader(view));
view->setMinimumSize(400, 200);
view->show();
return a.exec();
}