【问题标题】:How to re-enable close button in qt dialog如何在qt对话框中重新启用关闭按钮
【发布时间】:2015-11-03 23:08:49
【问题描述】:

我正在使用QProgressDialog,当我启动进度条时,我禁用了关闭 (x) 按钮。

progress->setWindowFlags(progress->windowFlags() & ~Qt::WindowCloseButtonHint);

QProcess 中的操作完成后,在完成的插槽中,我重新启用了关闭按钮,但它不起作用。而是关闭进度窗口。我已经尝试了下面的两行,但效果相同。

progress->setWindowFlags(progress->windowFlags() | Qt::WindowCloseButtonHint);

progress->setWindowFlags(progress->windowFlags() | Qt::WindowCloseButtonHint | Qt::CustomizeWindowHint);

为什么它没有按应有的方式工作?

【问题讨论】:

  • 代码:setWindowFlags(windowFlags() & ~Qt::WindowCloseButtonHint); setWindowFlags(windowFlags() | Qt::WindowCloseButtonHint); 有效且按预期工作(Qt5.2)您的连接可能有问题,插槽?
  • @Chernobyl 它几乎不可能是连接。当QProgressDialog 启动时,我还删除了“取消”按钮,并在进程通过progress->setCancelButtonText("Ok");完成时在同一插槽中显示“确定”按钮,这工作正常,下一行启用 x 按钮没有,而是关闭对话框.如果我评论这一行,对话框会按预期保留在那里。
  • 哦,我理解并重现了这个问题,真的!我会试着弄清楚。

标签: qt


【解决方案1】:

我发现了问题。你的对话框被隐藏了,没有办法解决这个问题。您只能再次show()

正如doc所说:

注意:此函数在更改标志时调用 setParent() 窗口,导致小部件被隐藏。您必须调用 show() 来制作 小部件再次可见。

来自 Qt 源码:

void QWidget::setWindowFlags(Qt::WindowFlags flags)
{
    if (data->window_flags == flags)
        return;

    Q_D(QWidget);

    if ((data->window_flags | flags) & Qt::Window) {
        // the old type was a window and/or the new type is a window
        QPoint oldPos = pos();
        bool visible = isVisible();
        setParent(parentWidget(), flags);
        ^^^^^^^^^

        // if both types are windows or neither of them are, we restore
        // the old position
        if (!((data->window_flags ^ flags) & Qt::Window)
            && (visible || testAttribute(Qt::WA_Moved))) {
            move(oldPos);
        }
        // for backward-compatibility we change Qt::WA_QuitOnClose attribute value only when the window was recreated.
        d->adjustQuitOnCloseAttribute();
    } else {
        data->window_flags = flags;
    }
}

正如doc 再次所说:

注意:小部件在更改其父级时变得不可见, 即使它以前是可见的。您必须调用 show() 来制作 小部件再次可见。

例如:

MainWindow w;w.show();
w.setWindowFlags(w.windowFlags() & ~Qt::WindowCloseButtonHint);
w.setWindowFlags(w.windowFlags() | Qt::WindowCloseButtonHint);
w.show();

【讨论】:

  • 哇,这是一个有趣的解决方案,结果却是一个棘手的解决方案。谢谢!
猜你喜欢
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
  • 2017-04-12
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 1970-01-01
  • 2012-10-24
相关资源
最近更新 更多