Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. update() function is not working in qopenlwidget properly
Qt 6.11 is out! See what's new in the release blog

update() function is not working in qopenlwidget properly

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 371 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Md Saif Khan
    wrote on last edited by SGaist
    #1

    I am new in qt platform. Here i have added my code. I trying to use mouse functionalities. I have added Slot and signal in my code. In slot, i have added update() function to update while i change something. Whenever i use update() function, the qopenglwidget window do not show anything which is drawn .

    
    MyOpenglWidget::MyOpenglWidget(QWidget *parent) : QOpenGLWidget(parent)
    {
        setFormat(QSurfaceFormat::defaultFormat());
    
    }
    void MyOpenglWidget::initializeGL()
    {
        initializeOpenGLFunctions(); // obvious
    
            buffer = QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
            buffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
            Q_ASSERT(buffer.create());
            Q_ASSERT(buffer.bind());
    
            shaderProg.addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
            shaderProg.addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
            Q_ASSERT(shaderProg.link());
    
            m_MVPMatrixLoc = shaderProg.uniformLocation("MVP");
            Q_ASSERT(shaderProg.bind());
    
            const int vPosition = 0;
    
            glVertexAttribPointer(vPosition, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    
            glEnableVertexAttribArray(vPosition);
    
    
    
    
    }
    
    void MyOpenglWidget::paintGL()
    {
      QTextStream cout(stdout);
      glClear(GL_COLOR_BUFFER_BIT);
    
      glClearColor(1.0, 1.0, 1.0, 1.0);
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glEnable(GL_DEPTH_TEST);
      glDepthFunc(GL_LESS);
    
    
      shaderProg.enableAttributeArray("vPosition");
    
      modelMatrix.setToIdentity();
    
      mvp = projectionMatrix * viewMatrix * modelMatrix;
    
      shaderProg.setUniformValue(m_MVPMatrixLoc, mvp);
    
      viewMatrix.lookAt(
                  QVector3D(x, y, z), // Camera is at (x,y,z), in World Space
                  QVector3D(x, y, 0), // and looks at (x,y)
                  QVector3D(0, 1, 0)  // Head is up (set to 0,-1,0 to look upside-down)
                  );
    
    
    
        for(unsigned int i=0;i<vec.size();i++){
            for(unsigned int j=0;j<vec[i].size();j++){
                buffer.allocate( vec[i].size() *sizeof (GLfloat));
                buffer.write(0, &vec[i][0], vec[i].size() *sizeof (GLfloat));
                glDrawArrays(GL_LINE_STRIP, 0, vec[i].size()/3);
                //cout<<i<<" "<<j<<" "<<vec[i].size()<<" \n";
            }
        }
    
    
        glFlush();
    
    }
    
    
    void MyOpenglWidget::resizeGL(int w, int h)
    {
          Q_UNUSED(w); Q_UNUSED(h);
          glViewport(0, 0, (GLint)width(), (GLint)height());
          projectionMatrix.perspective(45.0f, (float)w/(float)h, z, z/1000);
    }
    
    void MyOpenglWidget::mousePressEvent(QMouseEvent *event)
    {
        m_lastPos = event->pos();
    }
    
    void MyOpenglWidget::mouseMoveEvent(QMouseEvent *event)
    {
        int dx = event->position().x() - m_lastPos.x();
        int dy = event->position().y() - m_lastPos.y();
    
        if (event->buttons() & Qt::LeftButton) {
            setZRotation(m_zRot + 8 * dx);
        } 
        m_lastPos = event->pos();
    }
    
    void MyOpenglWidget::setZRotation(int angle) // slot
    {
        qNormalizeAngle(angle);
        if (angle != m_zRot) {
            m_zRot = angle;
            emit zRotationChanged(angle);
            update();
            qDebug()<< m_zRot;
        }
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You should take a look at the Hello GL 2 example to get started.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved