Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QPainter native painting in QImage



  • Hi,
    i try to paint into a QImage with native painting:

    void paint(QPaintDevice *device)
    {
        QPainter painter(device);
        painter.beginNativePainting();
    
        glBegin(GL_POINTS);
          glColor3f(1, 0, 0); glVertex3f(0, 0, 0);
          glColor3f(0, 1, 0); glVertex3f(1, 0, 0);
          glColor3f(0, 0, 1); glVertex3f(1, 1, 0);
          glColor3f(1, 1, 0); glVertex3f(0, 1, 0);
        glEnd;
    
        painter.endNativePainting();
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QImage image(100,100, QImage::Format_RGB32);
        paint(&image);
        bool ok = image.save("test.jpg"); // ok = true
    
        return 0;
    }
    

    But the image doesn't show the points. Do i forget something?


  • Lifetime Qt Champion

    @beecksche
    Hi
    Are you sure this can work ?
    I mean, Qimage must have an openGL context for this to be even possible to work.



  • @mrjj
    I'am definitily not sure ;-).

    I just saw, that the QPainter class paints into a QPaintDevice, and QImage is a QPaintDevice.
    I was not sure if the QPainter class translates the raw OpenGL commands and "paints" them into the QPaintDevice.

    So i will try it with a QWidget and see what happens :)

    Edit:
    So i tested the native painting:

    First of all, if i want to do the native painting in separate function it has no effect at all. So I inherited a class by QWidget and override the paintEvent function. But also the native painting has no effect, unless i use the QOpenGLWidget as base class.

    But here i don't understand the benefit, because you can use the functions described in the doc from QOpenGLWidget, paintGL and so on.


Log in to reply