labelNew->setStyleSheet(QString("background-image: url(%1);" "background-repeat: no-r...-Go服务器开发" />

ZBLOG

QString currentPath = QCoreApplication::applicationDirPath(); QString pathnew = currentPath + "/img/new.svg"; ui->labelNew->setStyleSheet(QString("background-image: url(%1);" "background-repeat: no-r...

在 Qt 中,如果您发现 labelNew 在布局中没有居中,可能是由于以下几个原因:

  1. Margins 和 Spacing: 您的布局可能有额外的边距或间距。可以检查父布局的设置,看看是否有设置导致空间增加。

  2. Contents Margins: 您已将 setContentsMargins(0, 0, 0, 10) 设置为标签,这会在底部留出 10 像素的空白。如果希望完全去掉这个底部空间,可以将其改为 (0, 0, 0, 0)

  3. Alignment: 您已正确地使用 setAlignment(Qt::AlignBottom | Qt::AlignHCenter) 来设置标签对齐方式。如果要使其垂直居中,则需要使用 Qt::AlignVCenter 而不是 Qt::AlignBottom

以下是修改后的代码示例,使得标签在垂直和水平上都居中:

  1. QString currentPath = QCoreApplication::applicationDirPath();
  2. QString pathnew = currentPath + "/img/new.svg";
  3. ui->labelNew->setStyleSheet(QString("background-image: url(%1);"
  4. "background-repeat: no-repeat;"
  5. "background-position: center;").arg(pathnew));
  6. // 修改为垂直居中
  7. ui->labelNew->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
  8. // 将内容边距设为 0,以避免多余空间
  9. ui->labelNew->setContentsMargins(0, 0, 0, 0);

确保在 labelNew 的父控件或布局上没有设置额外的边距或间隔,这样就能够达到理想的效果。如果仍然存在问题,请检查其他控件的布局属性以及整个窗口或控件的大小策略。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:labelNew->setStyleSheet(QString("background-image: url(%1);" "background-repeat: no-r...">https://golang.0voice.com/?id=22155

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?