Incorrect viewport problem..

Hello,
I am working on a CAD type application,where i am using multiple QOpenglWidgets. I am trying to render a SAMPLE Triangle but the viewport position is incorrect.

//This the resize function-------------------------------------------------

void resizeEvent(QResizeEvent * event) 
{
    unsigned int width=event->size().width();
    unsigned int height=event->size().height();

    if (height == 0) height = 1;                
   GLfloat aspect = (GLfloat)width / (GLfloat)height;
   glViewport(0, 0, width, height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   
   if (width >= height)
   {
     // aspect >= 1, set the height from -1 to 1, with larger width
      gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
   } 
   else 
   {
      // aspect < 1, set the width to -1 to 1, with larger height
     gluOrtho2D(-1.0, 1.0, -1.0 / aspect, 1.0 / aspect);
   }
   glMatrixMode(GL_MODELVIEW);      
   glLoadIdentity();                
}

// And from PaintEvent i am calling this-------------------

void SAMPLE_OPENGL(float Ty)
{
  
     glClearColor(1,1,0,1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  qDebug("~~~~~~~~~~~~~TRIANGLE~~~~~~~~~~~~~~~~");
  
  glMatrixMode(GL_MODELVIEW);      // To operate on Model-View matrix
   glLoadIdentity();                // Reset the model-view matrix
 
    glBegin(GL_LINES); // sample X,Y axis (in immediate mode) (Just to check)
    glColor3f(1,0,0);              //X
      glVertex2f(-1.0f, 0.0f);
      glVertex2f( 3.0f, 0.0f);
    glColor3f(0,1,0);              //Y
      glVertex2f( 0.0f, 1.0f);
      glVertex2f( 0.0f, -1.0f);
   glEnd();
   
   glTranslatef(0, Ty, 0.0f); // Translate left and up
   glBegin(GL_TRIANGLES);          // Each set of 3 vertices form a triangle
      glColor3f(0.0f, 0.0f, 1.0f); // Blue
      glVertex2f(0.2f, 0.2f);
      glColor3f(1.0f, 0.0f, 0.0f); // Red
      glVertex2f(0.8f, 0.2f);
      glColor3f(0.0f, 1.0f, 0.0f); // Green
      glVertex2f(0.5f, 0.6f);
  glEnd();
  glPopMatrix();
 
}

//Note there are multiple QOpenglWidgets
//Here is the screenshot of output
Screenshot%20from%202019-03-08%2018-37-37

Please be sure to wrap your code in code tags to make it easier to read. You will also need to post what the problem is that you are having, what you have tried and not tried. I suggest your re-read the Forum Posting Guidelines. These will help you get an accurate answer.

Override the resizeGL() and paintGL() methods rather than the generic QWidget methods.

QOpenGLWidget overrides paintEvent() and resizeEvent(), and if you override those in a subclass you need to re-implement the relevant functionality from the QOpenGLWidget methods, and you aren’t doing that.

Yah That worked…i replaced resizeEvent() and paintevent()…with resizeGL() and paintGL() respectively. Thank you so much!!!