Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. [Using QPainter in PaintGL() crashes the app]

[Using QPainter in PaintGL() crashes the app]

Scheduled Pinned Locked Moved Unsolved Qt 6
14 Posts 3 Posters 1.3k 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
    appdev
    wrote on last edited by
    #1

    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]

    QpainterQt.PNG

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 call begin.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        appdev
        wrote on last edited by
        #3

        @SGaist okay I tried this:

            QPainter p(this);
            p.setPen(Qt::red);
            p.setFont(QFont("Arial", 10));
            p.drawText(7, 50, "-----X_axis");
        
        

        The application is still crashing.

        JonBJ 1 Reply Last reply
        0
        • A appdev

          @SGaist okay I tried this:

              QPainter p(this);
              p.setPen(Qt::red);
              p.setFont(QFont("Arial", 10));
              p.drawText(7, 50, "-----X_axis");
          
          

          The application is still crashing.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @appdev
          At this point run it un debugger, allow it to crash, show the stack trace which you will find in a debugger pane. See if it helps see what is called from drawText() which crashes.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            appdev
            wrote on last edited by appdev
            #5

            This is what it shows actually
            Stacktrace1.png
            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Can you check this small example that mixes OpenGL and QPainter ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • A Offline
                A Offline
                appdev
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Can you show the relevant code you use ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    appdev
                    wrote on last edited by appdev
                    #9

                    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 the

                    drawText()
                    

                    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.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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 ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        appdev
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          So you managed to reduce to minimal example ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            appdev
                            wrote on last edited by
                            #13

                            I did but the app is still crashing

                            JonBJ 1 Reply Last reply
                            0
                            • A appdev

                              I did but the app is still crashing

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #14

                              @appdev
                              If you have now written a minimal example which shows the problematic behaviour, as @SGaist asked you to, then obviously you should show that code....

                              1 Reply Last reply
                              1

                              • Login

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