1、方法1:准備一張邊界是透明的不規則圖形
QPushButton * pbtn = new QPushButton;
pbtn->setStyleSheet("QPushButton{border:0px;}");//這句務必加上,否則看到的就是矩形了,而不是不規則圖形了
pbtn->setText("aaa");
pbtn->setIcon(QPixmap("://louDong.png"));
pbtn->setIconSize(QPixmap("://louDong.png").size());
pbtn->resize(QPixmap("://louDong.png").size());
效果如下:
方法2:
QPushButton * pbtn = new QPushButton;
pbtn->setFixedSize(QPixmap("://louDong.png").size());
pbtn->setStyleSheet("border-image:url(://louDong.png)");
方法3:
QPushButton * pbtn = new QPushButton;
pbtn->setFixedSize(QPixmap("://louDong.png").size());
pbtn->setIcon(QPixmap("://louDong.png"));
pbtn->setIconSize(QPixmap("://louDong.png").size());
pbtn->setMask(QPixmap("://louDong.png").createHeuristicMask()); //不過該方法效果並不好,能看到button的邊緣有鋸齒,createHeuristicMask換成mask也是一樣。
方法4:
繼承qpushButton,重寫paintevent,在里面可以設置mask或者通過qpainterPath自己構造不規則輪廓,代碼如下:
//重繪事件,構造不規則圖形
void DownloadMaskWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this); //創建painter
painter.setRenderHint(QPainter::Antialiasing, true); //消除鋸齒效果
//構造painterpath
QPainterPath path;
path.moveTo(0, 0);
path.lineTo(DOWNLOAD_MASK_WIDTH, 0);
path.lineTo(DOWNLOAD_MASK_WIDTH/2, DOWNLOAD_MASK_HEIGHT);
path.lineTo(0, 0);
//path->setFillRule(Qt::WindingFill);
//設置無畫筆,避免邊框出現一條黑線
painter.setPen(Qt::NoPen);
//設置畫刷
painter.setBrush(QBrush(QColor(36,169,225), Qt::SolidPattern));
//繪制背景
painter.drawPath(path);
event->accept();//不再向父類傳遞消息
}
方法5:
對於qlabel,pLabelUnInstallingIcon->setFixedSize(40,40);
pLabelUnInstallingIcon->setScaledContents(true);
pLabelUnInstallingIcon->setPixmap(QPixmap(iconPath));
這樣,可以保證圖片不失真,如果用pLabelUnInstallingIcon.setPixmap(QPixmap(iconPath).scaled(40,40))的話可以看到label的邊緣有鋸齒
http://blog.csdn.net/u013281495/article/details/50894096