【问题标题】:how to set style sheet for custom shaped Qpushbutton如何为自定义形状的 Qpushbutton 设置样式表
【发布时间】:2015-04-16 22:29:30
【问题描述】:

我继承了 Qpushbutton 使其成为圆形,如果我在此类的paintevent中设置背景它工作正常,但我想为此类的每个瞬间设置不同的图像,所以我尝试通过 seticon 和 setstylesheet 设置它,但它确实不行。可能是什么原因。请帮帮我。 提前致谢。

这是我的代码。

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");

【问题讨论】:

  • 您在绘画活动中拥有的就是您将在屏幕上看到的。你不应该期待别的。可以在source code看到基类实现。

标签: c++ qt


【解决方案1】:

如文档 here 中所述,您需要将其添加到您的绘画事件中:

 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); QStyleOption 选择; opt.init(这个); QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); p.setRenderHints(QPainter::Antialiasing); p.drawEllipse(0, 0, 50, 50);这设置了图标,但不是圆形,Qpushbutton 的形状也变为正常
  • 在样式表中设置border-radius为“border-radius:10px;”
猜你喜欢
  • 2012-11-06
  • 2020-02-17
  • 1970-01-01
  • 2020-03-05
  • 1970-01-01
  • 1970-01-01
  • 2013-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多