如何平均分配QSplitter的宽度
How to equally distribute the width of QSplitter
我在调整 QSplitter
的宽度时遇到问题。我在 Qt Designer 中准备了 UI,现在看起来像这样:
设计器视图:
这是我的 对象检查器:
我的问题是如何将QSplitter
的宽度平均分配到左右区域。我想得到这种效果作为初始尺寸,然后客户可以做任何他想做的事。
我想要的效果如下:
在其他世界,我正在寻找一种方法将左右区域的拆分器初始划分指定为 window 宽度的 50%/50%。有什么选择吗?
感谢您的帮助,请见谅,我刚刚开始学习Qt。
简答
splitter->setSizes(QList<int>({INT_MAX, INT_MAX}));
注意:如果您需要其他比率,微调部分可能包含重要信息。
说明
QSpacer
?号
在这种情况下,您不需要 QSpacer
。 spacer 的目标是填充所有可用的 space.
QSplitter
提供了两种方法来指定其children的大小:
1。 QSplitter::setStretchFactor
?号
虽然 拉伸因子 似乎是您要指定 child 小部件的相对大小的东西,但它对您的情况有不利影响这些因素与 child 小部件的初始大小有关:
stretch is not the effective stretch factor; the effective stretch
factor is calculated by taking the initial size of the widget and
multiplying it with stretch.
因此,将两个 children 的拉伸因子设置为相同的值(例如 1
)效果不佳:
splitter->setStretchFactor(0, 1);
splitter->setStretchFactor(1, 1);
2。 QSplitter::setSizes
?是的。
更好的选择是使用 setSizes
,因为只有相对大小才重要:
The overall size of the splitter widget is not affected. Instead, any
additional/missing space is distributed amongst the widgets according
to the relative weight of the sizes.
因此,可以认为将两个大小都设置为 1 会将总大小平均分配给 children:
splitter->setSizes(QList<int>({1, 1}));
不,因为您应该考虑到最小尺寸政策将通过用最小尺寸替换太小的值来强制执行:
The size policies of the widgets are preserved. That is, a value
smaller than the minimal size hint of the respective widget will be
replaced by the value of the hint.
因此,您应该使用大于 children 的最小大小的值,例如可以用 int
表示的最大数字,即 INT_MAX
:
splitter->setSizes(QList<int>({INT_MAX, INT_MAX}));
微调
如评论中所建议,INT_MAX
可能会导致一些溢出问题,具体取决于 Qt 对这些值所做的计算。如果您想要 non-equal 的分配比率,情况可能尤其如此。因此,比较合理的大值可能是桌面width/height.
// Horizontal splitter
int largeWidth = QGuiApplication::primaryScreen ()->virtualSize ().width ();
splitter->setSizes(QList<int>({largeWidth , largeWidth}));
// Vertical splitter
int largeHeight = QGuiApplication::primaryScreen ()->virtualSize ().height ();
splitter->setSizes(QList<int>({largeHeight , largeHeight}));
进一步阅读
大多数时候,Qt 都能很好地为所有小部件提供合适的大小,但调整它可能会变得非常困难,需要反复试验。因此,我想提供一些在 Qt 中使用布局管理时可能有用的额外资源:
- Qt documentation about layout management
- A nice summary of layout management in Qt
- 示例:
基于
要等分拆分器,请使用 QSplitter::setSizes
,列表中的每个大小都应该相等。使用 QWidget::minimumSizeHint()
确定要使用的大小。使用您添加到拆分器的每个小部件的最大宽度(对于水平拆分器)或最大高度(对于垂直拆分器)
// Example for a horizontal splitter
splitter = new QSplitter(Qt::Horizontal, this);
splitter->addWidget(widgetLeft);
splitter->addWidget(widgetRight);
auto equalWidth = std::max(widgetLeft->minimumSizeHint().width(),
widgetRight->minimumSizeHint().width());
splitter->setSizes({ equalWidth, equalWidth });
// Example for a vertical splitter
splitter = new QSplitter(Qt::Vertical, this);
splitter->addWidget(widgetTop);
splitter->addWidget(widgetBottom);
auto equalHeight = std::max(widgetTop->minimumSizeHint().height(),
widgetBottom->minimumSizeHint().height());
splitter->setSizes({ equalHeight, equalHeight });
我会建议进一步的变化。 m7913d 的要点都成立并且很重要。但就我的情况 (Qt5.13.2) 而言,我发现设置足够大的尺寸可能 仍然 会导致一些变化。我不知道为什么,但是将所有拉伸因子设置为一个 另外 最终表现得像我想要的那样。
另请注意,将所有拉伸因子设置为 1 但使用不相等(但仍然足够大以避免最小尺寸限制)尺寸 setSizes
也可以!例如:
splitter->setStretchFactor(0,1);
splitter->setStretchFactor(1,1);
splitter->setSizes({static_cast<int>(10000 / 1.618), static_cast<int>(10000 - 10000 / 1.618)});
设置黄金分割比例。
我在调整 QSplitter
的宽度时遇到问题。我在 Qt Designer 中准备了 UI,现在看起来像这样:
设计器视图:
这是我的 对象检查器:
我的问题是如何将QSplitter
的宽度平均分配到左右区域。我想得到这种效果作为初始尺寸,然后客户可以做任何他想做的事。
我想要的效果如下:
在其他世界,我正在寻找一种方法将左右区域的拆分器初始划分指定为 window 宽度的 50%/50%。有什么选择吗?
感谢您的帮助,请见谅,我刚刚开始学习Qt。
简答
splitter->setSizes(QList<int>({INT_MAX, INT_MAX}));
注意:如果您需要其他比率,微调部分可能包含重要信息。
说明
QSpacer
?号
在这种情况下,您不需要 QSpacer
。 spacer 的目标是填充所有可用的 space.
QSplitter
提供了两种方法来指定其children的大小:
1。 QSplitter::setStretchFactor
?号
虽然 拉伸因子 似乎是您要指定 child 小部件的相对大小的东西,但它对您的情况有不利影响这些因素与 child 小部件的初始大小有关:
stretch is not the effective stretch factor; the effective stretch factor is calculated by taking the initial size of the widget and multiplying it with stretch.
因此,将两个 children 的拉伸因子设置为相同的值(例如 1
)效果不佳:
splitter->setStretchFactor(0, 1);
splitter->setStretchFactor(1, 1);
2。 QSplitter::setSizes
?是的。
更好的选择是使用 setSizes
,因为只有相对大小才重要:
The overall size of the splitter widget is not affected. Instead, any additional/missing space is distributed amongst the widgets according to the relative weight of the sizes.
因此,可以认为将两个大小都设置为 1 会将总大小平均分配给 children:
splitter->setSizes(QList<int>({1, 1}));
不,因为您应该考虑到最小尺寸政策将通过用最小尺寸替换太小的值来强制执行:
The size policies of the widgets are preserved. That is, a value smaller than the minimal size hint of the respective widget will be replaced by the value of the hint.
因此,您应该使用大于 children 的最小大小的值,例如可以用 int
表示的最大数字,即 INT_MAX
:
splitter->setSizes(QList<int>({INT_MAX, INT_MAX}));
微调
如评论中所建议,INT_MAX
可能会导致一些溢出问题,具体取决于 Qt 对这些值所做的计算。如果您想要 non-equal 的分配比率,情况可能尤其如此。因此,比较合理的大值可能是桌面width/height.
// Horizontal splitter
int largeWidth = QGuiApplication::primaryScreen ()->virtualSize ().width ();
splitter->setSizes(QList<int>({largeWidth , largeWidth}));
// Vertical splitter
int largeHeight = QGuiApplication::primaryScreen ()->virtualSize ().height ();
splitter->setSizes(QList<int>({largeHeight , largeHeight}));
进一步阅读
大多数时候,Qt 都能很好地为所有小部件提供合适的大小,但调整它可能会变得非常困难,需要反复试验。因此,我想提供一些在 Qt 中使用布局管理时可能有用的额外资源:
- Qt documentation about layout management
- A nice summary of layout management in Qt
- 示例:
基于
要等分拆分器,请使用 QSplitter::setSizes
,列表中的每个大小都应该相等。使用 QWidget::minimumSizeHint()
确定要使用的大小。使用您添加到拆分器的每个小部件的最大宽度(对于水平拆分器)或最大高度(对于垂直拆分器)
// Example for a horizontal splitter
splitter = new QSplitter(Qt::Horizontal, this);
splitter->addWidget(widgetLeft);
splitter->addWidget(widgetRight);
auto equalWidth = std::max(widgetLeft->minimumSizeHint().width(),
widgetRight->minimumSizeHint().width());
splitter->setSizes({ equalWidth, equalWidth });
// Example for a vertical splitter
splitter = new QSplitter(Qt::Vertical, this);
splitter->addWidget(widgetTop);
splitter->addWidget(widgetBottom);
auto equalHeight = std::max(widgetTop->minimumSizeHint().height(),
widgetBottom->minimumSizeHint().height());
splitter->setSizes({ equalHeight, equalHeight });
我会建议进一步的变化。 m7913d 的要点都成立并且很重要。但就我的情况 (Qt5.13.2) 而言,我发现设置足够大的尺寸可能 仍然 会导致一些变化。我不知道为什么,但是将所有拉伸因子设置为一个 另外 最终表现得像我想要的那样。
另请注意,将所有拉伸因子设置为 1 但使用不相等(但仍然足够大以避免最小尺寸限制)尺寸 setSizes
也可以!例如:
splitter->setStretchFactor(0,1);
splitter->setStretchFactor(1,1);
splitter->setSizes({static_cast<int>(10000 / 1.618), static_cast<int>(10000 - 10000 / 1.618)});
设置黄金分割比例。