1
votes

I want to draw in QOpenGLWidget, processing data and call repaint() in mousePressEvent() function:

QPoint point[2];
QImage img=QImage(1000,800,QImage::Format_Grayscale8);
void Widget::mousePressEvent(QMouseEvent *event)
{
    point[0]=event->pos();
    point[1]=point[0]+QPoint(10,10);
    repaint();
}

Now repaint widget in paintGL():

void Widget::paintGL()
{  
    vao->release();
    vbo->release();

    QPainter painter;
    painter.begin(image);
    painter.drawLine(point[0],point[1]);
    painter.end();
    painter.begin(this);
    painter.drawImage(0,0,*del->tmpImages[1]);
    painter.beginNativePainting();
    vao->bind();
    vbo->bind();  
    glViewport(0,0,200,200);
    glDrawArrays(GL_TRIANGLE_STRIP,0,8);
    painter.endNativePainting();
    painter.end();
}

I have three questions.

  1. why I have to release vao and vbo to paint widget with QPainter in paintGL(), if not, Qpainter doesn't work.

  2. widget can't paint textures in paintGL() when use QPainter and OpenGL API at the same time. It works well when just call OpenGL API.

  3. When I repaint widget frequently (click widget frequently, calling mousePressEvent() and repaint()), some repaint event are ignored by Qt.If call repaint(), QWidget will repaint immediately, so how can I force Qt to repaint QOpenGLWidget just like calling repaint() to repaint QWidget?

2
Not a Qt OpenGL guru, but I think you need to call release on vao and vbo at the end of your function. - Andrejs Cainikovs
I tried, all of the OpenGL API doesn't work if call release at the end. If call release at the beginning, only texture doesn't show up. - Chris
Regarding (3.): Why do you want immediate repaint? Usually, it's better to call QWidget::update than QWidget::repaint (see also this one). If you want to store the renderd textures and you need all frames (which would not be visible on screen), you might prefer some offscreen-rendering solution. - pasbi

2 Answers

0
votes

Finally,I place the Qpainter in mouseMove() function to draw on QImage. Then when I begin to draw on image, mouseMove() will draw on image immediately, every action will be responded, then paint the image on widget ,paint actions will no longer be ignored by qt. The problem get solved. But I still don't get the answer of question two and three.

-1
votes

Do you called initializeOpenGLFunctions()? And may be you need to reimplement QOpenGLWidget::paintEvent and call update()?