通过函数调用缩小 QDialog

发布于 2024-11-13 10:46:58 字数 880 浏览 2 评论 0原文

我有一个由中央应用程序创建和管理的对话。对话框在运行时生成小部件,并具有一个成员函数将对话框恢复为其默认排列,即顶部的按钮框和底部的单个小部件。当对话隐藏时调用此恢复函数。我可以从对话中获取额外的小部件,但我无法让对话本身缩小到原始大小。这是我正在使用的代码,名称已更改为通用名称。

void Dialogue::restore()
{
    const short RESTORE_WIDTH = 800;
    const short RESTORE_HEIGHT = 200;
    QRect newGeometry(frameGeometry());

    // Remove all old origins
    foreach(RuntimeWidget* child, findChildren< RuntimeWidget* >())
        child->deleteLater();

    // Restore widget to default state
    newGeometry.setWidth(RESTORE_WIDTH);
    newGeometry.setHeight(RESTORE_HEIGHT);
    setGeometry(newGeometry);
    updateGeometry();
    addRuntimeWidget();
}

void Dialogue::addRuntimeWidget()
{
    RuntimeWidget* pWidget(new RuntimeWidget());

    vbxlytDialogue->addWidget(pWidget);
    adjustSize();
    adjustPosition(this);
    pWidget->setFocus(Qt::OtherFocusReason);
}

I have a dialogue that is created and managed by the central application. The dialogue generates widgets at runtime and has a member function to restore the dialogue to its default arrangement, I.e. button box at the top and a single widget on the bottom. This restore function is called while the dialogue is hidden. I can get the extra widgets out of the dialogue, but I cannot get the dialogue itself to shrink to it's original size. Here is the code I'm using, names have been changed to be generic.

void Dialogue::restore()
{
    const short RESTORE_WIDTH = 800;
    const short RESTORE_HEIGHT = 200;
    QRect newGeometry(frameGeometry());

    // Remove all old origins
    foreach(RuntimeWidget* child, findChildren< RuntimeWidget* >())
        child->deleteLater();

    // Restore widget to default state
    newGeometry.setWidth(RESTORE_WIDTH);
    newGeometry.setHeight(RESTORE_HEIGHT);
    setGeometry(newGeometry);
    updateGeometry();
    addRuntimeWidget();
}

void Dialogue::addRuntimeWidget()
{
    RuntimeWidget* pWidget(new RuntimeWidget());

    vbxlytDialogue->addWidget(pWidget);
    adjustSize();
    adjustPosition(this);
    pWidget->setFocus(Qt::OtherFocusReason);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

碍人泪离人颜 2024-11-20 10:46:58

我猜您在此处使用 deleteLater 存在问题。只有当您返回到主事件循环时,子窗口小部件才会被删除,并且只有在 restore() 完成之后(即在您调用 <代码>调整大小)。

在调用 deleteLater() 之前,您是否尝试过从子部件所在的布局中删除它们?

foreach(RuntimeWidget* child, findChildren< RuntimeWidget* >()) {
  vbxlytDialogue->removeWidget(child);
  child->deleteLater();
}

(或者类似的东西 - 我只是猜测 vbxlytDialogue 的类型。)

I'm guessing there's a problem with your use of deleteLater here. The child widgets will only get deleted once you go back to the main event loop, and that will only happen after restore() is finished (i.e. after you've called adjustSize).

Have you tried removing the child widgets from whatever layout they're in before calling deleteLater()?

foreach(RuntimeWidget* child, findChildren< RuntimeWidget* >()) {
  vbxlytDialogue->removeWidget(child);
  child->deleteLater();
}

(Or something to that effect - I'm just guessing about the type of vbxlytDialogue.)

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