如何在qt对话框中重新启用关闭按钮

时间:2015-08-11 15:59:54

标签: qt

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

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

QProcess中完成操作后,在完成的插槽中,我重新启用了关闭按钮,但它不起作用。它改为关闭进度窗口。我在下面尝试了两行,但它也是如此。

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

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

为什么它不按预期方式工作?

1 个答案:

答案 0 :(得分:2)

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

正如doc所说:

  

注意:此函数在更改a的标志时调用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();
相关问题