PDA

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



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


void QGLWidget::paintGL()
{
QPainter ipainter(this);
ipainter.setWindow(QRect(this->translate.X,this->translate.Y,1,1));
ipainter.setRenderHint(QPainter::Antialiasing, true);
CanvasDrawer->RunOpenGL(&ipainter);
}

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.setWindow(QRect(this->translate.X,this->translate.Y,1,1));
ipainter.setRenderHint(QPainter::Antialiasing, true);
CanvasDrawer->RunOpenGL(&ipainter);
}

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

stampede
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.

superwave
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?

stampede
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 ?

superwave
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];
CalNodeOrder(nodeOrders);

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

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

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

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)
continue;
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;
}
else
{
glPointSize(1.f);
alpha = 0.f;
}
glBegin(GL_POINTS);
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]);
glEnd();
}

// 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);
glBegin(GL_LINE_LOOP);
glVertex2d(squareX, squareY);
glVertex2d(squareX + w * 0.96, squareY);
glVertex2d(squareX + w * 0.96, squareY + h);
glVertex2d(squareX, squareY + h);
glEnd();
}

glDisable(GL_BLEND);

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

stampede
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 ?

mcosta
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??

superwave
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.