简体   繁体   English

在QT中,如何在自定义小部件中为布局定义几何

[英]In QT, How to define the geometry for the Layout in a custom Widget

In a custom QWidget (say MyWidget ) I include a layout to manage children widgets: 在自定义QWidget (例如MyWidget ),我包括一个布局来管理子部件:

MyWidget window;
QPushButton button;
QVBoxLayout layout(window);
layout.addWidget(button);
window.show();

I would like the layout to be in a specific position and size, by default, QWidget set it to the whole geometry. 我希望布局处于特定的位置和大小,默认情况下,QWidget将其设置为整个几何。

How can I set the geometry the layout will consider for managing his space? 如何设置layout将用于管理其空间的几何图形?

As an indirect question: Which function the layout use to set children geometries? 作为一个间接的问题:布局用于设置子几何的功能是什么?

QPushButton *button = new QPushButton;
verticalLayout->addSpacing(300);
verticalLayout->addWidget(button);
button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
button->setMinimumSize(500, 200);
button->setMaximumSize(500, 200);

If you need both vertical and horizontal spacing - use QGridLayout . 如果同时需要垂直和水平间距,请使用QGridLayout

The QLayout use as margins the summary of both: QLayout将以下两者的摘要用作边距:

  • The parent widget contents margins. 父窗口小部件的内容页边距。
  • The layout contents margins. 布局内容页边距。

The solution is to set the contents margins to the required value in the custom widget constructor. 解决方案是在自定义窗口小部件构造函数中将内容边距设置为所需值。 And when creating the layout, to set his contents margins to 0. 并且在创建布局时,将其内容边距设置为0。

this->setContentsMargins(left, top, right, bottom);
// ...
layout->setContentsMargins(0,0,0,0);

The Layout setGeometry is called by QWidget resize event, and set children widgets size according with it. Layout setGeometry由QWidget resize事件调用,并根据它设置子控件的大小。 As all functions are not virtual, it is not possible (or at least difficult) to modify in this behavior. 由于所有功能都不是虚拟的,因此不可能(或至少很难)修改此行为。

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

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