I had this issue when I needed to draw axis and corresponding ticks. In Qt4, I can use the following code:
for (int i = _Ymin; i <= _Ymax; i = i + _yGrid)
{
double yPos = (_Ymax-i)*_yscaleF*_yscalingF;
double xPos = -_xmin*_xscaleF-_shiftX;
if(yPos < (_Ymax-_Ymin)*_yscaleF-_shiftY+1)
{
glBegin(GL_LINES);
glVertex2f(xPos-2, yPos);
glVertex2f(xPos+2, yPos);
glEnd();
renderText (xPos - offset,yPos+5 , 0, QString::number(i/10.0));
}
}
glScalef(_xscalingF,_yscalingF,0);
However, when I try to migrate my code to Qt6, there's no renderText anymore. After searching on the web and investigating the source code of renderText, finally I found the solution, one needs to save/load gl status:
glBegin(GL_LINES);
glVertex2f(xPos-4, yPos);
glVertex2f(xPos+4, yPos);
glEnd();
qt_save_gl_state();
renderText(xPos - offset,yPos+5 , QString::number(i/10.0));
qt_restore_gl_state();
Here are the def of qt_save_gl_state, renderText, qt_restore_gl_state:
static void qt_save_gl_state()
{
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
glPushAttrib(GL_ALL_ATTRIB_BITS);
glMatrixMode(GL_TEXTURE);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glShadeModel(GL_FLAT);
glDisable(GL_CULL_FACE);
glDisable(GL_LIGHTING);
glDisable(GL_STENCIL_TEST);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}
static void qt_restore_gl_state()
{
glMatrixMode(GL_TEXTURE);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPopAttrib();
glPopClientAttrib();
}
void GLWidgetnew::renderText(double x, double y, const QString text)
{
GLdouble textPosX = x, textPosY = y;
// Retrieve last OpenGL color to use as a font color
GLdouble glColor[4];
glGetDoublev(GL_CURRENT_COLOR, glColor);
QColor fontColor = QColor(glColor[0]*255, glColor[1]*255,
glColor[2]*255, glColor[3]*255);
// Render text
QPainter painter(this);
painter.translate(float(_shiftX),float(_shiftY)); //This is for my own mouse event (scaling)
painter.setPen(fontColor);
QFont f;
f.setPixelSize(10);
painter.setFont(f);
painter.drawText(textPosX, textPosY, text);
painter.end();
}
Note that one might need to adjust the renderText function for some special purposes (e.g., scaling, translation etc)
src code of the project can be found here QtSignalProcessing