C++ QWidget呈现到QPixmap似乎忽略了;“边界半径”;样式表的设置

C++ QWidget呈现到QPixmap似乎忽略了;“边界半径”;样式表的设置,c++,qt,qwidget,qpixmap,C++,Qt,Qwidget,Qpixmap,我需要从QWidget保存QPixmap(在我的例子中是QPushButton),但似乎忽略了一些样式表设置 下面是一个示例代码: button = new QPushButton(QIcon(":/resources/icons/my_icon.png"), "Sample text", this); button->setFocusPolicy(Qt::NoFocus); button->setStyleSheet(" color: white; "

我需要从QWidget保存QPixmap(在我的例子中是QPushButton),但似乎忽略了一些样式表设置

下面是一个示例代码:

button = new QPushButton(QIcon(":/resources/icons/my_icon.png"), "Sample text", this);
button->setFocusPolicy(Qt::NoFocus);
button->setStyleSheet(" color: white; "
                      " background-color: gray; "
                      " font: 30px;"
                      " border-radius: 18px;");

button->setLayoutDirection(Qt::RightToLeft);
button->setIconSize(QSize(96, 96));
button->setGeometry(13, 10, 455, 100);

button->show();

QPixmap pixmap(button->size());
button->render(&pixmap);
渲染方法似乎忽略了样式表的“border radius:18px;”字段,我已尝试将QPixmap保存到一个文件:

pixmap.save("test.png");
但保存的图像是一个矩形,没有圆形边框。 当应用程序显示时,QPushButton具有正确的圆形边框


我在使用QWideget::render(…)方法时是否做错了什么?

您的问题是render func中的QRegion是一个矩形区域

void QWidget::render(QPaintDevice *target, const QPoint &targetOffset = QPoint(), const QRegion &sourceRegion = QRegion(), QWidget::RenderFlags renderFlags = RenderFlags(DrawWindowBackground | DrawChildren))
Qt不知道如何制作圆角矩形区域,您应该手动计算此区域,您可以使用此函数开始;)

然后在渲染函数中使用它

button->render(&pixmap,QPoint(),computeRegion(button,18));
button->render(&pixmap,QPoint(),computeRegion(button,18));