Qt QHboxLayout 单元格大小问题

发布于 2024-08-10 12:10:44 字数 386 浏览 8 评论 0原文

对于那些还没有读过我的 Qt 问题的人,我正在为一个项目学习 Qt。我在 GUI 设计方面的经验非常有限,在 Qt 方面更是没有。

我有一个水平布局,我想用一些按钮填充它。我可以很好地输入这些按钮,但是我以前的方形按钮被水平拉伸以占用更多空间。

我想让布局管理器确定调整这些按钮大小的最佳方式,但我也希望它们的原始比例保持不变。例如,如果我开始使用需要缩小以适合布局中的所有按钮的 32X32 按钮,我希望它们按比例缩小,以便保持宽度与高度的比例。 20X20、16X16、12X12 都可以,但 24X16 是不可接受的尺寸示例。

我修改了按钮和拉伸选项的尺寸策略。即使在阅读了 QPushButton 和 QHboxLayout 类之后,我也没有看到如何做到这一点。它是如何实现的?

谢谢。

For those of you who haven't been reading my Qt questoins, I am learning Qt for a project. I have only limited experience with GUI design at all, and not in Qt.

I've got a horizontal layout that I want to populate with some buttons. I can feed these buttons in just fine, but my formerly-square buttons are stretched horizontally to take up more space.

I want to let the layout manager determine the best way to size these buttons, but I also want their original proportions to remain intact. For instance, if I start would with 32X32 buttons that need to shrink to fit all of them in the layout, I want them to shrink proportionally so that the width to height scale is maintained. 20X20, 16X16, 12X12 would all be just fine, but 24X16 would be an example of dimensions that are unacceptable.

I've tinkered with size policies on the buttons and stretch options. I'm not seeing, even after reading the QPushButton and QHboxLayout classes how to do this. How is it accomplished?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的十字路口 2024-08-17 12:10:44

只要我正确理解问题,我认为您想要的是 QBoxLayout::addStretch()。这将添加一个填充对象来填充未使用的空间。因此,按钮将具有理想的尺寸,而垫片将填充其余部分。您可以尝试在 Designer 中进行试验,它比写入/编译/运行周期更容易。

As long as I understand the question correctly, I think what you want is QBoxLayout::addStretch(). This will add a spacer object that fills the unused space. So the buttons will have their ideal size and the spacer will fill the rest. You can try experimenting with this in Designer, it's easier than the write/compile/run cycle.

过期情话 2024-08-17 12:10:44

您应该查看这个问题的答案< /a>.这是我的回答的回顾。

您需要创建 QLayoutItem 的自定义派生类,它重写 bool hasHeightForWidth() 和 int heightForWidth( int width ) 来保留纵横比。您可以传入按钮并查询它,也可以直接设置比率。您还需要确保 widget() 函数返回指向正确按钮的指针。

完成后,您可以像添加小部件一样将布局项添加到布局中。因此,当添加按钮时,请将其更改为使用自定义布局项类。

我还没有实际测试过这些,所以目前它只是一个理论上的解决方案。如果需要的话,我不知道有什么方法可以通过设计器来完成此解决方案。

You should take a look at the answers to this question. This is a recap of my answer there.

You need to create a custom derivative of QLayoutItem, which overrides bool hasHeightForWidth() and int heightForWidth( int width ) to preserve the aspect ratio. You could either pass the button in and query it, or you could just set the ratio directly. You'll also need to make sure the widget() function returns a pointer to the proper button.

Once that is done, you can add a layout item to a layout in the same manner you would a widget. So when your button gets added, change it to use your custom layout item class.

I haven't actually tested any of this, so it is a theoretical solution at this point. I don't know of any way to do this solution through designer, if that was desired.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文