QLayout是Qt应用开发中一个非常重要的组件,然而平时使用的时候不小心经常会发现控制台有类似如下的警告: QLayout: Attempting to add QLayout "" to studio 但是实际上,如果不注意的话,任意的QWidget上使用QLayout都会出现这个警告。其实原因一般有两个: (1)已经显式设置了QLayout后,再次给QWidget设置QLayout。 解决办法是优化代码或者删掉其中一个QLayout。 (2)隐式的设置了QLayout后,再次给QWidget设置QLayout。这种情况是怎么产生的呢? this); centralWidget->setLayout(mainLayout); ## 3 setWidget(centralWidget); } 其中,1和2两处代码通过this指针设置了QLayout
QWidget的setLayout源码中可以看到布局控件(QLayout)对象最终会设置自己的父类为QWidget。 /* 省略部分无关代码 */ void QWidget::setLayout(QLayout *l) { ... if (oldParent ! 的addWidget内部实现是传入的w对象最终也会设置其QLayout的parentWidget()对象。 void QLayout::addWidget(QWidget *w) { addChildWidget(w); addItem(QLayoutPrivate::createWidgetItem (this, w)); } void QLayout::addChildWidget(QWidget *w) { QWidget *mw = parentWidget(); QWidget
QGridLayout 栅格布局 方法: addLayout(QLayout, row, column, Qt.Alignment) 在栅格布局的行(row)、列(column)位置添加新的布局,并设置对齐方式 addLayout(QLayout, row, column, rowSpan, columnSpan, Qt.Alignment) 在栅格布局中新的布局,从行(row)、列(column)开始,占据 ) addRow(str, QWidget) addRow(str, QLayout) 以上在表单布局最后添加一行数据,设置表单的标签和控件 addRow(QWidget) addRow(QLayout ) QWidget和QLayout添加在最后一行,并占据两列宽度 insertRow(row, QWidget, QWidget) insertRow(row, QWidget, QLayout) insertRow (row, str, QWidget) insertRow(row, str, QLayout) 在指定行添加标签和控件 insertRow(row, QWidget) insertRow(row, QLayout
还需要配合每一个控件本身的 sizePolicy() 以及布局的 sizeConstraint() 方可显示出Qt动态布局的威力 updateGeometry() 并不是每次调用都一定会生效,有时候你需要 QLayout 这个时候你可以选择拿到控件的 QLayout 然后循环对每个布局调用 QLayout::invalidate() ;在比较特殊的情况下你可以选择用 setFixedSize(sizeHint()) 这种奇怪的调用代替循环 尽量使用 QLayout 来控制控件的位置,而不是套用一个又一个的 QWidget ,因为永远不能预料到产品会把一个页面上的哪两个控件联系在一起,如果这些控件相隔了几个 QWidget ,拿到它们的指针将是一件非常蛋疼的事
这些类都从QLayout继承而来,它们都来源于QObject(而不是QWidget)。创建更加复杂的布局,可以让它们彼此嵌套完成。 在某些情况下,父类布局被放入QLayout::FreeResize模式,这意味着它将不适应内容布局所设置的最小窗口,或者甚至阻止用户让窗口小到不可用的情况。 它分层管理每个元素,每个元素的通过QLayout::spacing()来设置位移量。 编写自定义布局类,必须定义以下内容: l 由布局控制的存放元素的数据结构。 { public: CardLayout(QWidget *parent, int dist): QLayout(parent, 0, dist) {} CardLayout(QLayout *parent, int dist): QLayout(parent, dist) {} CardLayout(int dist): QLayout(dist) {} ~CardLayout
边界会非常大,很难看 ui.horizontalLayout->setContentsMargins(0, 0, 0, 0); } 4.关于函数setContentsMargins() void QLayout By default, QLayout uses the values provided by the style. PM_LayoutLeftMargin, PM_LayoutTopMargin, PM_LayoutRightMargin, and PM_LayoutBottomMargin. */ void QLayout By default, QLayout uses the values provided by the style.
.QtGui import QFont, QFontDatabase from PySide6.QtWidgets import QApplication, QHBoxLayout, QLabel, QLayout self.init_scroll_area(fonts_v_layout) self.setCentralWidget(fonts_area) def init_scroll_area(self, layout: QLayout
include <QApplication> #include <QDialog> #include <QPushButton> #include <QLineEdit> #include <QLayout (2)addLayout [cpp] view plaincopy void addLayout ( QLayout * layout, int row, int column, Qt:: Alignment alignment = 0 ) void addLayout ( QLayout * layout, int row, int column, int rowSpan, include <QApplication> #include <QDialog> #include <QPushButton> #include <QLineEdit> #include <QLayout
annotationsfrom PySide6.QtGui import QFontfrom PySide6.QtWidgets import QApplication, QHBoxLayout, QLayout self.setCentralWidget(self.init_scroll_area(v_main_layout)) def init_scroll_area(self, layout: QLayout
int> list; list<<50<<100<<200;//width 为 50 100 200 splitter->setSizes(list); 注意: Note: Adding a QLayout to a QSplitter is not supported (either through setLayout() or making the QSplitter a parent of the QLayout 不支持向QSplitter添加QLayout(通过setLayout()或将QSplitter作为QLayout的父元素); 使用addWidget()代替(参见上面的例子)。
会出现这种提示: QWidget::setLayout: Attempting to set QLayout "" on MainWindow "", which already has a layout **************************************************************************/ void QWidget::setLayout(QLayout
AbstractKeyboard(parent) { QVBoxLayout *layout = new QVBoxLayout(this); layout->setSizeConstraint(QLayout createLayout = [&](ModesList list){ QHBoxLayout *h = new QHBoxLayout; h->setSizeConstraint(QLayout
在Qt中,使用布局器QLayout进行布局,QLayout本质上是一个工具类。 1、它为我们自动计算各个控件的大小和位置 2、当父窗口调整时,它根据既定策略Policy来调整各个子窗口的大小和位置 两个常用的QLayout类: 1、QHBoxLayout:横向布局 2、QVBoxLayout 和 QPlainTextEdit的纵向Policy分别为Fixed和Expanding) SizeHint: 表示窗口的推荐大小 注意这个大小仅仅是推荐大小而不是最终大小 最终的实际大小是由布局器(QLayout
布局管理器可以自定义,从而达到更加个性化界面布局的效果 布局管理器可以相互嵌套,完成所有常用的界面布局 QLayout是Qt中布局管理器的抽象基类,如下图所示: ? stretch ); //设置部件拉伸系数,如果存在*widget这个组件,则设置成功,返回true bool QBoxLayout::setStretchFactor ( QLayout 的形式创建的 表单布局也支持嵌套,可以管理子布局 常用函数: addRow ( QWidget * label, QWidget * field ); addRow ( QWidget * label, QLayout field ); addRow ( const QString & labelText, QWidget * field ); addRow ( const QString & labelText, QLayout
示例代码from PySide6.QtCore import Qtfrom PySide6.QtWidgets import QApplication, QLayout, QMainWindow, QSlider
这有5个主要的布局容器,他们都是继承自QLayout: QHBoxLayout QVBoxLayout QGridLayout QStackedLayout QFormLayout 他们用来满足不同的需求 单参数的版本: 1 addRow(QWidget) 2 addRow(QLayout) 添加widget或布局在整个QFormLayout的最后。 双参数版本: 1 unicode, QLayout 2 unicode, QWidget 3 QWidget, QWidget 4 QWidget, QLayout 作为一个“标签”在第一列中初始化元素
[From Qt Doc: QLayout will never resize a widget to a size smaller than the minimum size hint unless If there is a QLayout that manages this widget's children, the size policy specified by that layout is If there is no such QLayout, the result of this function is used.]
在指定的行列增加widget addWidget(QWidget, int r, int c, int rowspan, int columnspan) 在指定的行列增加widget并设定大小 addLayout(QLayout 该布局可以让我们很方便的创建一个表单布局 即只有两列的网格布局 第一列为label 第二列为输入框 它有如下3个方法 addRow(QLabel, QWidget) 增加包含label和input的一行 addRow(QLabel, QLayout
[6] mainLayout->setSizeConstraint(QLayout::SetFixedSize); mainLayout->addWidget(display, 0, 0, 1, 6);
import os from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import ( QApplication, QLayout self.qscrollarea.setWidget(self.listWidget) self.setWindowTitle("Flow Layout Scroll") class FlowLayout(QLayout