zar
zar

Reputation: 12247

How to re-enable close button in qt dialog

I am using QProgressDialogand I disable the close (x) button when I start the progress bar.

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

After the operation is complete in QProcess, in the finished slot, I am re-enabling the close button but it doesn't work. It instead closes the progress window. I have tried both lines below but it does the same.

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

or

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

Why is it not working the way it should?

Upvotes: 2

Views: 3006

Answers (1)

Jablonski
Jablonski

Reputation: 18524

I figured out problem. You dialog is hidden and there are no way to solve that. You can only show() it again.

As doc said:

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.

From Qt source:

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;
    }
}

And as doc said again:

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.

For example:

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

Upvotes: 2

Related Questions