扩展 Qt 布局:如何将一个小部件放在右侧并让另一个小部件完全填充左侧?

发布于 2024-11-16 19:18:17 字数 440 浏览 1 评论 0原文

我想在 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 技术交流群。

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

发布评论

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

评论(4

累赘 2024-11-23 19:18:17

尝试更改:

tmplayout->addWidget(lineEdit, 0, Qt::AlignRight);
tmplayout->addWidget(pushButton, 0, Qt::AlignRight);

至:

tmplayout->addWidget(lineEdit);
tmplayout->addWidget(pushButton);

在处理这样的简单布局时,无需显式指定对齐或拉伸因子。

如果你想强制pushButton为特定大小,你可以使用 setMinimumSize, setMaximumSizesetFixedSize

致以诚挚的问候

Try changing:

tmplayout->addWidget(lineEdit, 0, Qt::AlignRight);
tmplayout->addWidget(pushButton, 0, Qt::AlignRight);

To:

tmplayout->addWidget(lineEdit);
tmplayout->addWidget(pushButton);

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

辞取 2024-11-23 19:18:17

对于您想要位于左侧并展开的小部件,请尝试将它们简单地添加到右侧的小部件之前,并为其拉伸因子添加 1 。例如,

tmplayout->addWidget(exampleWidget, 1);

然后,您可以简单地将想要位于右侧的小部件添加到左侧的小部件之后,只需使用:

tmplayout->addWidget(lineEdit);
tmplayout->addWidget(pushbutton);

这将自动为它们提供 0 的拉伸因子。

由于 exampleWidget< 的拉伸因子本例中的 /code> 为 1,高于默认的 0exampleWidget 将扩展;而且,由于您将其添加在其他内容之前,因此它将位于左侧。

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,

tmplayout->addWidget(exampleWidget, 1);

Then, you could simply add the widgets you want to be on the right side after the ones on the left, using just:

tmplayout->addWidget(lineEdit);
tmplayout->addWidget(pushbutton);

This will automatically give them a stretch factor of 0.

Since the stretch factor of exampleWidget in this example is 1 which is higher than the default 0, exampleWidget will expand; and, since you add it before the others, it will be on the left.

看春风乍起 2024-11-23 19:18:17

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)

汐鸠 2024-11-23 19:18:17

尝试固定最小大小策略。即使您可能想固定最大宽度。

Try fixed or minimum size policies. Even you may want to fix the maximum width.

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