Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ qt-widget-positioning_C++_User Interface_Qt - Fatal编程技术网

C++ qt-widget-positioning

C++ qt-widget-positioning,c++,user-interface,qt,C++,User Interface,Qt,我想把一些小部件放在一些随机的地方,比如一个按钮在点(10,10)上,另一个按钮在点(15,40)上,等等。如何实现这一点?。QGridLayout将所有内容都转换为行-列样式。但是我想把小部件放在任何我想要的地方,有人能帮我吗?如果你真的想设置绝对位置,我会完全忽略使用布局。您可以使用move()函数或setGeometry()函数手动设置图元的位置 QWidget *parent = new QWidget(); parent->resize(400, 400); QPushButt

我想把一些小部件放在一些随机的地方,比如一个按钮在点(10,10)上,另一个按钮在点(15,40)上,等等。如何实现这一点?。QGridLayout将所有内容都转换为行-列样式。但是我想把小部件放在任何我想要的地方,有人能帮我吗?

如果你真的想设置绝对位置,我会完全忽略使用布局。您可以使用
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);
旁注:我会避免在Qt中设置元素的绝对位置。为什么?Qt试图成为一个独立于平台的GUI库。在不同的平台上,许多显示内容可能会发生变化(即按钮中文本的字体大小),因此实际按钮的大小可能会有所不同,以适应较大或较小的字体大小。如果您使用上述示例中的绝对位置,则可以取消精心设置的按钮


如果使用布局,可以避免重叠按钮或从窗口边缘脱落的按钮。

您可以在QT中看到我对重叠按钮的回答:。这可能有助于实现您的目标。

+1在此处添加了一条注释,说明如何手动右对齐小部件。