简体   繁体   English

qt - 小部件 - 定位

[英]qt - widget - positioning

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?.我想在一些随机位置的父小部件中放置一些小部件,例如点 (10,10) 处的一个按钮和 (15,40) 处的另一个按钮等。如何实现这一点?。 QGridLayout is pushing everything into row column style. QGridLayout 将所有内容都推入行列样式。 But I want to put the widgets whereever I want,Can anybody help me?但是我想把小部件放在任何我想要的地方,有人可以帮我吗?

If you really want to set absolute positions, I would ignore using a layout altogether.如果你真的想设置绝对位置,我会完全忽略使用布局。 You can manually set the positions of elements by using the move() function or the setGeometry() function.您可以使用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.旁注:我会避免在 Qt 中设置元素的绝对位置。 Why?为什么? Well, Qt tries to be a platform-independent GUI library.好吧,Qt 试图成为一个独立于平台的 GUI 库。 On different platforms, a lot of display things can change (ie 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.如果您在上面的示例中使用绝对位置,则这可以摆脱您精心间隔的按钮。

If you use layouts, overlapping buttons or buttons falling off the edge of your window can be avoided.如果您使用布局,可以避免重叠按钮或按钮从窗口边缘脱落。

You can see my answer for overlay button in QT: Qt Widget Overlays .您可以在 QT 中查看我对叠加按钮的回答: Qt Widget Overlays This may help you to achieve what you want.这可能会帮助你实现你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM