如何在Qt中创建一侧向内弯曲而另一侧平坦的QPushbutton

时间:2018-08-30 21:27:56

标签: qt qtwidgets

QPushButton {    
    border: 2px solid red;
    height:24px;
    width:100px;
    border-top-left-radius: 50px;
    border-bottom-left-radius: 50px;
    border-top-right-radius: 0px;
    border-bottom-right-radius: 0px; 
}

这使我像这样倒圆,而另一边平整
enter image description here

我希望在一侧具有向内的弯曲,而在另一侧具有平坦的形状,例如平凹,这样就可以实现这一目的
 enter image description here

1 个答案:

答案 0 :(得分:4)

我认为这里更好的方法是将QPushButton子类化并重新实现paintEvent。像这样

void PushButton::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // Define pen
    QPen pen;
    pen.setWidth(4);

    // Draw outer cover
    pen.setColor(Qt::black);
    painter.setPen(pen);
    painter.setBrush(QColor(Qt::gray));
    painter.drawRect(this->rect());

    // Draw Inward arc
    pen.setWidth(2);
    painter.setPen(pen);
    painter.setBrush(QColor(Qt::white));

    int x = this->rect().x() - this->rect().width()/2;
    int y = this->rect().y();
    int w = this->rect().width();
    int h = this->rect().height();
    QRectF rectangle(x,y,w,h);
    painter.drawRoundedRect(rectangle,w,h);

}

请记住,这只是一个示例,您应该考虑其他因素,例如小部件的大小以及向内固化的角度以及其他所有因素。

This is how it looks; I have programmed assuming its a the size of widget is always a square