0
votes

I have inherited Qpushbutton to make it circular shape, if I set background in paintevent of this class it works fine but I want to set different image for each instant of this class so I tried setting it by seticon and setstylesheet but it does not work. What could be the reason. please help me out. Thanks in advance.

this is my code.

CirclularButton::CirclularButton(QWidget *parent):QPushButton(parent)
{
}

void CirclularButton::paintEvent(QPaintEvent *e)
{
    QPainter p(this);
    p.setRenderHints(QPainter::Antialiasing);
    p.drawEllipse(0, 0, 50, 50);
}

and in another class where I am using it,

 CirclularButton *cir = new CirclularButton(this);
    cir->setGeometry(50,50,50,50);
    cir->setStyleSheet("border:1px solid red");
1
What you have in the paint event is what you'll get on the screen. You shouldn't expect anything else. You can see the base class implementation in the source code. - thuga

1 Answers

0
votes

As explained in the docs here, you will need to add this into your paint event:

 void paintEvent(QPaintEvent *e)
 {
     QStyleOption opt;
     opt.init(this);
     QPainter p(this);
     style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);

     //your other code here
 }