View Full Version : Why the drawing cannot be shown when using paintGL() in the QGLWidget?

28th June 2011, 09:16
I reimplemented paintGL() :

void QGLWidget::paintGL()
QPainter ipainter(this);
ipainter.setRenderHint(QPainter::Antialiasing, true);

the OpenGL codes are inside the function RunOpenGL .
The widget is black and none of the Opengl drawings is shown.

However, after I changed paintGL() to paintEvent:

void grCanvas::paintEvent(QPaintEvent* event)
QPainter ipainter(this);
ipainter.setRenderHint(QPainter::Antialiasing, true);

the drawing result will show on the widget well

What's the problem? why the paintGL doesn't work? It is said to work like paintEvent

28th June 2011, 10:04
Why are you using a QPainter to render with OpenGL ? It should be enough to reimplement paintGL() alone, and use OpenGL calls to render the scene. Can you post RunOpenGL code ?

why the paintGL doesn't work? It is said to work like paintEvent
Where it is said so ? paintEvent "will cause the virtual paintGL() function to be called.", they are not the same.

28th June 2011, 10:09
Thanks a lot.

However, since the paintGL() is called by the QGLWidget automatically, why the OpenGL drawing will not be shown on widget, but paintEvent works?

28th June 2011, 10:12
It depends on what you draw in RunOpenGL, I can only guess, but if you are passing QPainter to that method, its possible that you are not using any OpenGL calls, just regular QPainter drawing code. Can you post the code ?

28th June 2011, 16:29
Here is the code in the function RunOpenGL:

IRGraph *graph = docManager->GetIRGraph();
int nodeNum = graph->GetNodeNum();
int squareN = maxLevel + 1;
double x1 = 0.0;
double y1 = 0.0;
double x2 = 1.0;
double y2 = 1.0;
double w = (x2 - x1) / (double)squareN;
double h = y2 - y1;

int *nodeOrders = new int [nodeNum];

double *posX = new double [nodeNum];
double *posY = new double [nodeNum];

CalDiagramSquareNodePos(w * 0.96, h, posX, posY);


int level, i, member, colorIdx, len, nodeId;
// bool isCore;
double squareX, squareY;
float alpha;

// squareY = y1 + h * 0.05;
squareY = y1;
for (level=0; level<=maxLevel; level++)
// squareX = x1 + w * level + w * 0.05;
squareX = x1 + w * level + w * 0.02;

// Draw nodeNum nodes
for (i=0; i<nodeNum; i++)
nodeId = nodeOrders[i];
if (nodeId == -1)
member = graph->GetNodeMembership(nodeId);
// isCore = graph->IsClusterCore(nodeId);
len = graph->GetDistToFocus(nodeId, this->focusKey);
if ((level < maxLevel && len <= level) || (level == maxLevel && len >= level))
// glPointSize((float)(nodeSize*(this->GetIRGraph()->GetNodeCentrality(nodeId))));
glPointSize(graph->IsNodeSelected(nodeId) ? (float)(nodeSize + distortionFactor) : (float)nodeSize);
alpha = graph->IsNodeSelected(nodeId) ? 1.0f : 0.5f;
alpha = 0.f;
colorIdx = member % IRGPixelDrawer::colorNum;
glColor4f(qualitativeColorTable[colorIdx][0]/255.f, qualitativeColorTable[colorIdx][1]/255.f, qualitativeColorTable[colorIdx][2]/255.f, alpha);
glVertex2d(squareX+posX[i], squareY+posY[i]);

// Draw a boundary rectangle
colorIdx = 9;
alpha = 1.0f - 0.8f/(float)maxLevel*(float)level;
glColor4f(qualitativeColorTable[colorIdx][0]/255.f, qualitativeColorTable[colorIdx][1]/255.f, qualitativeColorTable[colorIdx][2]/255.f, alpha);
glVertex2d(squareX, squareY);
glVertex2d(squareX + w * 0.96, squareY);
glVertex2d(squareX + w * 0.96, squareY + h);
glVertex2d(squareX, squareY + h);


delete [] nodeOrders;
delete [] posX;
delete [] posY;

29th June 2011, 06:29
Ok, maybe I just can't see it but I think there is no QPainter drawing code inside that method, so why are you passing QPainter pointer to it ? Does it work if you do some "basic" OpenGL drawing inside, like drawing a line or few points ?

29th June 2011, 17:30
I Think the proble is creating a QPainter inside paintGL().

You don't use the painter in RunOpenGL. Why create it??

1st July 2011, 02:21
The RunOpenGL function is owned by 4 classes inherited from a same parent class. Some RunOpenGL functions have Qt drawing codes, some have only OpenGL codes.

The RunOpenGL is a virtual function, some classes need qt drawing codes.