逛奔的蜗牛

我不聪明,但我会很努力

   ::  :: 新随笔 ::  ::  :: 管理 ::
关键是使用
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的大小.
posted on 2009-05-24 17:13 逛奔的蜗牛 阅读(7531) 评论(3)  编辑 收藏 引用 所属分类: Qt自定义Widget

评论

# re: QtWidget: 自定义不规则窗体与按钮 2009-07-21 17:03 QT初学者
button->setMask(maskBitmap/*maskedRegion*/);
这句话能否讲解一下?
maskBitmap是什么类型,从何而来?setMask()函数的作用是什么?谢谢哈。  回复  更多评论
  

# re: QtWidget: 自定义不规则窗体与按钮 2010-04-15 19:59 喃喃
是这样的,不错  回复  更多评论
  

# re: QtWidget: 自定义不规则窗体与按钮 2011-08-09 13:08 QT菜鸟
写的好粗略。。除了得知应该用到QRegion之外其他都有点不知所云。。  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理