Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Save parts of screen to image file

    General and Desktop
    3
    7
    2147
    Loading More Posts
    • 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
      hlhl last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • H
          hlhl last edited by

          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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 0
            • H
              hlhl last edited by

              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 Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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 Reply Quote 0
                • IamSumit
                  IamSumit last edited by

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

                  Be Cute

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post