简体   繁体   English

如何立即强制 Qt 在paintGL() 中重新绘制 QOpenglWidget?

[英]How to force Qt repaint QOpenglWidget in paintGL() immediately?

I want to draw in QOpenGLWidget, processing data and call repaint() in mousePressEvent() function:我想在 QOpenGLWidget 中绘图,处理数据并在 mousePressEvent() function 中调用 repaint():

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():现在在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.为什么我必须在paintGL() 中释放vao 和vbo 来使用QPainter 绘制小部件,如果没有,Qpainter 不起作用。

  2. widget can't paint textures in paintGL() when use QPainter and OpenGL API at the same time.当同时使用 QPainter 和 OpenGL API 时,小部件无法在 paintGL() 中绘制纹理。 It works well when just call OpenGL API.只需调用 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?当我频繁重绘小部件(频繁点击小部件,调用mousePressEvent()和repaint())时,Qt会忽略一些重绘事件。如果调用repaint(),QWidget会立即重绘,那么我该如何强制Qt像重绘QOpenGLWidget一样调用 repaint() 来重绘 QWidget?

Finally,I place the Qpainter in mouseMove() function to draw on QImage.最后,我将 Qpainter 放在 mouseMove() function 中以在 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.然后当我开始在图像上绘制时,mouseMove() 将立即在图像上绘制,每个动作都会得到响应,然后在小部件上绘制图像,qt 将不再忽略绘制动作。 The problem get solved.问题得到解决。 But I still don't get the answer of question two and three.但我仍然没有得到问题二和三的答案。

Do you called initializeOpenGLFunctions()?你调用了initializeOpenGLFunctions()吗? And may be you need to reimplement QOpenGLWidget::paintEvent and call update()?并且您可能需要重新实现 QOpenGLWidget::paintEvent 并调用 update()?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM