我正在使用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/