-1

I am trying to make a screengrabber using qt and QOpenGLFunctions. My code seg faults on funcs = context->functions(); This code is not really that great or threaded and the timers are not precise at such small intervals but it's more of a proof of concept thing than code I plan on using. As I understand I need to use these QOpenGLFunctions in order for it to be able to use ANGLE on windows. Which would help because windows only ships with opengl 1.0 and I'd rather not use that and use directx through ANGLE instead. I have tested on Ubuntu 16.04 LTS and Windows 10 using QT 5.5.1 through 5.7 on all the kits.

Constructor:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//QOpenGLContext *context = new QOpenGLContext();
QOpenGLContext *context = QOpenGLContext::CurrentContext();
//I only want to use glreadpixels. I don't need the rest of opengl. This may not be necessary. 
QSurfaceFormat format;
format.setRenderableType(QSurfaceFormat::OpenGL);
format.setDepthBufferSize(32);
format.setVersion(4,5);
format.setSamples(4);
format.setProfile(QSurfaceFormat::CompatibilityProfile);
context->setFormat(format);
context->create();

funcs = context->functions(); //seg faults here
funcs->initializeOpenGLFunctions();
connect(timer, SIGNAL(timeout()), this, SLOT(grabScreen()));
connect(timer2, SIGNAL(timeout()),this, SLOT(quit()));

timer->setTimerType(Qt::PreciseTimer);
timer2->setTimerType(Qt::PreciseTimer);
timer2->start(10000); //10 sec
timer->start(1000/60); //60 fps

}

grabScreen:

void MainWindow::grabScreen()
{
//this method it too slow for anything more than one Monitor at 60 fps.
//this->originalPixmap = this->primary->grabWindow(0,0,0,1920,1080);



//this method is fast. Did 120 FPS no problem. It doesn't use the QtOpenGLFunctions. Less portable?
//QImage image(1920,1080,QImage::Format_RGBA8888);
//glReadPixels(0,0,1920,1080,GL_RGBA,GL_UNSIGNED_BYTE, image.bits());//not writing to the image bits

QImage image(1920,1080,QImage::Format_RGBA8888);
funcs->glReadPixels(0,0,1920,1080,GL_RGBA,GL_UNSIGNED_BYTE, image.bits());

frameCount++;
if(frameCount % 10 == 0) //update preview label
{
    //ui->label->setPixmap(QPixmap::fromImage(image.scaled(ui->label->size())));
    ui->label->setPixmap(originalPixmap.scaled(ui->label->size()));
    //qDebug() << QString("Frame: " + frameCount);
}

}

My other problem with this code is that for some reason the non qt glreadpixels does not write to the qimage.bits().

  • 1
    does `context->create();` return true or false? – PeterT Jul 20 '16 at 05:52
  • `My code seg faults` OK, so of course, the next thing you did was to open up your debugger, invoke the crash, and run a stack trace in debug mode. What did it show you? – underscore_d Jul 20 '16 at 12:25
  • context.create() returns false. So that could be the problem. The seg fault occurs and it just gives the location of the compiled code in the instruction queue. Just says the address of the instruction that caused it. – Will Tangney Jul 21 '16 at 21:23

1 Answers1

0

You should probably make your context current or use QOpenGLContext::currentContext. Also you should check your app is actually using OpenGL.

If you don't specifically need to use OpenGL and you just want to get a shot of a QWidget like QMainWindow you may want to have a look at QWidget::render.

Luca Carlon
  • 9,546
  • 13
  • 59
  • 91
  • I made context the current context and I changed it to openGL 4.5 just to try it. I am not using openGL in my app. I am really only using it to capture the screen because the [Screenshot Example](http://doc.qt.io/qt-5/qtwidgets-desktop-screenshot-example.html) method is too slow. – Will Tangney Jul 21 '16 at 21:32
  • glReadPixels reads pixels inside the window associated to the gl context. If your application is not using OpenGL it won't return those pixels. And if you want the entire screen you need to ask the OS, or X11 actually. There are many questions on SO about that for Linux: http://stackoverflow.com/questions/2607010/linux-how-to-capture-screen-and-simulate-mouse-movements. – Luca Carlon Jul 21 '16 at 22:08