QLabel將方形圖片顯示成圓形

想把QLabel中傳入的方形圖片切成原型顯示,嘗試用樣式表寫圓角能完成純背景色切成圓形顯示,但是不能將圖片切成圓形。

於是上網搜尋答案,找到了解決我疑惑的帖子

https://blog.csdn.net/liukang325/article/details/78150504?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-5.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-5.nonecase

貼代碼

QPixmap L::PixmapToRound(QPixmap &src, int radius)
{
    if (src.isNull()) {
        return QPixmap();
    }

    QSize size(2 * radius, 2 * radius);
    QBitmap mask(size);
    QPainter painter(&mask);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::SmoothPixmapTransform);
    painter.fillRect(0, 0, size.width(), size.height(), Qt::white);
    painter.setBrush(QColor(0, 0, 0));
    painter.drawRoundedRect(0, 0, size.width(), size.height(), 99, 99);

    QPixmap image = src.scaled(size);
    image.setMask(mask);
    return image;
}

    //使用方法  圖片56*56
    QPixmap pixmap_userIcon;
    pixmap_userIcon.load(":/png/images/t_head.png");
    QPixmap fitpixmap_userIcon = pixmap_userIcon.scaled(56, 56, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    fitpixmap_userIcon = PixmapToRound(fitpixmap_userIcon, 28);
    m_ui.label_photo->setPixmap(fitpixmap_userIcon);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章