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().