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. Save parts of screen to image file
QtWS25 Last Chance

Save parts of screen to image file

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.5k 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.
  • H Offline
    H Offline
    hlhl
    wrote on last edited by
    #1

    Hello,

    I'm having an odd problem when trying to save parts of the screen in my window (i.e. a screenshot) to an image file. It works on the first call, but the subsequent calls simply write the first screenshot over again.

    I'm coding a QGuiApplication, with QWindow. Somewhere in the QWindow-inherited class (outside any Qt/OpenGl rendering), I call:

    m_context->screen()->grabWindow(winId(),x,y,w,h).save(QString("test.bmp"));

    m_context is a QOpenGLContext, with the OpenGLSurface surface type.

    Any ideas to why this only works on the first call (and then seems to "freeze" somehow)?

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

      Hi,

      Can you provide a more complete example that shows the problem ?

      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
      • H Offline
        H Offline
        hlhl
        wrote on last edited by
        #3

        Here's a minimalist code snippet, MainWindow inherits QWindow:

        @MainWindow::MainWindow(QWindow *parent) :
        QWindow(parent),
        m_context(0),
        m_device(0),
        {
        setWidth(WINDOWRESX);
        setHeight(WINDOWRESY);
        setSurfaceType(QWindow::OpenGLSurface);
        }

        bool MainWindow::event(QEvent *event)
        {
        switch (event->type()) {
        case QEvent::UpdateRequest:
        render();
        return true;
        default:
        return QWindow::event(event);
        }
        }

        void MainWindow::exposeEvent(QExposeEvent *event)
        {
        Q_UNUSED(event);
        if (isExposed())
        startRender();
        }

        void MainWindow::render()
        {
        if (!m_device)
        m_device = new QOpenGLPaintDevice;

        m_device->setSize(size());
        QPainter painter(m_device);
        
        glClearColor(.75f,.75f,.75f,1.0f); // background colour
        glViewport(0,0,width(),height());
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
        
        painter.beginNativePainting(); // start OpenGL painting
        

        openGL drawing

        painter.endNativePainting();
        render(&painter);
        

        }

        void MainWindow::render(QPainter *painter)
        {
        draw with painter
        }

        void MainWindow::startRender()
        {
        if (!isExposed())
        return;

        bool needsInitialize = false;
        
        if (!m_context) {
            m_context = new QOpenGLContext(this);
            m_context->setFormat(requestedFormat());
            m_context->create();
            needsInitialize = true;
            initialize();
        }
        
        m_context->makeCurrent(this);
        if (needsInitialize) {
            initializeOpenGLFunctions();
        }
        
        render();
        
        m_context->swapBuffers(this);
        

        }

        void MainWindow::keyPressEvent(QKeyEvent *ev)
        {
        if(ev->key()==Qt::Key_S) { // save file
        int x = 50, y = 50, w = 500, h = 500;
        m_context->screen()->grabWindow(winId(),x,y,w,h).save(QString(“test.bmp”));
        }
        }@

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

          Shouldn't you make the context current before doing a grabWindow ?

          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
          • H Offline
            H Offline
            hlhl
            wrote on last edited by
            #5

            Nope, that didn't solve the problem; that is, the image that is being saved is still the first saved image, even if I delete the "old" image before I press S again. The edited keyPressEvent:

            @void MainWindow::keyPressEvent(QKeyEvent *ev)
            {
            if(ev->key()==Qt::Key_S) { // save file
            int x = 50, y = 50, w = 500, h = 500;
            m_context->makeCurrent(this);
            m_context->screen()->grabWindow(winId(),x,y,w,h).save(QString(“test.bmp”));
            }
            }@

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

              Did you try grabWidget ?

              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
              • IamSumitI Offline
                IamSumitI Offline
                IamSumit
                wrote on last edited by
                #7

                Try This,,
                QString format = "bmp";
                QString fileName="D:/mTest";
                QPixmap::grabWidget(this).save(fileName, format.toAscii());

                Be Cute

                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