multiple QOpenGLWidgets in the same window only show one and the other can't paint after set QSurfaceFormat::setDefaultFormat



  • I developed a cross-platform app which can work both mac and windows using qt framework, there are two QOpenGLWidgets in my project, and I need to change the QSurfaceFormat::defaultFormat using QSurfaceFormat::setDefaultFormat() method, here is the code for change QSurfaceFormat::defaultFormat in the main.cpp

    int main(int argc, char *argv[])
    {
       QSurfaceFormat defaultFormat = QSurfaceFormat::defaultFormat();
    //_defaultFormat = defaultFormat;
    QSurfaceFormat format;
    	format.setSamples(8);
    	format.setVersion(3, 3);
    #if __APPLE__
    	format.setProfile(QSurfaceFormat::CoreProfile);
    #elif _WIN32
    	format.setProfile(QSurfaceFormat::CompatibilityProfile);
    #endif
    	//setFormat(format);
    	QSurfaceFormat::setDefaultFormat(format);
    	QApplication a(argc, argv);
            return a.exec();
    }
    

    now, when I run the app, only one QOpenGLWidget can paint normal, the others can't paint anything, even I use a simple code to paint a simple rectangle, it can't paint too, here is the test code in the test .cpp file in my project to paint a simple rectangle :
    myopenglview.cpp

    
    #include "myopenglview.h"
    MyOpenGLView::MyOpenGLView(QWidget *parent) : QOpenGLWidget(parent)
    {
    	
    }
    void MyOpenGLView::initializeGL(){
         	glClearColor(0.0,0.0,0.0,0.0);
    	/*
    	 * initialize viewing values
    	 */
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
    
    
    }
    
    void MyOpenGLView::paintGL(){
       	glClear(GL_COLOR_BUFFER_BIT);
    	/*
    	 * draw white polyon (rectangle) with corners at
    	 * (0.25,0.25,0.0) and (0.75,0.75,0.0)
    
    	 */
    	glColor3f(1.0,1.0,1.0);
    	glBegin(GL_POLYGON);
    	glVertex3f(0.25,0.25,0.0);
    	glVertex3f(0.75,0.25,0.0);
    	glVertex3f(0.75,0.75,0.0);
    	glVertex3f(0.25,0.75,0.0);
    	glEnd();
    
    	/*
    	 * don't wait!
    	 * start processing buffered OpenGL routines
    	 */
    	glFlush();
    
    }
    void MyOpenGLView::resizeGL(int w,int h){
    
    }
    
    

    when I run the app, I can only see the black background of MyOpenGLView, It seems the MyOpenGLView can only paint the black background correct, the content can't paint, I don't why, can you give me some tips, thanks a lot!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.