Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change QGLFormat for an existing QGLWidget at runtime?

In my application, the user may change the properties of the OpenGL format (i.e. double buffering, multisampling, depth of various buffers, ...).

For now, there is only one QGLWidget in my application, and if the user changes anything, I destroy and recreate the widget.

Now, I would like to have more than one widget. So if the format change, I need to destroy/recreate all the widgets. As the widget can assume various configurations, it destroying/recreating them is difficult. So is there a way to change the format of a QGLWidget at runtime?

Alternatively, is there a way to replace a widget by another one? (i.e. destroy a widget and place a new one exactly where it use to stand)

like image 656
PierreBdR Avatar asked Mar 02 '11 22:03

PierreBdR


1 Answers

This may work:

QGLFormat newFormat;
newFormat.setDoubleBuffer(true);
// ...
theGLWidget->context().setFormat(newFormat);

Edit: You can also call QGLWidget::setFormat directly, but it's obsolete and may not always work. I think it's safer to recreate the widget. Here's how: Put your GL widget in a sublayout (any kind - e.g. QVBoxLayout) that contains nothing but your GL widget. When you want to replace it with a new GL widget, delete the old one, and insert your new widget in that sublayout.

like image 58
Stefan Monov Avatar answered Oct 25 '22 02:10

Stefan Monov