Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to toggle WindowCloseButton Hint off and on



  • I am wanting to disable the close window button on a QDialog until a process has finished, I want to disable and enable the button using a boolean as a toggle.

    I have written this function but unfortunately I am having no luck. I saw that when I change the window flags it it calls setParent which hides the dialog, but when using show() to reshow the dialog once hidden the application stops responding and I cannot work out what I have done wrong.

    void NodeLogDialog::enableButtons(bool bEnabled)
    {
    	ui->btnOK->setEnabled(bEnabled);
    	ui->btnSave->setEnabled(bEnabled);
    
    	Qt::WindowFlags flags = this->windowFlags();
    
    	if (bEnabled)
    	{
    		setWindowFlags(windowFlags() | (Qt::WindowContextHelpButtonHint) | (Qt::WindowCloseButtonHint));
    	}
    	else
    	{
    		setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint) & (~Qt::WindowCloseButtonHint));
    	}
    
    	show();
    }
    

    Thank you in advance


  • Lifetime Qt Champion

    Hi,

    What version of Qt are you using ?
    What OS are you running ?
    When/How is that method called in your application ?



  • @SGaist

    Hey,

    I am using QT 5.1.0 and I am running Windows 10 Enterprise.

    I create a node dialog object and then call exec from my main window class.

    NodeLogDialog oNodeLogDialog(moSuppMgrHubAddress,
                                        oSelectedNodes,
                                        this);
    
    oNodeLogDialog.exec();
    

    The code below is a function within NodeLogDialog.

    void NodeLogDialog::enableButtons(bool bEnabled)
    {
    	ui->btnOK->setEnabled(bEnabled);
    	ui->btnSave->setEnabled(bEnabled);
    
    	Qt::WindowFlags flags = this->windowFlags();
    
    	if (bEnabled)
    	{
    		setWindowFlags(windowFlags() | (Qt::WindowContextHelpButtonHint) | (Qt::WindowCloseButtonHint));
    	}
    	else
    	{
    		setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint) & (~Qt::WindowCloseButtonHint));
    	}
    
    	show();
    }
    

  • Lifetime Qt Champion

    @AaronKelsey said in How to toggle WindowCloseButton Hint off and on:

    void NodeLogDialog::enableButtons(bool bEnabled)

    Where do you call it?
    Keep in mind that exec() is a blocking call, so you need to call that method before exec().



  • @jsulm I call this function twice after I call exec(). If exec is a blocking call what method can I use to get around this? Whilst the dialog is open using exec I need to disable and enable the buttons.


  • Lifetime Qt Champion

    @AaronKelsey Initially you can call it just before exec().
    Afterwards you can use signals/slots to let the dialog know that you want to enable/disable buttons.
    But I'm wondering what the trigger is (or who calls it then) after exec() as that one is blocking?



  • @jsulm I am not sure whether I have fully understood your question but the toggle for enabling/disabling the dialog close button is handled and called within the dialog class.


Log in to reply