qt 自定义外形 qpushbutton

http://bbs.csdn.net/topics/340175652

以圆钮为例,个人猜想:

方法一)png皮肤法

1、皮肤里放个圆的png图片;通过皮肤设置,让按钮呈现出圆形;并且点圆周外,无任何反应。

用皮肤法实现了,按钮呈现出圆形效果,美中不足,点圆周外,方格内,有反应。

方法二)png图片法

2、继承QPushButton,在QMyPushButton重绘按钮。

3.使用setMask

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的大小.


  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值