C++ 如何为自定义形状的Qpushbutton设置样式表
我继承了Qpushbutton使其成为圆形,如果我在这个类的paintevent中设置背景,它可以正常工作,但我想为这个类的每个瞬间设置不同的图像,所以我尝试通过seticon和setstylesheet设置它,但它不起作用。原因可能是什么。请帮帮我。 提前谢谢 这是我的密码C++ 如何为自定义形状的Qpushbutton设置样式表,c++,qt,C++,Qt,我继承了Qpushbutton使其成为圆形,如果我在这个类的paintevent中设置背景,它可以正常工作,但我想为这个类的每个瞬间设置不同的图像,所以我尝试通过seticon和setstylesheet设置它,但它不起作用。原因可能是什么。请帮帮我。 提前谢谢 这是我的密码 CirclularButton::CirclularButton(QWidget *parent):QPushButton(parent) { } void CirclularButton::paintEvent(QPa
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");
如文档中所述,您需要将其添加到绘制事件中:
void paintEvent(QPaintEvent *e)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
//your other code here
}
还是不行。我尝试了以下QPushButton::paintEvent(e);qstyleopt;选择初始(本);油漆工p(本);style()->drawPrimitive(QStyle::PE_小部件,&opt,&p,this);p、 SetRenderInts(QPaint::抗锯齿);p、 抽屉长度(0,0,50,50);这会设置图标,但不是圆形,Qpushbutton的形状也会更改为normalSet,在样式表中将边框半径设置为“border radius:10px”;您在paint事件中得到的是您将在屏幕上得到的。你不应该期待任何其他事情。您可以在中看到基类实现。