Solved Can't link QOpenGLWidget into a Form
-
In that case, let's take another way. Put a layout in your UI and add your OpenGL widget to it. This will likely be simpler in the short run.
-
@SGaist How an I add my OpenGLWIdget to the UI instead of adding an empty one?. I am adding the widgets from here:
It has to be something with the context, I was not using any function related with the context when I hadn't the UI but It seems that now I need to use it and I don't know how.
-
@JesusM said in Can't link QOpenGLWidget into a Form:
How an I add my OpenGLWIdget to the UI instead of adding an empty one?
@SGaist suggested to do it in your code manually (not using Designer).
-
@jsulm ok I will try it now.
-
@SGaist ok, It works but it creates the widget at the bottom:
and I need to have it on the left side, as I had it before creating it manually:
This is my hierarchy (with the empty QOpenGLWidget):
I added the widget to the centralWidget which has a grid layout. With the Qt Designer I just needed to drag my widget to the left margin to move it to the left but now I can't do it as I created the widget by code. -
@JesusM said in Can't link QOpenGLWidget into a Form:
I added the widget to the centralWidget
You should add it to the layout at desired position.
-
@jsulm this is the layout where I want to add it. I had the QOpenGLWidget in that layout and it was ok as you can see in the second image.
-
@JesusM I'm a bit lost now. You should remove that QOpenGLWidget you added in the Designer and instead add one via code and insert it in same layout.
-
@jsulm I think I didn't explain myself well, sorry. The hierarchy that I posted is before I added the QOpenGLWidget by code, just to show you where I had it added. When I added it by code I removed that component from the Qt Designer so, now, my hierarchy in the Qt Designer hasn't got that component.
I added this code to the constructor of my UI to add the QOpenGLWidget:
QSurfaceFormat format; format.setDepthBufferSize(24); format.setStencilBufferSize(8); format.setVersion(4, 1); format.setProfile(QSurfaceFormat::CoreProfile); QSurfaceFormat::setDefaultFormat(format); glwindow *ventana = new glwindow(); ventana->setFormat(format); ventana->show(); ventana->setMouseTracking(true); ventana->installEventFilter(ventana); std::cout<<"Ventana lanzada"<<std::endl; ui->gridLayout->addWidget(ventana);
This code add the QOpenGLWidget but at the bottom of the grid layout.
-
@JesusM said in Can't link QOpenGLWidget into a Form:
ui->gridLayout->addWidget(ventana);
You don't specify where exactly you want this widget to be in the layout. Take a look at https://doc.qt.io/qt-5/qgridlayout.html#addWidget
-
@jsulm all the values I have tried make my program crash. I think I don't understand well how to use the addWidget function.
I tried all of this:ui->gridLayout->addWidget(ventana,0,0); // This doesn't crash but the widget is still at the bottom; ui->gridLayout->addWidget(ventana,1,0); //Same here //From here all make my program crash. ui->gridLayout->addWidget(ventana,0,1); ui->gridLayout->addWidget(ventana,1,1); ui->gridLayout->addWidget(ventana,0,0, Qt::AlignLeft); ui->gridLayout->addWidget(ventana,0,1, Qt::AlignLeft); ui->gridLayout->addWidget(ventana,1,0, Qt::AlignLeft); ui->gridLayout->addWidget(ventana,1,1, Qt::AlignLeft);
-
I found the solution. I had to set the QSizePolicy of my widget and then place the widget at the top-left side:
ventana->setSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::Expanding); ui->gridLayout->addWidget(ventana,0,0);
Thanks all for helping :)