void QWidget::setMask ( const QRegion & region )
Causes only the parts of the widget which overlap region to be visible.
只有widget與region重疊的地方才會顯示出來. 自己構造一個QRegion就行了.
void ShapedClock::resizeEvent(QResizeEvent * /* event */) {
int side = qMin(width(), height());
QRegion maskedRegion(width() / 2 - side / 2, height() / 2 - side / 2, side,
side, QRegion::Ellipse);
setMask(maskedRegion);
}
void QWidget::setMask ( const QBitmap & bitmap )
Causes only the pixels of the widget for which bitmap has a corresponding 1 bit to be visible. If the region includes pixels outside the rect() of the widget, window system controls in that area may or may not be visible, depending on the platform.
只有在bitmap中像素數據是1的地方才會顯示出widget的相應像素來. Bitmap就是像素數據只有兩個值: 0和1 (1 bit-depth, monochrome).
QLabel topLevelLabel;
QPixmap pixmap(":/images/tux.png");
topLevelLabel.setPixmap(pixmap);
topLevelLabel.setMask(pixmap.mask()); // 可以不使用轉換的, 使用一張專門的bitmap圖片.
上面的這些方式用一普通的QWidget就可以了. 當然, 對于窗口而言, 很多時候我們要把它的標題欄去掉:
widget->setWindowFlags(Qt::FramelessWindowHint);
但是對于不規則的QPushButton就有些特殊, 要使用QIcon來處理:
button->setIcon(QIcon("xxx.png"));
button->setIconSize(w, h);
button->setMask(maskBitmap/*maskedRegion*/);
button->setFixedSize(w, h); // 這個當然最好使用它的icon的大小.