当前位置:文档之家› QtWidget 自定义不规则窗体与按钮

QtWidget 自定义不规则窗体与按钮

关键是使用
void QWidget::setMask ( const QBitmap & bitmap )
void QWidget::setMask ( const QRegion & region )
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的大小.

相关主题
文本预览
相关文档 最新文档