Important: Please read the 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);
          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);
    int main(int argc, char *argv[])
        QCoreApplication a(argc, argv);
        QImage image(100,100, QImage::Format_RGB32);
        bool ok ="test.jpg"); // ok = true
        return 0;

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

  • Lifetime Qt Champion

    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 :)

    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