QT开发中文网

ui->pushButton设置大小和圆角

要设置 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助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=12325

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?