扩展 Qt 布局:如何将一个小部件放在右侧并让另一个小部件完全填充左侧?
我想在 QHBoxLayout 的右侧放置一个小部件,其他空间应扩展左侧。我已将小部件的 SizePolicy 设置为 Expanding,但它无效。有人可以提供一些帮助吗?谢谢。
代码在这里:
QHBoxLayout* tmplayout = new QHBoxLayout(this);
tmplayout->setContentsMargins(0, 0, 0, 0);
lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Ignored);
tmplayout->addWidget(lineEdit, 0, Qt::AlignRight);
tmplayout->addWidget(pushButton, 0, Qt::AlignRight);
lineEdit 应该展开。
I want to put a widget on the right side of a QHBoxLayout, and the other spaces should expand the left side. I've set the widget's SizePolicy to Expanding, but it's not valid. Anyone could offer some help? Thanks.
Code is here:
QHBoxLayout* tmplayout = new QHBoxLayout(this);
tmplayout->setContentsMargins(0, 0, 0, 0);
lineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Ignored);
tmplayout->addWidget(lineEdit, 0, Qt::AlignRight);
tmplayout->addWidget(pushButton, 0, Qt::AlignRight);
lineEdit should expand.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试更改:
至:
在处理这样的简单布局时,无需显式指定对齐或拉伸因子。
如果你想强制pushButton为特定大小,你可以使用 setMinimumSize, setMaximumSize 和 setFixedSize
致以诚挚的问候
Try changing:
To:
When dealing with simple layouts like this, there is no need to specify alignments or stretch factors explicitly.
If you want to force pushButton to specific size, you can use setMinimumSize, setMaximumSize, and setFixedSize
Best regards
对于您想要位于左侧并展开的小部件,请尝试将它们简单地添加到右侧的小部件之前,并为其拉伸因子添加
1
。例如,然后,您可以简单地将想要位于右侧的小部件添加到左侧的小部件之后,只需使用:
这将自动为它们提供 0 的拉伸因子。
由于
exampleWidget< 的拉伸因子本例中的 /code> 为
1
,高于默认的0
,exampleWidget
将扩展;而且,由于您将其添加在其他内容之前,因此它将位于左侧。For the widgets you want to be on the left and expand, try to simply add them before the ones on the right, and add a
1
for their stretch factor. For example,Then, you could simply add the widgets you want to be on the right side after the ones on the left, using just:
This will automatically give them a stretch factor of 0.
Since the stretch factor of
exampleWidget
in this example is1
which is higher than the default0
,exampleWidget
will expand; and, since you add it before the others, it will be on the left.我不确定我是否正确理解您的问题,但也许您需要研究 QSpacerItem。如果这没有帮助,也许您可以在 Qt-Creator 中制作一个粗略的模型并发布屏幕截图(一个示例显示您拥有的大约内容,一个示例显示您想要的内容)
I am not sure if I understand your problem correctly, but maybe you need to look into QSpacerItem. If that is not helpful, maybe you can make a rough mock-up in Qt-Creator and post the screen shots (one example showing approx. what you have, one example showing what you want)
尝试
固定
或最小
大小策略。即使您可能想固定最大宽度。Try
fixed
orminimum
size policies. Even you may want to fix the maximum width.