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

标签 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

最佳答案

我认为这里更好的方法是子类化 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

关于qt - 如何在Qt中创建一侧向内弯曲而另一侧平坦的QPushbutton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52105600/

相关文章:

c++ - 使用 QListWidgetItem 类更改文本大小

python - 从 QDockWidget 附加和分离外部应用程序时的问题

c++ - 显示 QMenu 以响应在 QLabel 中右键单击,可能吗?

c++ - Qt creator 4 找不到QCamera

c++ - 使用导入的 makefile 将 Qt Creator .config 添加到 .h

c++ - 如何将 QQuickItem 扩展作为子扩展添加到另一个 QQuickItem 扩展?

c++ - Qt - 如何检测自动换行后行数的增加?

c++ - 无滚动但可拖动的小部件

qt5 - 如何避免带有 borderimage 的 QPushButton 的文本剪切

c++ - 通过 QML 仿函数对 C++ 模型进行排序和过滤?