QT Cancel MdiChild creation

Ingo Förster
Ingo Förster used Ask the Experts™
on
In my app I use an mdiArea as central widget. Inside this I create different Childs.

Like:
    MdiChildDiskInfo *child = new MdiChildDiskInfo(this,strDriveName);
    if(child){
        mdiArea->addSubWindow(child);
        child->showMaximized();
    }

Open in new window


The MdiChildDiskInfo is a
class MdiChildDiskInfo : public MdiChildBase
that is basing on
class MdiChildBase : public QMainWindow

Open in new window


The MdiChildDiskInfo is created with
MdiChildDiskInfo::MdiChildDiskInfo(QWidget* parent, const QString &device)
    :strBurnDrive(device){
      setAttribute(Qt::WA_DeleteOnClose);

      this->close();

}

Open in new window


I tried to stop creating the MdiChildDiskInfo with this->close() while the MdiChildDiskInfo is created. The close event is fired.
But always an empty QSubWindow is created. Means child has always an object.

I expect that the child is null when I add this->close() to the constructor. Special in case of Qt::WA_DeleteOnClose. But child is always an object. It looks that the MdiChildDiskInfo is always create also if I say close it.

Finally I want that
MdiChildDiskInfo *child = new MdiChildDiskInfo(this,strDriveName);
will return null to child.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I added a var to the MIDChild class. If an error happens I set this to false and do not 

    MdiChildDiskInfo *child = new MdiChildDiskInfo(this,strDriveName);
    //We will create the child and set the errorhandle inside the class.
    //If success, we call addSubWindow, if not we will delete it.
    if(child->thisSuccessfullCreated==true){
        mdiArea->addSubWindow(child);

        if(isCascaded()==false){
            child->showMaximized();
        }else{
            child->showNormal();
        }
    }else{
        child->close();
    }

Open in new window

Author

Commented:
Sorry, it is not possible to edit the solution text so I make a copy now.

I added a var to the MIDChild class. If an error happens I set this to false and do not add the child to the mdiArea. Lets see if this will cause memleaks. I hope not.

    MdiChildDiskInfo *child = new MdiChildDiskInfo(this,strDriveName);
    //We will create the child and set the errorhandle inside the class.
    //If success, we call addSubWindow, if not we will delete it.
    if(child->thisSuccessfullCreated==true){
        mdiArea->addSubWindow(child);

        if(isCascaded()==false){
            child->showMaximized();
        }else{
            child->showNormal();
        }
    }else{
        child->close();
    }

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial