Qt OpenGLFunctions seg fault

时间:2016-07-20 01:34:00

标签: c++ qt glreadpixels

我正在尝试使用qt和QOpenGLFunctions制作一个screengrabber。我的代码在funcs = context-> functions();这段代码实际上并不是那么好或线程,定时器在这么短的时间间隔内并不精确,但它比我计划使用的代码更像是概念验证。据我所知,我需要使用这些QOpenGLFunctions才能在Windows上使用ANGLE。哪个会有所帮助,因为windows只附带了opengl 1.0而我不想使用它而是通过ANGLE使用directx。我已经在Ubuntu 16.04 LTS和Windows 10上使用QT 5.5.1到5.7在所有套件上进行了测试。

构造

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);
}

}

我对此代码的另一个问题是,由于某种原因,非qt glreadpixels不会写入qimage.bits()。

1 个答案:

答案 0 :(得分:0)

您可能应该使您的上下文保持最新状态或使用QOpenGLContext::currentContext。此外,您应该检查您的应用实际上是否正在使用OpenGL。

如果你不是特别需要使用OpenGL,而你只想拍摄像QMainWindow这样的QWidget,你可能需要查看QWidget::render