开发者

How to create round shape buttons in Qt and set a round shape background image on it? How can we activate pixels of specific part of image in Qt

i am trying to add a round shape button in my project开发者_开发技巧 and i want to set round shape background image on it but the problem is that while setting any background image it is always taking the rectangular image.


You must use bitmap images with transparent background as buttons.

    CustomButton::CustomButton(QString file,QString pressedfile,QWidget *parent,int id)
    : QPushButton(parent),FileName(file),PressedName(pressedfile),ID(id)
{
    connect(this,SIGNAL(clicked()),SLOT(slotClicked()));
    setStyleSheet("border: 2px");

QPixmap pixmap(file)    ;
setMinimumHeight(pixmap.height());
setMinimumWidth(pixmap.width());
setMaximumHeight(pixmap.height());
     setMaximumWidth(pixmap.width());

} void CustomButton::slotClicked() { emit clicked(ID); }

void CustomButton::setColor(const QColor &c) { fontColor=c; }

//Paint event of button void CustomButton::paintEvent(QPaintEvent *paint) { QPainter p(this); p.save();

p.setPen(Qt::blue); QPixmap pixmapdown; if(PressedName>0) pixmapdown.load(PressedName); else pixmapdown.load(FileName); QPixmap pixmap; pixmap.load(FileName); if(isDown()) p.drawPixmap(1,1,pixmapdown); else p.drawPixmap(1,1,pixmap); if(text().length()>0) { p.setPen(fontColor); p.drawText(rect(), Qt::AlignCenter|Qt::AlignHCenter, text()); } p.restore(); p.end();}


You could also use a QStyle derived class, although this might be over the top if you really only want these round buttons and not tons of widgets with custom styling.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜