Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Possible to use two QPainters at the same time?
QtWS25 Last Chance

Possible to use two QPainters at the same time?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 4 Posters 902 Views
  • 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 Offline
    M Offline
    marlenet15
    wrote on last edited by
    #1

    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
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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
      1
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          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
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved