QSplitter 未正确设置大小
我有一个带有两个小部件的水平 QSplitter。我想用一个新的小部件替换右侧的小部件,以保持用户设置的比例。下面是我当前拥有的代码的简化版本:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
splitter = new QSplitter(this);
splitter->setOrientation(Qt::Horizontal);
leftWidget = new QPushButton("left", splitter);
rightWidget = new QPushButton("right", splitter);
splitter->addWidget(leftWidget);
splitter->addWidget(rightWidget);
setCentralWidget(splitter);
}
void MainWindow::swapLayout()
{
QList<int> sizes = splitter->sizes();
rightWidget->deleteLater();
splitter->update();
rightWidget = new QPushButton("new right", splitter);
splitter->addWidget(rightWidget);
splitter->setSizes(sizes);
}
swapLayout() 保存大小,删除右侧小部件,添加新的右侧小部件并尝试重置大小。然而,左侧小部件占据了 100% 的空间。在不尝试恢复大小的情况下,两个小部件都占用了 50% 的空间。
I have a horizontal QSplitter with two widgets. I want to replace the right hand widget with a new one in a way that the proportions the user has set are maintained. Below is a simplified version of the code I currently have:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
splitter = new QSplitter(this);
splitter->setOrientation(Qt::Horizontal);
leftWidget = new QPushButton("left", splitter);
rightWidget = new QPushButton("right", splitter);
splitter->addWidget(leftWidget);
splitter->addWidget(rightWidget);
setCentralWidget(splitter);
}
void MainWindow::swapLayout()
{
QList<int> sizes = splitter->sizes();
rightWidget->deleteLater();
splitter->update();
rightWidget = new QPushButton("new right", splitter);
splitter->addWidget(rightWidget);
splitter->setSizes(sizes);
}
swapLayout() saves the sizes, removes the right widget, adds a new right hand widget and attempts to reset the sizes. However the left hand widget occupies 100% of the space. Without trying to restore the sizes the widgets both take up 50% of the space.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为实际的操作顺序是:
sizes.at(2)
为0
。这是由
deleteLater()
引起的code> 仅安排删除,实际删除是在退出swapLayout()
方法后处理的,请尝试使用delete rightWidget;
而不是。rightWidget->deleteLater();
如果可能的话,或者在删除rightWidget
和添加新的之间处理事件。I think the actual order of operations is:
sizes.at(2)
is0
by default.It caused by the fact that
deleteLater()
only schedules deleting, and actual deleting is processed after you exitswapLayout()
method. Trydelete rightWidget;
instead ofrightWidget->deleteLater();
if it possible. Or process events between deletingrightWidget
and adding new one.