2016-07-20 3 views
-1

qt 및 QOpenGLFunctions를 사용하여 screengrabber를 만들려고합니다. 내 코드는 funcs = context-> functions(); 이 코드는 실제로 그렇게 크거나 스레드가 아니며 작은 간격으로 타이머가 정확하지는 않지만 사용하려는 코드보다 개념의 증거가됩니다. 내가 이해하기 때문에 창에서 ANGLE을 사용할 수 있으려면이 QOpenGLFunctions를 사용해야합니다. 윈도우가 opengl 1.0과 함께 제공되기 때문에 도움이 될 것입니다. 오히려 그 대신에 ANGLE을 통해 directx를 사용하고 싶습니다. 필자는 Ubuntu 16.04 LTS 및 Windows 10에서 QT 5.5.1에서 5.7까지의 모든 키트를 테스트했습니다.Qt OpenGLFunctions seg 오류

생성자 :

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

'context-> create();'는 true 또는 false를 반환합니까? – PeterT

+0

'My code seg faults' 물론, 다음으로 디버거를 열고, 크래쉬를 호출하고, 디버그 모드에서 스택 트레이스를 실행하는 것입니다. 무엇을 보여줬습니까? –

+0

context.create()는 false를 반환합니다. 그래서 그게 문제가 될 수 있습니다. seg 오류가 발생하고 명령 대기열에서 컴파일 된 코드의 위치 만 제공합니다. 그것을 일으킨 지시의 주소를 말하면됩니다. –

답변

0

컨텍스트를 현재로 설정하거나 QOpenGLContext::currentContext을 사용해야합니다. 또한 실제로 OpenGL을 사용하고 있는지 확인해야합니다.

특별히 OpenGL을 사용할 필요가없고 QMainWindow와 같은 QWidget을 얻으려면 QWidget::render을보고 싶을 수 있습니다.

+0

나는 컨텍스트를 현재 컨텍스트로 만들었고 그것을 시도하기 위해 openGL 4.5로 변경했다. 내 응용 프로그램에서 OpenGL을 사용하지 않습니다. [Screenshot Example] (http://doc.qt.io/qt-5/qtwidgets-desktop-screenshot-example.html) 메서드가 너무 느리기 때문에 실제로 화면 캡처에만 사용하고 있습니다. –

+0

glReadPixels는 gl 컨텍스트와 연결된 창 안의 픽셀을 읽습니다. 응용 프로그램이 OpenGL을 사용하지 않는 경우 해당 픽셀을 반환하지 않습니다. 그리고 전체 화면을 원한다면 실제로 OS 또는 X11에 문의해야합니다. 리눅스에 관해서 SO에 관한 많은 질문이 있습니다 : http://stackoverflow.com/questions/2607010/linux-how-to-capture-screen-and-simulate-mouse-movements. –