Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why this doesn't show me any window?
Forum Updated to NodeBB v4.3 + New Features

Why this doesn't show me any window?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JesusM
    wrote on last edited by JesusM
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      3
      • J Offline
        J Offline
        JesusM
        wrote on last edited by JesusM
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • SGaistS SGaist

          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 ?

          J Offline
          J Offline
          JesusM
          wrote on last edited by
          #4

          @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();
          }
          
          

          0_1550132104745_errorQt.PNG

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Can you show the stack trace of your crash ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            J 1 Reply Last reply
            0
            • SGaistS SGaist

              Can you show the stack trace of your crash ?

              J Offline
              J Offline
              JesusM
              wrote on last edited by
              #6

              @SGaist I think this is what you mean.
              0_1550188020337_stacktrace.PNG

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                J 1 Reply Last reply
                0
                • SGaistS SGaist

                  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.

                  J Offline
                  J Offline
                  JesusM
                  wrote on last edited by JesusM
                  #8

                  @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) << ")";
                   }
                  
                  
                  
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Why are you calling resizeGL manually from resizeEvent ? resizeGL will be called automatically for you.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    J 1 Reply Last reply
                    2
                    • SGaistS SGaist

                      Why are you calling resizeGL manually from resizeEvent ? resizeGL will be called automatically for you.

                      J Offline
                      J Offline
                      JesusM
                      wrote on last edited by
                      #10

                      @SGaist I thought resizeGL wasn't a callback and I needed to define a call to it. But now I removed it and it works! It shows me the window.
                      Thank you so much!

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        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 :)

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved