[Using QPainter in PaintGL() crashes the app]
-
Hello, I hope you're all doing well.
So I developed an application that rotates an object using Qt6 and OpenGL.
I added the object's coordinate system but I also wanted to add a label for each axis.
I thought about using QPainter in order to add the axes labels in my scene so that the user knows for example the red axis corresponds to the X axis.
However my application crashes whenever I use QPainter.I run the app in debug mode and it seems that the debugger indicates that there is a problem in this line [ in the QPainter.h file]
In my paintGL() function I tried this:
QPainter p(this); p.begin(this); p.beginNativePainting(); p.setPen(Qt::red); p.setFont(QFont("Arial", 10)); p.drawText(7, 50, "-----X_axis"); p.endNativePainting();
But it still crashes.
Please help.
Any help would be really appreciated.
Thank you all.
-
Hi,
beginNativePainting
is for when you want to interleave OpenGL code directly and it's not what you do.On an related note, since you pass
this
as parameter to the constructor, you do not need to callbegin
. -
This is what it shows actually
It only tells that there is a problem in the drawText() function.I don't really get it, the parameters passed are correct, I searched a lot on the internet on how to use QPainter functions in the right way, I tried them all but nothing seems to work.
I'm so confused.I click on the line where the app crashes and it only shows the parameters passed to the function and their types. That's all.
I forgot to add that I have shader programs in my code.
But I used QPainter after disabling the shader program.
-
Can you check this small example that mixes OpenGL and QPainter ?
-
I did.
I developed an application 4 months ago using OpenGL functions (without shaders) and QPainter; the app didn't crash.In this app, I'm drawing an object using shader programs.
Right after disabling the shader, I used QPainter to draw some text then the app started to crash. -
Can you show the relevant code you use ?
-
okay here is my code:
void MainWidget::paintGL() { //qDebug()<<__func__; // Clear color and depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //================Orbital Frame ==========================================// QMatrix4x4 localMatrix; glMatrixMode(GL_PROJECTION); glLoadMatrixf(projection.constData()); glMatrixMode(GL_MODELVIEW); glLoadMatrixf(localMatrix.constData()); gluLookAt(2.0,2.0,0.0, 0.0,0.0,-5.0,0.0,1.0,0.0); glTranslatef(0.0,0.0,-5.0); glRotatef(180.0,0.0,1.0,0.0); glRotatef(-90.0,1.0,0.0,0.0); glScalef(0.4,0.4,0.4); DrawOrbitalFrame(); //==========================drawing the object======================================================== localMatrix.setToIdentity(); localMatrix.lookAt(QVector3D(2.0, 2.0, 0.0), QVector3D(0.0,0.0,-5.0),QVector3D(0.0,1.0,0.0)); localMatrix.translate(0.0, 0.0, -5.0); localMatrix.scale(0.4,0.4,0.4); quaternion = QQuaternion(quat_w, quat_x, quat_y, quat_z); quaternion.normalize(); // Normalizing my quaternion localMatrix.rotate(180.0,0.0,1.0,0.0); localMatrix.rotate(-90.0,1.0,0.0,0.0); localMatrix.rotate(quaternion); // rotation using spin boxes localMatrix.rotate(csvquaternion); // rotation using csv file program.setUniformValue("mvp_matrix", projection * localMatrix); update();//updates the scene // Use texture unit 0 which contains cube.png program.setUniformValue("texture", 0); // Draw cube geometry geometries->drawCubeGeometry(&program); texture->release(); program.release(); // Disabling the shader program //===============================Satellite Body Frame======================================// glMatrixMode(GL_PROJECTION); glLoadMatrixf(projection.constData()); glMatrixMode(GL_MODELVIEW); glLoadMatrixf(localMatrix.constData()); //glRotatef(-180.0,0.0,1.0,0.0); DrawSatelliteBodyFrame(); //=============================Coordinate system axes labels=========================================================// QPainter p(this); p.begin(); p.setPen(Qt::red); p.setFont(QFont("Arial", 10)); p.drawText(7, 50, "-----X_axis"); // drawing the x-axis label QPainter p1(this); p1.setPen(Qt::green); p1.setFont(QFont("Arial", 10)); p.drawText(7,100, "-----Y_axis"); // drawing the y-axis label QPainter p2(this); p2.setPen(Qt::blue); p2.setFont(QFont("Arial", 10)); p.drawText(7,150,"-----Z_axis"); // drawing the z-axis label QPainter p3(this); p3.setPen(Qt::white); p3.setFont(QFont("Arial", 10)); p3.drawText(7, 200, "-----Origin"); // drawing the origin label p.end(); }
I started drawing the fixed coordinate system then I ENABLED the shader program to draw the object I want to rotate and applied the texture on it, then I Disabled the shader and drew the object's coordinate system and finally drew the axes labels using Qpainter.
It keeps crashing in thedrawText()
function
I've been trying to modify the code lines ( related to QPainter); the debugger indicates that the crash happens in the drawText() function. I don't really get what's wrong.
-
I am currently wondering whether the shader does some changes that affects the paint engine.
Would it be possible for you to reduce the code to a minimal buildable example ?
-
Okay.
I only used shaders to draw my object which is a textured cube.
I added two coordinate systems to my scene without using shaders [ using glMatrixMode].In my paintGL() function, I tried to comment all the lines that drew the cube and the coordinate systems and l only kept the lines that drew the labels using QPainter.
The application crashed so I don't think the problem is caused by the shaders. -
So you managed to reduce to minimal example ?