将 QGridLayout 中跨多列的按钮居中
我有一个跨越网格布局中两列的按钮。该按钮大约是其单元格宽度的一半,我希望它居中。
gridLayout->addWidget(btn3, 2, 6, 1, 2);
将按钮与单元格左侧对齐,我认为这是默认设置。我尝试了一下
gridLayout->addWidget(btn3, 2, 6, 1, 2, Qt::AlignHCenter);
,按钮消失了。我的按钮去了哪里?为什么它不在布局单元的中心?
我在 Linux (ubuntu 10.04) 中使用 QT opensoure。
一些上下文:
gridLayout->addWidget(btn1, 2, 3);
gridLayout->addWidget(btn2, 2, 5);
gridLayout->addWidget(btn3, 2, 6, 1, 2);
gridLayout->addWidget(btn4, 2, 8);
gridLayout->addWidget(btn5, 2, 10);
我希望 btn3 位于其两列跨越单元格的中心,但是当我尝试将 Qt:AlignCenter 作为 addWidget() 方法中的第 6 个参数传递时,btn3 消失了。
I have a button that spans two columns in a Grid Layout. The button is about half the width of its cell and I want it to be centered.
gridLayout->addWidget(btn3, 2, 6, 1, 2);
aligns the button the left of the cell, which I would assume to be the default. I tried
gridLayout->addWidget(btn3, 2, 6, 1, 2, Qt::AlignHCenter);
and the button disappears. Where did my button go and why is it not in the center of my layout cell?
I am using QT opensoure in Linux (ubuntu 10.04).
A bit of context:
gridLayout->addWidget(btn1, 2, 3);
gridLayout->addWidget(btn2, 2, 5);
gridLayout->addWidget(btn3, 2, 6, 1, 2);
gridLayout->addWidget(btn4, 2, 8);
gridLayout->addWidget(btn5, 2, 10);
I want btn3 to be in the center of its two-column-spanning cell, but when I attempt to pass Qt:AlignCenter as the 6th argument in the addWidget() method, btn3 disappears.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的 ColumnSpan 为 2,您的按钮可能位于 gridLayout 的其他某个 Widget 下。尝试将您的代码更改为:
它对我来说效果很好。
You are using a ColumnSpan of 2, your button is probably under some other Widget of your gridLayout. Try changing your code to:
It works fine to me.