7
7
#include < QCoreApplication>
8
8
#include < math.h>
9
9
10
- bool GLWidget ::m_transparent = false ;
10
+ bool MyGLWidget ::m_transparent = false ;
11
11
12
- GLWidget::GLWidget (QWidget *parent)
12
+ MyGLWidget::MyGLWidget (QWidget *parent)
13
13
: QOpenGLWidget(parent)
14
14
{
15
15
m_core = QSurfaceFormat::defaultFormat ().profile () == QSurfaceFormat::CoreProfile;
@@ -25,17 +25,17 @@ GLWidget::GLWidget(QWidget *parent)
25
25
setAttribute (Qt::WA_AlwaysStackOnTop);
26
26
}
27
27
28
- GLWidget ::~GLWidget ()
28
+ MyGLWidget ::~MyGLWidget ()
29
29
{
30
30
cleanup ();
31
31
}
32
32
33
- QSize GLWidget ::minimumSizeHint () const
33
+ QSize MyGLWidget ::minimumSizeHint () const
34
34
{
35
35
return QSize (50 , 50 );
36
36
}
37
37
38
- QSize GLWidget ::sizeHint () const
38
+ QSize MyGLWidget ::sizeHint () const
39
39
{
40
40
return QSize (400 , 400 );
41
41
}
@@ -48,7 +48,7 @@ static void qNormalizeAngle(int &angle)
48
48
angle -= 360 * 16 ;
49
49
}
50
50
51
- void GLWidget ::setXRotation (int angle)
51
+ void MyGLWidget ::setXRotation (int angle)
52
52
{
53
53
qNormalizeAngle (angle);
54
54
if (angle != m_xRot) {
@@ -58,7 +58,7 @@ void GLWidget::setXRotation(int angle)
58
58
}
59
59
}
60
60
61
- void GLWidget ::setYRotation (int angle)
61
+ void MyGLWidget ::setYRotation (int angle)
62
62
{
63
63
qNormalizeAngle (angle);
64
64
if (angle != m_yRot) {
@@ -68,7 +68,7 @@ void GLWidget::setYRotation(int angle)
68
68
}
69
69
}
70
70
71
- void GLWidget ::setZRotation (int angle)
71
+ void MyGLWidget ::setZRotation (int angle)
72
72
{
73
73
qNormalizeAngle (angle);
74
74
if (angle != m_zRot) {
@@ -78,7 +78,7 @@ void GLWidget::setZRotation(int angle)
78
78
}
79
79
}
80
80
81
- void GLWidget ::cleanup ()
81
+ void MyGLWidget ::cleanup ()
82
82
{
83
83
if (m_program == nullptr ){
84
84
return ;
@@ -88,7 +88,7 @@ void GLWidget::cleanup()
88
88
delete m_program;
89
89
m_program = nullptr ;
90
90
doneCurrent ();
91
- QObject::disconnect (context (), &QOpenGLContext::aboutToBeDestroyed, this , &GLWidget ::cleanup);
91
+ QObject::disconnect (context (), &QOpenGLContext::aboutToBeDestroyed, this , &MyGLWidget ::cleanup);
92
92
}
93
93
94
94
static const char *vertexShaderSourceCore =
@@ -146,7 +146,7 @@ static const char *fragmentShaderSource =
146
146
" gl_FragColor = vec4(col, 1.0);\n "
147
147
" }\n " ;
148
148
149
- void GLWidget ::initializeGL ()
149
+ void MyGLWidget ::initializeGL ()
150
150
{
151
151
// In this example the widget's corresponding top-level window can change
152
152
// several times during the widget's lifetime. Whenever this happens, the
@@ -155,7 +155,7 @@ void GLWidget::initializeGL()
155
155
// aboutToBeDestroyed() signal, instead of the destructor. The emission of
156
156
// the signal will be followed by an invocation of initializeGL() where we
157
157
// can recreate all resources.
158
- connect (context (), &QOpenGLContext::aboutToBeDestroyed, this , &GLWidget ::cleanup, Qt::UniqueConnection);
158
+ connect (context (), &QOpenGLContext::aboutToBeDestroyed, this , &MyGLWidget ::cleanup, Qt::UniqueConnection);
159
159
160
160
initializeOpenGLFunctions ();
161
161
glClearColor (0 , 0 , 0 , m_transparent ? 0 : 1 );
@@ -198,7 +198,7 @@ void GLWidget::initializeGL()
198
198
m_program->release ();
199
199
}
200
200
201
- void GLWidget ::setupVertexAttribs ()
201
+ void MyGLWidget ::setupVertexAttribs ()
202
202
{
203
203
m_logoVbo.bind ();
204
204
QOpenGLFunctions *f = QOpenGLContext::currentContext ()->functions ();
@@ -211,7 +211,7 @@ void GLWidget::setupVertexAttribs()
211
211
m_logoVbo.release ();
212
212
}
213
213
214
- void GLWidget ::paintGL ()
214
+ void MyGLWidget ::paintGL ()
215
215
{
216
216
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
217
217
glEnable (GL_DEPTH_TEST);
@@ -234,21 +234,31 @@ void GLWidget::paintGL()
234
234
m_program->release ();
235
235
}
236
236
237
- void GLWidget ::resizeGL (int w, int h)
237
+ void MyGLWidget ::resizeGL (int w, int h)
238
238
{
239
239
m_proj.setToIdentity ();
240
240
m_proj.perspective (45 .0f , GLfloat (w) / h, 0 .01f , 100 .0f );
241
241
}
242
242
243
- void GLWidget ::mousePressEvent (QMouseEvent *event)
243
+ void MyGLWidget ::mousePressEvent (QMouseEvent *event)
244
244
{
245
+ #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
245
246
m_lastPos = event->position ().toPoint ();
247
+ #else
248
+ m_lastPos = event->pos ();
249
+ #endif
246
250
}
247
251
248
- void GLWidget ::mouseMoveEvent (QMouseEvent *event)
252
+ void MyGLWidget ::mouseMoveEvent (QMouseEvent *event)
249
253
{
250
- int dx = event->position ().toPoint ().x () - m_lastPos.x ();
251
- int dy = event->position ().toPoint ().y () - m_lastPos.y ();
254
+
255
+ #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
256
+ QPoint pos = event->position ().toPoint ();
257
+ #else
258
+ QPoint pos = event->pos ();
259
+ #endif
260
+ int dx = pos.x () - m_lastPos.x ();
261
+ int dy = pos.y () - m_lastPos.y ();
252
262
253
263
if (event->buttons () & Qt::LeftButton) {
254
264
setXRotation (m_xRot + 8 * dy);
@@ -257,5 +267,5 @@ void GLWidget::mouseMoveEvent(QMouseEvent *event)
257
267
setXRotation (m_xRot + 8 * dy);
258
268
setZRotation (m_zRot + 8 * dx);
259
269
}
260
- m_lastPos = event-> position (). toPoint () ;
270
+ m_lastPos = pos ;
261
271
}
0 commit comments