QSplitter 未正确设置大小

发布于 2024-12-20 22:55:07 字数 926 浏览 2 评论 0原文

我有一个带有两个小部件的水平 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港小镇 2024-12-27 22:55:07

我认为实际的操作顺序是:

  1. 保存2个小部件的大小;
  2. 添加第三个小部件。
  3. 设置 3 个小部件的大小(默认情况下 sizes.at(2)0
  4. 删除第二个小部件。

这是由 deleteLater() 引起的code> 仅安排删除,实际删除是在退出 swapLayout() 方法后处理的,请尝试使用 delete rightWidget; 而不是。 rightWidget->deleteLater(); 如果可能的话,或者在删除 rightWidget 和添加新的之间处理事件。

I think the actual order of operations is:

  1. saving sizes of 2 widgets;
  2. adding 3rd widget.
  3. setting size for 3 widgets (and sizes.at(2) is 0 by default.
  4. Deleting 2nd widget.

It caused by the fact that deleteLater() only schedules deleting, and actual deleting is processed after you exit swapLayout() method. Try delete rightWidget; instead of rightWidget->deleteLater(); if it possible. Or process events between deleting rightWidget and adding new one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文