Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPainter.drawImage in paintGL not work
Forum Updated to NodeBB v4.3 + New Features

QPainter.drawImage in paintGL not work

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 154 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anima
    wrote on last edited by Anima
    #1

    I have a widget subclass QOpenGLWidget and I want to combine openGL and QPainter in the paintGL function, but it not work. Here is the code:

    void paintGL()
    {	
            glEnable(GL_MULTISAMPLE);
    	glEnable(GL_BLEND);
    	glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
    	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    	glClear(GL_COLOR_BUFFER_BIT);
    
    	m_pImageRender->paint();
    
    	glDisable(GL_BLEND);
    	glDisable(GL_MULTISAMPLE);
            
            QPainter painter(this);
            painter.fillRect(QRect(100, 100, 100, 100), QColor("#ff0000"); // without the following line,  this line work
            painter.drawImage(rect(), m_oImage, QRect(0, 0, m_oImage.width(), m_oImage.height()); // after add this line, the screen display nothing, just black
    }
    

    The openGL painting code is like below:

    m_pShaderProgram->bind();
    	m_pVertexBuffer->bind();
    	m_pIndexBuffer->bind();
    	
    	m_pShaderProgram->setUniformValue("projection", m_mtProjection);
    	m_pShaderProgram->setUniformValue("scale", m_fScale);
    	m_pShaderProgram->setUniformValue("offset", m_vecOffset);
    
    	int textureUnits[m_nImageCount], display[m_nImageCount];
    	float cmin[m_nImageCount], cmax[m_nImageCount];
    	QVector4D color[m_nImageCount];
    	for (int i = 0;i < m_nImageCount;i++)
    	{
    		textureUnits[i] = i;
    		display[i] = m_vecDisplay[i];
    		cmin[i] = m_vecClipMin[i];
    		cmax[i] = m_vecClipMax[i];
    		color[i] = QVector4D(m_vecColor[i].red() / 255.0, m_vecColor[i].green() / 255.0, m_vecColor[i].blue() / 255.0, 1.0);
    	}
    
    	m_pShaderProgram->setUniformValueArray("utexture", textureUnits, m_nImageCount);
    	m_pShaderProgram->setUniformValueArray("display", display, m_nImageCount);
    	m_pShaderProgram->setUniformValueArray("rgbcolor", color, m_nImageCount);
    	m_pShaderProgram->setUniformValueArray("cmin", cmin, m_nImageCount, 1);
    	m_pShaderProgram->setUniformValueArray("cmax", cmax, m_nImageCount, 1);
    	for (int i = 0;i < m_nImageCount;i++)
    	{
    		if (m_vecTexture[i])
    		{
    			m_vecTexture[i]->bind(i);
    		}
    	}
    
    	m_pShaderProgram->enableAttributeArray(0);
    	m_pShaderProgram->setAttributeBuffer(0, GL_FLOAT, 0, 2, 4 * sizeof(GLfloat));
    	m_pShaderProgram->enableAttributeArray(1);
    	m_pShaderProgram->setAttributeBuffer(1, GL_FLOAT, 2 * sizeof(GLfloat), 2, 4 * sizeof(GLfloat));
    
    	m_pOpenGLCore->glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    
    	for (int i = 0;i < m_nImageCount;i++)
    	{
    		if (m_vecTexture[i])
    		{
    			m_vecTexture[i]->release(i);
    		}
    	}
    	m_pShaderProgram->release();
    	m_pVertexBuffer->release();
    	m_pIndexBuffer->release();
    

    So, what may cause the problem?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved