label->show(); ui->noPaperpushButton->show(); QString pathnew = QCo...-Go服务器开发" />

ZBLOG

NoPaper::NoPaper(QWidget *parent) : QWidget(parent), ui(new Ui::NoPaper) { ui->setupUi(this); setStyleSheet("background-color: rgb(255, 0, 0);"); ui->label->show(); ui->noPaperpushButton->show(); QString pathnew = QCo...

要使子控件(如NoPaper类中的控件)在窗口中自动填充整个区域,你可以考虑以下步骤:

  1. 使用正确的布局:确保你在构造函数中为 NoPaper 组件设置了布局,并将其添加到该布局中。在你的代码中,虽然你创建了一个新的 QVBoxLayout,但并没有将具体的控件加入到这个新布局中。

  2. 去掉多余的布局:如果你已经在 UI 文件中定义了 verticalLayout_7 并希望使用它,确保不需要再手动创建新的布局。把所有想要填充的控件放入已存在的布局。

  3. 尺寸策略:给每个子控件适当设置大小策略,确保它们能够随着父窗口扩展而扩展。例如,将某些按钮设置为 QSizePolicy::Expanding 来使它们占据更多空间。

以下是修改后的构造函数示例:

  1. NoPaper::NoPaper(QWidget *parent) :
  2. QWidget(parent),
  3. ui(new Ui::NoPaper)
  4. {
  5. ui->setupUi(this);
  6. setStyleSheet("background-color: rgb(255, 0, 0);");
  7. // 设置标签和按钮可见
  8. ui->label->show();
  9. ui->noPaperpushButton->show();
  10. QString pathnew = QCoreApplication::applicationDirPath() + "/img/noPaper.png";
  11. ui->label->setStyleSheet(QString("background-color: rgba(221, 222, 226);"
  12. "background-image: url(%1);"
  13. "background-repeat: no-repeat;"
  14. "background-position: center;").arg(pathnew));
  15. QString pathadd = QCoreApplication::applicationDirPath() + "/img/addPaper.svg";
  16. QString styleSheet = QString("QPushButton {"
  17. "border: 1px solid rgb(102, 138, 222);"
  18. "border-radius: 5px;"
  19. "background-color: rgb(217, 218, 222 );"
  20. "background-image: url(%1);"
  21. "background-repeat: no-repeat;"
  22. "background-position:left;"
  23. "padding: 120px;"
  24. "text-align: center;"
  25. "} "
  26. ""
  27. "QPushButton:hover {"
  28. "background-color: rgb(102, 138, 222 ) ;"
  29. "}").arg(pathadd);
  30. ui->noPaperpushButton->setStyleSheet(styleSheet);
  31. // 注意这里可以选择是否固定大小,根据需求来定
  32. //ui->noPaperpushButton->setFixedSize(80,30);
  33. // 确保使用 verticalLayout_7
  34. this->setLayout(ui->verticalLayout_7);
  35. // 更新所有相关控件的大小策略,以便它们能合理地扩展
  36. ui->label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  37. ui->noPaperpushButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  38. }

注意事项:

  • 确保 ui 的成员变量(如 verticalLayout_7labelnoPaperpushButton)已经被正确定义并且可以访问。
  • 如果是通过 Qt Designer 创建 UI,请确保在 .ui 文件中已经正确配置了这些元素,并且在实际运行时进行适当的连接和配置。
  • 检查父窗口的大小策略,以及它们如何影响孩子窗口,这对实现期望行为非常重要。
  • 在使用 Qt 的时候,不建议同时维护多个不同的布局,如果在 UI 中已有合适的布局,则直接利用即可。


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

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:label->show(); ui->noPaperpushButton->show(); QString pathnew = QCo...">https://golang.0voice.com/?id=22198

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?