QT如何实现圆形按钮?
请看这张图片: http://hi.csdn.net/attachment/201006/28/2702037_1277732191x7A4.png
是方形的一张图,但里面的蓝色区域是圆形的........
我想把我的程序中的按钮的icon设成这张图片,以实现windows自带的播放器Media player里的播放按钮那样的效果.......请教各位大侠怎样实现?
[解决办法]
class Button : public QGraphicsWidget { Q_OBJECT public: Button(const QPixmap &pixmap, QGraphicsItem *parent = 0) : QGraphicsWidget(parent), _pix(pixmap) { setAcceptHoverEvents(true); setCacheMode(DeviceCoordinateCache); } QRectF boundingRect() const { return QRectF(-65, -65, 130, 130); } QPainterPath shape() const { QPainterPath path; path.addEllipse(boundingRect()); return path; } void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) { bool down = option->state & QStyle::State_Sunken; QRectF r = boundingRect(); QLinearGradient grad(r.topLeft(), r.bottomRight()); grad.setColorAt(down ? 1 : 0, option->state & QStyle::State_MouseOver ? Qt::white : Qt::lightGray); grad.setColorAt(down ? 0 : 1, Qt::darkGray); painter->setPen(Qt::darkGray); painter->setBrush(grad); painter->drawEllipse(r); QLinearGradient grad2(r.topLeft(), r.bottomRight()); grad.setColorAt(down ? 1 : 0, Qt::darkGray); grad.setColorAt(down ? 0 : 1, Qt::lightGray); painter->setPen(Qt::NoPen); painter->setBrush(grad); if (down) painter->translate(2, 2); painter->drawEllipse(r.adjusted(5, 5, -5, -5)); painter->drawPixmap(-_pix.width()/2, -_pix.height()/2, _pix); } signals: void pressed(); protected: void mousePressEvent(QGraphicsSceneMouseEvent *) { emit pressed(); update(); } void mouseReleaseEvent(QGraphicsSceneMouseEvent *) { update(); } private: QPixmap _pix; };
[解决办法]
QPushButton *playButton = new QPushButton;playButton->setIcon(QIcon(...));
[解决办法]
一种方式像2楼一样, 另一种方式采用遮罩也行`··
...QPixmap pixmap;QPushButton* pushButton = new QPushButton;pushButton->setIcon(pixmap);pushButton->setMask(pixMap.createHeuristicMask());...