Important: Please read the Qt Code of Conduct -

Possible to use two QPainters at the same time?

  • I have three labels: label1 is a triangle facing up, label2 is a triangle facing down, and label3 is a a rectangle. In the .ui, they will look like this:


    The rectangle will be in between the two triangles. I created the triangles using QPainter and the rectangle was created by a regular QLabel and just colored in the background since it already has a rectangular shape. Since I want two different shapes for two different labels, is it possible to have two QPainters one for each label? I have tried the following code but only label2 shows.

     //triangle facing down
    pm = QPixmap(200,100);
    pm.fill(QColor(255, 0, 0, 0));
    painter = new QPainter(&pm);
    triangle_1 << QPoint(50,100) << QPoint(80, 70)
                << QPoint(20, 70);
    painter->setPen(QPen(Qt::black, 2));
    //triangle facing up
    pm1 = QPixmap(200,100);
    pm1.fill(QColor(255, 0, 0, 0));
    painter = new QPainter(&pm1);
    triangle_2 << QPoint(50,0) << QPoint(80, 30)
                << QPoint(20, 30);
    painter->setPen(QPen(Qt::black, 2));

  • Lifetime Qt Champion

    yes that's possible and running your code does give both polygons.
    Make sure all labels are sized so they are big enough for the image.


  • Moderators

    You have a memory leak in your code. You never free the painters. Consider allocating them on the stack.
    Also you don't need to call end() explicitly. It is called for you in the painter's destructor.

  • HI,

    why don't reuse the same Painter?

Log in to reply