Why this doesn't show me any window?
-
I am trying to start with OpenGL c++ in Qt and I have this simply code that I found but it doesn't show me any window
main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QOpenGLWidget> #include <QApplication> #include "glwindow.h" #include <iostream> int main(int argc, char *argv[]) { QApplication app(argc, argv); //QOpenGLWidget *widget = new QOpenGLWidget(); //widget->setFormat(format); // must be called before the widget or its parent window gets shown glwindow *window= new glwindow(new QOpenGLWidget()); QSurfaceFormat format; format.setDepthBufferSize(24); format.setStencilBufferSize(8); format.setVersion(4, 1); format.setProfile(QSurfaceFormat::CoreProfile); QSurfaceFormat::setDefaultFormat(format); glwindow *ventana = new glwindow(window); ventana->setFormat(format); ventana->show(); std::cout<<"Ventana lanzada"<<std::endl; return app.exec(); }
glwindow.h
#ifndef GLWINDOW_H #define GLWINDOW_H #include <QOpenGLWidget> #include <QOpenGLFunctions> #include <QMatrix4x4> #include <QResizeEvent> #include <iostream> class glwindow : public QOpenGLWidget, protected QOpenGLFunctions { Q_OBJECT public: glwindow(QWidget *parent = nullptr); protected: void initializeGL() override; void resizeGL(int w, int h) override; void paintGL() override; void resizeEvent(QResizeEvent *e) override; ~glwindow() override; private: void printContextInformation(); }; #endif // GLWINDOW_H
glwindow.cpp
#include "glwindow.h" glwindow::glwindow(QWidget *parent): QOpenGLWidget (parent),QOpenGLFunctions () { } glwindow::~glwindow() { makeCurrent(); } void glwindow::initializeGL() { // Set up the rendering context, load shaders and other resources, etc.: std::cout<<"hola hola"; initializeOpenGLFunctions(); printContextInformation(); glClearColor(1.0f, 1.0f, 1.0f, 1.0f); } void glwindow::paintGL() { // Draw the scene: glClear(GL_COLOR_BUFFER_BIT); } void glwindow::resizeGL(int w, int h) { glViewport(0, 0, w, h); std::cout<<"Se ha llamado a la funcion"<<std::endl; } void glwindow::resizeEvent(QResizeEvent *e) { std::cout<<"Se ha llamado al callback"<<std::endl; resizeGL(e->size().width(),e->size().height()); } void glwindow::printContextInformation() { QString glType; QString glVersion; QString glProfile; // Get Version Information glType = (context()->isOpenGLES()) ? "OpenGL ES" : "OpenGL"; glVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION)); // Get Profile Information #define CASE(c) case QSurfaceFormat::c: glProfile = #c; break switch (format().profile()) { CASE(NoProfile); CASE(CoreProfile); CASE(CompatibilityProfile); } #undef CASE // qPrintable() will print our QString w/o quotes around it. qDebug() << qPrintable(glType) << qPrintable(glVersion) << "(" << qPrintable(glProfile) << ")"; }
I also tried to call initializeGL in main.cpp after create the glwindow object, but it throws me an error.
-
Hi and welcome to devnet,
@JesusM said in Why this doesn't show me any window?:
glwindow *window= new glwindow(new QOpenGLWidget());
That's because
window
is parented to that QOpenGLWidget you create there but that one is never shown. Why are you doing it like that ? -
@SGaist Hello, and thanks for helping.
You are right, I don't know why I did that. I looked so many examples and it seems I mixed them badly.
I change that and now It throws me the openglfunctions error I said in my first message.main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QOpenGLWidget> #include <QApplication> #include "glwindow.h" #include <iostream> int main(int argc, char *argv[]) { QApplication app(argc, argv); //QOpenGLWidget *widget = new QOpenGLWidget(); //widget->setFormat(format); // must be called before the widget or its parent window gets shown 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(); std::cout<<"Ventana lanzada"<<std::endl; return app.exec(); }
-
Can you show the stack trace of your crash ?
-
One thing I would do is to remove
QOpenGLFunctions
from the initialiser list of your constructor, I haven't seen it used like that anywhere.Take a look at this blog post it shows how to use QOpenGLWidget with QOpenGLFunctions.
-
@SGaist I did that because of that error, I thought maybe I should put it in the initialiser. I just removed it and I have the same error.
In all the pages I visited I had examples with the initializeOpenGLFunctions() in the initializeGL, even in the post you just sent me but it seems that for me it's not beeing called or it's happening something that I can't see and I don't know what to do.This is the glwindow.cpp with the call remove, and, as I said, I have the same error:
#include "glwindow.h" glwindow::glwindow(QWidget *parent): QOpenGLWidget (parent) { } glwindow::~glwindow() { makeCurrent(); } void glwindow::initializeGL() { // Set up the rendering context, load shaders and other resources, etc.: std::cout<<"hola hola"; initializeOpenGLFunctions(); printContextInformation(); glClearColor(1.0f, 1.0f, 1.0f, 1.0f); } void glwindow::paintGL() { // Draw the scene: glClear(GL_COLOR_BUFFER_BIT); } void glwindow::resizeGL(int w, int h) { glViewport(0, 0, w, h); std::cout<<"Se ha llamado a la funcion"<<std::endl; } void glwindow::resizeEvent(QResizeEvent *e) { std::cout<<"Se ha llamado al callback"<<std::endl; resizeGL(e->size().width(),e->size().height()); } void glwindow::printContextInformation() { QString glType; QString glVersion; QString glProfile; // Get Version Information glType = (context()->isOpenGLES()) ? "OpenGL ES" : "OpenGL"; glVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION)); // Get Profile Information #define CASE(c) case QSurfaceFormat::c: glProfile = #c; break switch (format().profile()) { CASE(NoProfile); CASE(CoreProfile); CASE(CompatibilityProfile); } #undef CASE // qPrintable() will print our QString w/o quotes around it. qDebug() << qPrintable(glType) << qPrintable(glVersion) << "(" << qPrintable(glProfile) << ")"; }
-
Why are you calling
resizeGL
manually fromresizeEvent
?resizeGL
will be called automatically for you. -
Great !
If something needs to be called manually, it will be stated in the documentation.
Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)