77#include < QCoreApplication>
88#include < math.h>
99
10- bool GLWidget ::m_transparent = false ;
10+ bool MyGLWidget ::m_transparent = false ;
1111
12- GLWidget::GLWidget (QWidget *parent)
12+ MyGLWidget::MyGLWidget (QWidget *parent)
1313 : QOpenGLWidget(parent)
1414{
1515 m_core = QSurfaceFormat::defaultFormat ().profile () == QSurfaceFormat::CoreProfile;
@@ -25,17 +25,17 @@ GLWidget::GLWidget(QWidget *parent)
2525 setAttribute (Qt::WA_AlwaysStackOnTop);
2626}
2727
28- GLWidget ::~GLWidget ()
28+ MyGLWidget ::~MyGLWidget ()
2929{
3030 cleanup ();
3131}
3232
33- QSize GLWidget ::minimumSizeHint () const
33+ QSize MyGLWidget ::minimumSizeHint () const
3434{
3535 return QSize (50 , 50 );
3636}
3737
38- QSize GLWidget ::sizeHint () const
38+ QSize MyGLWidget ::sizeHint () const
3939{
4040 return QSize (400 , 400 );
4141}
@@ -48,7 +48,7 @@ static void qNormalizeAngle(int &angle)
4848 angle -= 360 * 16 ;
4949}
5050
51- void GLWidget ::setXRotation (int angle)
51+ void MyGLWidget ::setXRotation (int angle)
5252{
5353 qNormalizeAngle (angle);
5454 if (angle != m_xRot) {
@@ -58,7 +58,7 @@ void GLWidget::setXRotation(int angle)
5858 }
5959}
6060
61- void GLWidget ::setYRotation (int angle)
61+ void MyGLWidget ::setYRotation (int angle)
6262{
6363 qNormalizeAngle (angle);
6464 if (angle != m_yRot) {
@@ -68,7 +68,7 @@ void GLWidget::setYRotation(int angle)
6868 }
6969}
7070
71- void GLWidget ::setZRotation (int angle)
71+ void MyGLWidget ::setZRotation (int angle)
7272{
7373 qNormalizeAngle (angle);
7474 if (angle != m_zRot) {
@@ -78,7 +78,7 @@ void GLWidget::setZRotation(int angle)
7878 }
7979}
8080
81- void GLWidget ::cleanup ()
81+ void MyGLWidget ::cleanup ()
8282{
8383 if (m_program == nullptr ){
8484 return ;
@@ -88,7 +88,7 @@ void GLWidget::cleanup()
8888 delete m_program;
8989 m_program = nullptr ;
9090 doneCurrent ();
91- QObject::disconnect (context (), &QOpenGLContext::aboutToBeDestroyed, this , &GLWidget ::cleanup);
91+ QObject::disconnect (context (), &QOpenGLContext::aboutToBeDestroyed, this , &MyGLWidget ::cleanup);
9292}
9393
9494static const char *vertexShaderSourceCore =
@@ -146,7 +146,7 @@ static const char *fragmentShaderSource =
146146 " gl_FragColor = vec4(col, 1.0);\n "
147147 " }\n " ;
148148
149- void GLWidget ::initializeGL ()
149+ void MyGLWidget ::initializeGL ()
150150{
151151 // In this example the widget's corresponding top-level window can change
152152 // several times during the widget's lifetime. Whenever this happens, the
@@ -155,7 +155,7 @@ void GLWidget::initializeGL()
155155 // aboutToBeDestroyed() signal, instead of the destructor. The emission of
156156 // the signal will be followed by an invocation of initializeGL() where we
157157 // can recreate all resources.
158- connect (context (), &QOpenGLContext::aboutToBeDestroyed, this , &GLWidget ::cleanup, Qt::UniqueConnection);
158+ connect (context (), &QOpenGLContext::aboutToBeDestroyed, this , &MyGLWidget ::cleanup, Qt::UniqueConnection);
159159
160160 initializeOpenGLFunctions ();
161161 glClearColor (0 , 0 , 0 , m_transparent ? 0 : 1 );
@@ -198,7 +198,7 @@ void GLWidget::initializeGL()
198198 m_program->release ();
199199}
200200
201- void GLWidget ::setupVertexAttribs ()
201+ void MyGLWidget ::setupVertexAttribs ()
202202{
203203 m_logoVbo.bind ();
204204 QOpenGLFunctions *f = QOpenGLContext::currentContext ()->functions ();
@@ -211,7 +211,7 @@ void GLWidget::setupVertexAttribs()
211211 m_logoVbo.release ();
212212}
213213
214- void GLWidget ::paintGL ()
214+ void MyGLWidget ::paintGL ()
215215{
216216 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
217217 glEnable (GL_DEPTH_TEST);
@@ -234,21 +234,31 @@ void GLWidget::paintGL()
234234 m_program->release ();
235235}
236236
237- void GLWidget ::resizeGL (int w, int h)
237+ void MyGLWidget ::resizeGL (int w, int h)
238238{
239239 m_proj.setToIdentity ();
240240 m_proj.perspective (45 .0f , GLfloat (w) / h, 0 .01f , 100 .0f );
241241}
242242
243- void GLWidget ::mousePressEvent (QMouseEvent *event)
243+ void MyGLWidget ::mousePressEvent (QMouseEvent *event)
244244{
245+ #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
245246 m_lastPos = event->position ().toPoint ();
247+ #else
248+ m_lastPos = event->pos ();
249+ #endif
246250}
247251
248- void GLWidget ::mouseMoveEvent (QMouseEvent *event)
252+ void MyGLWidget ::mouseMoveEvent (QMouseEvent *event)
249253{
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 ();
252262
253263 if (event->buttons () & Qt::LeftButton) {
254264 setXRotation (m_xRot + 8 * dy);
@@ -257,5 +267,5 @@ void GLWidget::mouseMoveEvent(QMouseEvent *event)
257267 setXRotation (m_xRot + 8 * dy);
258268 setZRotation (m_zRot + 8 * dx);
259269 }
260- m_lastPos = event-> position (). toPoint () ;
270+ m_lastPos = pos ;
261271}
0 commit comments