-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathwheelbox.cpp
167 lines (132 loc) · 4.9 KB
/
wheelbox.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
#include "wheelbox.h"
#include <qwt_wheel.h>
#include <qlcdnumber.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qevent.h>
#include <qapplication.h>
#include <qdebug.h>
/* 此事件滤波对象的作用是想在整个WheelBox组件区域(包括LCD控件区域)滚动鼠标滚轮时,QwtWheel的值都能改变 */
class Wheel: public QwtWheel
{
public:
Wheel( WheelBox *parent ):
QwtWheel( parent )
{
setFocusPolicy( Qt::WheelFocus );
parent->installEventFilter( this );
}
virtual bool eventFilter( QObject *object, QEvent *event )
{
if ( event->type() == QEvent::Wheel )
{
const QWheelEvent *we = static_cast<QWheelEvent *>( event );
/*
* void QwtWheel::wheelEvent( QWheelEvent *event )
* {
* if ( !wheelRect().contains( event->pos() ) )
* {
* event->ignore();
* return;
* }
* ………………
* }
*
* 以上代码是从QwtWheel里面摘抄出来的,可以看出有ignore的分支
* 看看QPoint( 5, 5 )什么意思?
*/
QWheelEvent wheelEvent( QPoint( 5, 5 ), we->delta(),
we->buttons(), we->modifiers(),
we->orientation() );
/* 注释掉此行代码,在wheel上面滚动滚轮,程序就不会崩溃了 */
/*
* 原因分析,若在Wheel的wheelEvent()函数中ignore了Event,则Event会继续向其父祖件WheelBox传播,
* 而传向WheelBox的Event会首先进入此事件滤波函数,然后又会通过sendEvent()函数发送给Wheel,再被
* ignore,再传给父祖件WheelBox……陷入死循环,从而程序崩溃。
*/
// QApplication::sendEvent( this, &wheelEvent );
/* WXLmjr于2016-12-11日屏蔽上一行代码,修改为下面这行代码,解决程序崩溃问题,
* 但直接调用event()函数和调用sendEvent()函数有什么区别?导致了崩溃的问题?
*/
this->event(&wheelEvent);
return true;
}
return QwtWheel::eventFilter( object, event );
}
// virtual void wheelEvent( QWheelEvent *event)
// {
// static int i = 0;
//// event->accept();
//// event->ignore();
// QwtWheel::wheelEvent(event);
// qDebug() << i++;
// }
};
WheelBox::WheelBox( const QString &title,
double min, double max, double stepSize, QWidget *parent ):
QWidget( parent )
{
d_number = new QLCDNumber( this );
d_number->setSegmentStyle( QLCDNumber::Filled );
d_number->setAutoFillBackground( true );
d_number->setFixedHeight( d_number->sizeHint().height() * 2 );
d_number->setFocusPolicy( Qt::WheelFocus );
QPalette pal( Qt::black );
pal.setColor( QPalette::WindowText, Qt::green );
d_number->setPalette( pal );
d_wheel = new Wheel( this );
d_wheel->setOrientation( Qt::Vertical );
d_wheel->setInverted( true );
d_wheel->setRange( min, max );
d_wheel->setSingleStep( stepSize );
d_wheel->setPageStepCount( 5 );
d_wheel->setFixedHeight( d_number->height() );
d_number->setFocusProxy( d_wheel );
QFont font( "Helvetica", 10 );
font.setBold( true );
d_label = new QLabel( title, this );
d_label->setFont( font );
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->setContentsMargins( 0, 0, 0, 0 );
hLayout->setSpacing( 2 );
hLayout->addWidget( d_number, 10 );
hLayout->addWidget( d_wheel );
QVBoxLayout *vLayout = new QVBoxLayout( this );
vLayout->addLayout( hLayout, 10 );
vLayout->addWidget( d_label, 0, Qt::AlignTop | Qt::AlignHCenter );
connect( d_wheel, SIGNAL( valueChanged( double ) ),
d_number, SLOT( display( double ) ) );
connect( d_wheel, SIGNAL( valueChanged( double ) ),
this, SIGNAL( valueChanged( double ) ) );
}
void WheelBox::setTheme( const QColor &color )
{
d_wheel->setPalette( color );
}
QColor WheelBox::theme() const
{
return d_wheel->palette().color( QPalette::Window );
}
void WheelBox::setValue( double value )
{
d_wheel->setValue( value );
d_number->display( value );
}
double WheelBox::value() const
{
return d_wheel->value();
}
//bool WheelBox::eventFilter( QObject *object, QEvent *event )
//{
// if ( event->type() == QEvent::Wheel )
// {
// const QWheelEvent *we = static_cast<QWheelEvent *>( event );
// QWheelEvent wheelEvent( QPoint( 5, 5 ), we->delta(),
// we->buttons(), we->modifiers(),
// we->orientation() );
// /* 注释掉此行代码,在wheel上面滚动滚轮,程序就不会崩溃了 */
// QApplication::sendEvent( d_wheel, &wheelEvent );
// return true;
// }
// return QwtWheel::eventFilter( object, event );
//}