forked from AlexK168/CourseProject2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mytablewidget.cpp
65 lines (55 loc) · 2.42 KB
/
mytablewidget.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
#include "mytablewidget.h"
MyTableWidget::MyTableWidget(QWidget *parent) : QTableWidget (parent) {
setEditTriggers(QAbstractItemView::NoEditTriggers);
}
MyTableWidget::MyTableWidget(int rows, int cols, QWidget *parent) : QTableWidget (rows, cols, parent) {
setEditTriggers(QAbstractItemView::NoEditTriggers);
}
void MyTableWidget::wheelEvent(QWheelEvent *event) {
focusCol = horizontalScrollBar()->value() + horizontalScrollBar()->pageStep() / 2;
focusRow = verticalScrollBar()->value() + verticalScrollBar()->pageStep() / 2;
QFont font = horizontalHeader()->font();
int ceilSize = verticalHeader()->defaultSectionSize();
int fontSize = font.pointSize();
if (event->delta() > 0 && fontSize < 9) {
font.setPointSize(fontSize + 1);
setFont(font);
horizontalHeader()->setDefaultSectionSize(ceilSize + 4);
verticalHeader()->setDefaultSectionSize(ceilSize + 4);
} else if (event->delta() <= 0 && fontSize > 1) {
font.setPointSize(fontSize - 1);
setFont(font);
horizontalHeader()->setDefaultSectionSize(ceilSize - 4);
verticalHeader()->setDefaultSectionSize(ceilSize - 4);
}
QCoreApplication::postEvent(this, new QEvent(QEvent::Type::User));
}
bool MyTableWidget::event(QEvent *event) {
if (event->type() == QEvent::Type::User){
int newHorValue = focusCol - horizontalScrollBar()->pageStep() / 2;
int newVerValue = focusRow - verticalScrollBar()->pageStep() / 2;
horizontalScrollBar()->setValue(newHorValue);
verticalScrollBar()->setValue(newVerValue);
return true;
}
return QTableWidget::event(event);
}
void MyTableWidget::mouseMoveEvent(QMouseEvent * event) {
int currentRow = rowAt(event->y());
int currentCol = columnAt(event->x());
int currentPos = horizontalScrollBar()->value();
horizontalScrollBar()->setValue(currentPos - currentCol + prevCol);
currentPos = verticalScrollBar()->value();
verticalScrollBar()->setValue(currentPos - currentRow + prevRow);
}
void MyTableWidget::mousePressEvent(QMouseEvent * event) {
prevCol = columnAt(event->x());
prevRow = rowAt(event->y());
}
void MyTableWidget::normalize(){
QFont font = horizontalHeader()->font();
font.setPointSize(9);
setFont(font);
horizontalHeader()->setDefaultSectionSize(47);
verticalHeader()->setDefaultSectionSize(47);
}