要设置 QPushButton 的大小和圆角,可以使用以下代码:
QPushButton *button = new QPushButton("按钮", parent); // 创建 QPushButton 对象
// 设置按钮大小
button->setFixedSize(QSize(width, height)); // 替换 width 和 height 为所需的大小值
// 设置按钮圆角
QStyleOptionButton option;
option.initFrom(button);
option.rect = button->rect();
option.features = QStyleOptionButton::None;
if (button->isEnabled())
option.state |= QStyle::State_Enabled;
if (button->isChecked())
option.state |= QStyle::State_On;
else
option.state |= QStyle::State_Off;
QPainter painter(button);
button->style()->drawControl(QStyle::CE_PushButton, &option, &painter, button);
int radius = 10; // 替换为所需的圆角半径值
QPainterPath path;
path.addRoundedRect(option.rect, radius, radius);
painter.fillPath(path, QColor(Qt::GlobalColor::red)); // 替换为所需的背景颜色
button->setMask(path.toFillPolygon().toPolygon()); // 将按钮的遮罩设置为圆角形状
请注意,上述代码是基于 Qt 框架进行编写,你可能需要将其适应到你正在使用的 UI 框架中。
内容由零声教学AI助手提供,问题来源于学员提问