C++ qt - 小部件 - 定位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3608928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
qt - widget - positioning
提问by prabhakaran
I want to place some widgets in a parent widget in some random places, like one button at Point (10,10) and another at (15,40),etc. How to achieve this. QGridLayout is pushing everything into row column style. But I want to put the widgets whereever I want,Can anybody help me?
我想在一些随机位置的父小部件中放置一些小部件,例如点 (10,10) 处的一个按钮和 (15,40) 处的另一个按钮等。如何实现这一目标。QGridLayout 将所有内容都推入行列样式。但是我想把小部件放在任何我想要的地方,有人可以帮我吗?
回答by Joel Verhagen
If you reallywant to set absolute positions, I would ignore using a layout all together. You can manually set the positions of elements by using using the movefunction or the setGeometryfunction.
如果你真的想设置绝对位置,我会忽略一起使用布局。您可以使用move函数或setGeometry函数手动设置元素的位置。
QWidget *parent = new QWidget();
parent->resize(400, 400);
QPushButton *buttonA = new QPushButton(parent);
buttonA->setText("First Button");
buttonA->move(10, 10);
QPushButton *buttonB = new QPushButton(parent);
buttonB->setText("Second Button");
buttonB->move(15, 40);
Side note:I would avoid setting absolute positions of elements in Qt. Why? Well, Qt tries to be a platform-independent GUI library. On different platforms, a lot of display things can change (i.e. font size of text in push buttons) so the size of your actual push buttons can vary to accommodate large or smaller font sizes. This can throw off your meticulously spaced push buttons is you use absolute positions as in the example above.
旁注:我会避免在 Qt 中设置元素的绝对位置。为什么?好吧,Qt 试图成为一个独立于平台的 GUI 库。在不同的平台上,许多显示内容可能会发生变化(即按钮中文本的字体大小),因此实际按钮的大小可能会有所不同,以适应较大或较小的字体大小。如果您使用绝对位置,如上例所示,这可以摆脱您精心间隔的按钮。
If you use layouts, overlapping buttons or buttons falling off the edge of your window can be avoided.
如果您使用布局,可以避免重叠按钮或按钮从窗口边缘脱落。
回答by Patrice Bernassola
You can see my answer for overlay button in QT: Qt Widget Overlays. This may help you to achieve what you want.
您可以在 QT 中查看我对叠加按钮的回答:Qt Widget Overlays。这可能会帮助你实现你想要的。