Draw polygon with image inside



  • 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!



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



  • 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?



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



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


Log in to reply