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

标签 qt

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

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

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

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

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

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

最佳答案

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

doc说:

Note: This function calls setParent() when changing the flags for a window, causing the widget to be hidden. You must call show() to make the widget visible again.

来自 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又说:

Note: The widget becomes invisible as part of changing its parent, even if it was previously visible. You must call show() to make the widget visible again.

例如:

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

关于qt - 如何重新启用qt对话框中的关闭按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31946612/

相关文章:

c++ - Qt 设置 z 或 z-index 的正确方法(以编程方式)

c++ - 简单 QMainWindow 代码的菜单栏不显示,Qt Creator Mac OS

QTCreator 的集成表单编辑器不会加载自定义小部件插件(Designer 会)

c++ - Qt - 从函数中获取响应

Qt:空透明QImage有噪音

c++ - 如何在 Blackberry Cascades、QML 和 C++、QT 的 ListView 中获取 Web View

c++ - 如何使用 QStandardPaths 获取 "ProgramData"路径

python - PyCharm 关闭后仍然显示正在运行的脚本

c++ - QDateEdit 验证

c++ - 当树莓派终端上的更新时间时,QT 上的 exe 文件挂起