在QGLWidget上使用QPainter时,需要哪些步骤来启用反混叠?

时间:2022-03-01 20:02:31

I am trying to draw basic shapes on a QGLWidget. I am trying to enable antialiasing to smooth out the lines, but it is not working.

我试着在一个QGLWidget上画基本形状。我正在尝试启用反锯齿来平滑线条,但它不起作用。

This is what I am trying at the moment:

这就是我现在正在尝试的:

QGLWidget *widget = ui->renderWidget;

QPainter painter;

widget->makeCurrent();
glEnable(GL_MULTISAMPLE);
glEnable(GL_LINE_SMOOTH);

painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::HighQualityAntialiasing);

painter.begin(widget);

However, anything drawn with this painter still has jagged edges. What else do I need to do?

然而,与这位画家画的任何东西都有锯齿状的边缘。我还需要做什么?

3 个解决方案

#1


10  

I found the solution. When debugging a different issue, I found messages in my debug output to the effect that you can't set renderhints before the call to begin().

我找到了解决方案。在调试另一个问题时,我在调试输出中发现了消息,在调用开始之前不能设置render提示。

The following works:

以下工作:

QGLWidget *widget = ui->renderWidget;

QPainter painter;

widget->makeCurrent();
glEnable(GL_MULTISAMPLE);
glEnable(GL_LINE_SMOOTH);

painter.begin(widget);

painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::HighQualityAntialiasing);

#2


7  

You can try to enable the antialiasing on the complete Widget :

您可以尝试在完整的小部件上启用反别名:

QGLWidget::setFormat(QGLFormat(QGL::SampleBuffers));

#3


0  

This question is quite old but I still found it on Google. You shouldn't use QGLWidget any more. Use the newer QOpenGLWidget. This renders the scene off-screen rather than creating a native OpenGL window which causes all sorts of issues with resizing layouts. This code works for me. Put it in your QGraphicsView constructor:

这个问题很老了,但我还是在谷歌找到了。您不应该再使用QGLWidget了。使用较新的QOpenGLWidget。这使得场景脱离屏幕,而不是创建一个原生的OpenGL窗口,它会导致各种各样的大小调整布局的问题。这段代码对我有效。将其放入QGraphicsView构造函数中:

QOpenGLWidget* gl = new QOpenGLWidget;
QSurfaceFormat fmt;
fmt.setSamples(8);
gl->setFormat(fmt);
setViewport(gl);
setRenderHint(QPainter::Antialiasing);

#1


10  

I found the solution. When debugging a different issue, I found messages in my debug output to the effect that you can't set renderhints before the call to begin().

我找到了解决方案。在调试另一个问题时,我在调试输出中发现了消息,在调用开始之前不能设置render提示。

The following works:

以下工作:

QGLWidget *widget = ui->renderWidget;

QPainter painter;

widget->makeCurrent();
glEnable(GL_MULTISAMPLE);
glEnable(GL_LINE_SMOOTH);

painter.begin(widget);

painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::HighQualityAntialiasing);

#2


7  

You can try to enable the antialiasing on the complete Widget :

您可以尝试在完整的小部件上启用反别名:

QGLWidget::setFormat(QGLFormat(QGL::SampleBuffers));

#3


0  

This question is quite old but I still found it on Google. You shouldn't use QGLWidget any more. Use the newer QOpenGLWidget. This renders the scene off-screen rather than creating a native OpenGL window which causes all sorts of issues with resizing layouts. This code works for me. Put it in your QGraphicsView constructor:

这个问题很老了,但我还是在谷歌找到了。您不应该再使用QGLWidget了。使用较新的QOpenGLWidget。这使得场景脱离屏幕,而不是创建一个原生的OpenGL窗口,它会导致各种各样的大小调整布局的问题。这段代码对我有效。将其放入QGraphicsView构造函数中:

QOpenGLWidget* gl = new QOpenGLWidget;
QSurfaceFormat fmt;
fmt.setSamples(8);
gl->setFormat(fmt);
setViewport(gl);
setRenderHint(QPainter::Antialiasing);