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)); painter->setBrush(QBrush(Qt::blue)); painter->drawPolygon(triangle_1); ui->label2->setPixmap(pm); //painter->end(); //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)); painter->setBrush(QBrush(Qt::blue)); painter->drawPolygon(triangle_2); ui->label1->setPixmap(pm1); painter->end();
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.
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.
why don't reuse the same Painter?