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. How to create a QOffscreenSurface correctly?
Forum Updated to NodeBB v4.3 + New Features

How to create a QOffscreenSurface correctly?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 3.3k Views
  • 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.
  • M Offline
    M Offline
    MartinChan 0
    wrote on last edited by
    #1

    When I try to do some offscreen rendering jobs , I create a QOffscreenSurface in my program but it encounters error.The source code about this part says:

    qoffscreensurface.cpp 129(Qt5.3):

        //if your applications aborts here, then chances are your creating a QOffscreenSurface before
        //the screen list is populated.
        Q_ASSERT(d->screen);
    

    So, what should I do when I want to use a QOffscreenSurface correctly ?

    jsulmJ 1 Reply Last reply
    0
    • M MartinChan 0

      When I try to do some offscreen rendering jobs , I create a QOffscreenSurface in my program but it encounters error.The source code about this part says:

      qoffscreensurface.cpp 129(Qt5.3):

          //if your applications aborts here, then chances are your creating a QOffscreenSurface before
          //the screen list is populated.
          Q_ASSERT(d->screen);
      

      So, what should I do when I want to use a QOffscreenSurface correctly ?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @MartinChan-0 said in How to create a QOffscreenSurface correctly?:

      So, what should I do when I want to use a QOffscreenSurface correctly ?

      Can you show what you're doing now?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      1
      • jsulmJ jsulm

        @MartinChan-0 said in How to create a QOffscreenSurface correctly?:

        So, what should I do when I want to use a QOffscreenSurface correctly ?

        Can you show what you're doing now?

        M Offline
        M Offline
        MartinChan 0
        wrote on last edited by
        #3

        @jsulm

        Here is my code,I want to use a QGLFramebufferObject to bind the offscreen surface to do some opengl painting ,then use QGLFramebufferObject to output it .

        qtestoffscreen.h

        #ifndef QTESTOFFSCREEN_H
        #define QTESTOFFSCREEN_H
        
        #include <QOffscreenSurface>
        #include <QtGui/QOpenGLFunctions_3_3_Core>
        #include <QImage>
        #include <QGLFramebufferObject>
        #include <QOpenGLPaintDevice>
        
        class QTestOffScreen : public QOffscreenSurface,
                               protected QOpenGLFunctions_3_3_Core
        {
        public:
            explicit QTestOffScreen(QWindow *parent = 0);
            ~QTestOffScreen();
        
            virtual void render();
            virtual void initialize();
            void renderNow();
            void setAnimating(bool animating);
        
        private:
            QGLFramebufferObject *fbo;
            bool m_animating;
        
            QOpenGLContext *m_context;
            QOpenGLPaintDevice *m_device;
        
        };
        
        #endif // QTESTOFFSCREEN_H
        

        qtestoffscreen.cpp

        #include "qtestoffscreen.h"
        #include <QTime>
        
        QTestOffScreen::QTestOffScreen(QWindow *parent):
            fbo(Q_NULLPTR),
            m_context(Q_NULLPTR),
            m_device(Q_NULLPTR)
        {
            Q_UNUSED(parent);
        }
        
        QTestOffScreen::~QTestOffScreen()
        {
            delete m_context;
            delete m_device;
        }
        
        void QTestOffScreen::render()
        {
            glClear(GL_COLOR_BUFFER_BIT
                  | GL_DEPTH_BUFFER_BIT
                  | GL_STENCIL_BUFFER_BIT);
        
            glViewport(0,0,100,100);
            glOrtho(0,100,0,100,0,1);
        
            glColor3f(1.0,0.0,1.0);
        
            glBegin(GL_LINE_STRIP);
                glVertex2f(10.0f,10.0f);
                glVertex2f(90.0f,10.0f);
                glVertex2f(90.0f,90.0f);
                glVertex2f(90.0f,10.0f);
            glEnd();
        
        }
        
        void QTestOffScreen::initialize()
        {
        
            if (!fbo)
            {
                fbo = new QGLFramebufferObject(1920,1080,0);
            }
        
            fbo->bind();
        }
        
        void QTestOffScreen::renderNow()
        {
            bool needsInitialize = false;
            if (!m_context)
            {
                m_context = new QOpenGLContext(this);
                m_context->setFormat(requestedFormat());
                m_context->create();
            }
        
            m_context->makeCurrent(this);
        
            if (needsInitialize)
            {
                initializeOpenGLFunctions();
                initialize();
            }
        
            render();
        
            fbo->toImage().save(QString("buffer") + QString(".png"));
        }
        
        void QTestOffScreen::setAnimating(bool animating)
        {
            m_animating = animating;
        }
        
        

        main.cpp

        #include <QCoreApplication>
        #include "qtestoffscreen.h"
        
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
        
            QTestOffScreen screen;   ///It is wrong here.
            screen.renderNow();
            
            return a.exec();
        }
        
        1 Reply Last reply
        0
        • M Offline
          M Offline
          MartinChan 0
          wrote on last edited by MartinChan 0
          #4

          When I use the most simplified situation -- creating a QOffscreenSurface in an original main() function,it still fails.

          jsulmJ 1 Reply Last reply
          0
          • M MartinChan 0

            When I use the most simplified situation -- creating a QOffscreenSurface in an original main() function,it still fails.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @MartinChan-0 I'm not an expert on this topic, but my guess is that you need to initialize OpenGL before you create offscreenscreen surface.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            M 1 Reply Last reply
            0
            • jsulmJ jsulm

              @MartinChan-0 I'm not an expert on this topic, but my guess is that you need to initialize OpenGL before you create offscreenscreen surface.

              M Offline
              M Offline
              MartinChan 0
              wrote on last edited by
              #6

              @jsulm Thank u anyway and if I have any solutions I will update this topic.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MartinChan 0
                wrote on last edited by
                #7

                I have solved it (althought it still has some problems) like the link in this topic

                1 Reply Last reply
                1

                • Login

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