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的大小.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值