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. Draw polygon with image inside
Qt 6.11 is out! See what's new in the release blog

Draw polygon with image inside

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 3.5k Views 1 Watching
  • 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.
  • L Offline
    L Offline
    Lunarix
    wrote on last edited by
    #1

    Hello,
    I'd like to draw a polygon in a QGraphicsView. This works. Now I'd like to add a "background" image for the polygon.
    A simple color like green works, but I'd like to use my own .png image.

        QVector<QPointF> hexPoints;
        hexPoints << QPointF(71,20) <<QPointF(35,0) <<QPointF(0,20) <<
                     QPointF(0,50) <<QPointF(35,71) <<QPointF(71,50);
        QPolygonF my_polygon(hexPoints);
    
        QImage *myImage = new QImage("my.png");
        if(myImage->isNull()){
            std::cout << "Could not read image!" << std::endl;
        }
        QBrush *myb = new QBrush(*myImage);
    
        QPen outlinePen(Qt::black);
    
        QGraphicsScene *scene = new QGraphicsScene(this);
        scene->addPolygon(my_polygon,outlinePen,*myb);
        ui->graphicsView->setScene(scene);
    

    But sadly there is no image in my polygon.
    How to fix it?
    Thanks in advance!

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kenchan
      wrote on last edited by
      #2

      @Lunarix
      did you try setting the brush style to Qt::TexturePattern?

      1 Reply Last reply
      2
      • L Offline
        L Offline
        Lunarix
        wrote on last edited by
        #3

        Hello @kenchan,
        I added

            QBrush *myb = new QBrush(*myImage);
            myb->setStyle(Qt::TexturePattern); // added
        
        

        but still, I can not see any image :/ what do I do wrong?

        1 Reply Last reply
        0
        • L Offline
          L Offline
          Lunarix
          wrote on last edited by
          #4

          Solved it. The image was white where it was displayed - works with different image. x.x

          1 Reply Last reply
          2
          • K Offline
            K Offline
            kenchan
            wrote on last edited by
            #5

            @Lunarix
            Glad to hear you were able to get it to work.

            1 Reply Last reply
            1

            • Login

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