Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Possible to use two QPainters at the same time?

    General and Desktop
    4
    4
    527
    Loading More Posts
    • 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.
    • M
      marlenet15 last edited by

      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:

      label2
      label3
      label1

      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();
      
      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by mrjj

        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.

        ui->label2->resize(pm.width(),pm.height());
        ui->label1->resize(pm1.width(),pm1.height());

        http://postimg.org/image/3uoelm313/

        1 Reply Last reply Reply Quote 1
        • Chris Kawa
          Chris Kawa Moderators last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • M
            mcosta last edited by

            HI,

            why don't reuse the same Painter?

            Once your problem is solved don't forget to:

            • Mark the thread as SOLVED using the Topic Tool menu
            • Vote up the answer(s) that helped you to solve the issue

            You can embed images using (http://imgur.com/) or (http://postimage.org/)

            1 Reply Last reply Reply Quote 0
            • First post
              Last post